• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection in Java  RSS feed

 
Suman Chaudhuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a class as such :<br><br>

<pre>
public class Employee {<br><br>

private String name;<br>
private Address address;<br>
private Collection phones;<br><br>

//getter and setters for above fields go here<br><br>

}<br><br>

public class Address {<br><br>

private String address1;<br><br>

//getter and setter for address1<br><br>
}<br><br>

public class Phone {<br><br>

private String number;<br><br>

//getter and setter for number<br><br>

}<br><br><br><br>


</pre>

I want to write some reflection code, that given a field name, will drill down to the appropriate class, find the field and set it to "".<br><br>

For example, say the reflection code class name is DataFilter and it has a "filter" method. Then calling :<br><br>

a) DataFilter.filter(this, "name") will set the Employee.name = ""<br><br>

b) DataFilter.filter(this, "Address.address1") will first know that I need to extract the field whose datatype is Address, then look for the "address1" field in Address and set it to ""<br><br>

c) DataFilter.filter(this, Phone.number) will go thru Employee, and once it cant find a field whose datatype is "Phone", will then look to see if there are any collections, and, in this case, when it finds one, will iterate thru the collection and if there are any objects of type "Phone", will set all the "number" fields of that collection of Phone objects to ""<br><br>

I am new to Java and this is a bit complex for me. Can someone please help me out ? If someone can show me how to write this, I would really be grateful.<br><br>

Thank you so much for your time. I really appreciate it.<br><br>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please continue any discussion of this topic here.

"Synergistic", it's not cool to post the same question in multiple forums. Please choose the one forum you feel is most appropriate and post there. That way people's time isn't wasted in multiple redundant conversations.

Also, please be aware that your display name is not in keeping with JavaRanch policy. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!