Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reflection

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
Can I only use reflection to enquire about the contents of a Class.

What I want to do is to read in a property file and populate the property value into a MyObject object.
e.g. MyObject have fields String f1,int f2,String f3, etc.
and the properties file I read have key f1,f2,f3

I see some setMethods in the java.lang.Field class, but none in the java.lang.Class class. Is it possible to read in property value of f1 into field f1, etc. etc.??
 
Through reflection you can get a class instance. Depending on your design the instance may be all you need. You can also lookup methods based on signature but I would try to avoid this. Here is a basic example of how to instantiate a class using Reflection.
Code:
Class c = Class.forName("MyObject");
Object myObj = c.newInstance();

This is normally used to instantiate classes of a certain interface. The that case the interface would be used as the object reference. You could then call the methods defined in the interface and let the wonder that is polymorphism call the correct version of the method. Thus the interface is used the define your contract with the objects.

I hope this helps. Wushutwist
 
Here's how it's done:<br><br>First, getting the fields.<br><br><br>ÿ/* if you don't already have a handle to the class, get one from an instantiated object*/<br>Class c = o.getClass();<br>// get the fields for that class<br>Field[] fields = c.getFields();<br>// step through each field<br>for (int i = 0; i &lt; fields.length; i++) {<br>&nbsp;&nbsp;&nbsp;&nbsp;// get a handle on the current field<br>&nbsp;&nbsp;&nbsp;&nbsp;Field currentField = field<i>;<br>&nbsp;&nbsp;&nbsp;&nbsp;// find the type of the field<br>&nbsp;&nbsp;&nbsp;&nbsp;Class fieldType = currentField.getDeclaringClass();<br>&nbsp;&nbsp;&nbsp;&nbsp;if (fieldType == Integer.TYPE) try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentField.setInt (o, yourInt);<br>&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {}<br>&nbsp;&nbsp;&nbsp;&nbsp;else if (fieldType == String.class) try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentField.set (o, yourString);<br>&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {}<br>&nbsp;&nbsp;&nbsp;&nbsp;else if (fieldType == Boolean.TYPE) try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {}<br>&nbsp;&nbsp;&nbsp;&nbsp;else System.err.println(&quot;Unsupported type: &quot; + fieldType.getName());<br>}<br><br>If the fields are private, then you have to jump through more hoops. Let me know if that's an issue.<br><br>I obviously left out the code of how to deal with the properties file; if you need more information let me know. I had to do something very similar, where I was instantiating objects based on a flat text-based database, so I was using a file similar to your properties file. However, I was using the Method class (a close cousin to the Field class). If you need a hand, let me know. Best of luck. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
A bit more of an explanation, now that I'm looking over this.<br><br>The sad thing about this is that you actually have to test your declaring class somewhere. I don't know of any way to get around that. It would be nice if you could do this:<br><br>currentField.set (o, yourValue, currentField.getDeclaringClass());<br><br>where the 3rd parameter would tell the type, but no can do. It'd also be nice if you could instantiate an object (say, a string) with your value like this:<br><br>String myValue = getFromPropertiesFile(it.next());<br>currentField.set (o, myValue);<br><br>but I don't think you can do that. (Probably get a conversion error, like could not convert string to object or something.)<br><br>--<br><br>At first, it's a little weird having the field set itself, but that's what reflection is, really (well, kind of). Basically, the fields are specific to the class, not the object, so you get them from the class. Then, you want to take that field and give it a new value; that's where the object comes in (because each object can have a different value). So, you give the object and the value to the field, and it does the work.<br><br>I hope this helps. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
What if the fields are not standard java classes (e.g. com.fool.MyData, which have its own constructor)?

How do it 'Set' it?
 
same way as you do the rest. It really depends on the class, though- int and string are both rather simple, but others are somewhat dependant. You should just be able to <br><br><FONT FACE=monospace>else if (c == com.fool.MyData) try {<br>&nbsp;&nbsp;&nbsp;&nbsp;currentField.set (o, yourData);<br>}</font><br><br>of course you'd have to play around with this. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top