Results 1 to 12 of 12
- 10-07-2010, 04:10 PM #1
Trouble using java.lang.reflect.Field class
Hello,
I tried using java.lang.reflect.Field class Objects to get the fields a particular class at run time and use and get their respective values using get(Object obj) method that is defined in the java.lang.reflect.Field class . Though I was successful in getting the Field objects of a particular class ( by using java.lang.Class class's getDeclaredFields() method) , i am not able to get the 'value' that is stored in a particular Field.
Here is the code which I wrote to experiment with the Field class :
The above code is compiling successfully, but I am getting a IIegalArgumentException when I run the file.Java Code:import java.lang.reflect.*; class TwoSe { void field() { Container<String> hm= new Container<String>("Raeder"); Field[] f=hm.getClass().getDeclaredFields(); String st= new String(); for(Field g : f) { try { System.out.println(g.get(st)); }catch(Exception e) { throw new RuntimeException(e); } } } } class Container<T> { T bag; Container(T n) { bag=n; } T get() {return bag;} } public class Sample { public static void main(String[] args) { TwoSe t = new TwoSe(); t.field(); } }
Here are the details of the Exception that I am getting :
I will be very thankful if you can help set right the above code or provide me with help on how to use the java.lang.reflect.Field class .Java Code:Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: Can not set java.lang.Object field Container.bag to java.lang.String at TwoSe.field(Sample.java:12) at Sample.main(Sample.java:27) Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Object field Container.bag to java.lang.String at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55) at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) at java.lang.reflect.Field.get(Field.java:376) at TwoSe.field(Sample.java:10) ... 1 more
Here is the link to the Documentation page of the java.lang.reflect.Field class, if you need it : Field (Java Platform SE 6)
I thank you for your help !.
- 10-07-2010, 04:14 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
System.out.println(g.get(hm));
- 10-08-2010, 03:59 PM #3
I don't understand the get() method in Field class
I thank you for your terse reply eRaaaa. I now presume that I have misunderstood the function of the get() method . Here is what I understood : "get() in Field class returns the Object containing the value of the field that Field represents " . Am I right in my perception ?. If I am not, can anyone give an explanation of the function or the 'object' of the get() method .
Thank you for your help !.
- 10-08-2010, 04:14 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
See: Field.get()
Parameters:
obj - object from which the represented field's value is to be extracted
Returns:
the value of the represented field in object obj; primitive values are wrapped in an appropriate object before being returned
- 10-08-2010, 05:05 PM #5
-
- 10-09-2010, 01:36 PM #7
Okay , Now I understand the real meaning of this method. What I was actually trying to do was this : get the value that Field is having , for instance
Thank you for your helpJava Code:class Container<T> { T bag; public Container(T s) { st = s } T get() { return st; } void generic() { Field[] f= this.getClass().getDeclaredFieds(); /* * Now I have f[] which has the field of my class Container.My question is whether * is there a way to get the 'data' that is being stored in the field, i.e, in this case, * is it possible to get the data that is stored 'bag' field.This is what i exactly want. and * now I have realized get() cannot do that */ } }
-
I believe that get can get the data from the fields, and is the only way I know how to do this.
If you're having problems using it, let's see how you're calling it.
Note that this is misspelled: getDeclaredFieds
- 10-09-2010, 02:56 PM #9
get() does get the value of the 'field's' value but the its the value of the 'obj' argument field's and not the Field object's own ! .
I am modifying the previous illustration and adding code which effectively calls the get() method of the Field class .If you're having problems using it, let's see how you're calling it.
Java Code:class Container<T> { T bag; public Container(T s) { st = s } T get() { return st; } void generic() { Field[] f= this.getClass().getDeclaredFields(); Container<String> c = new Container<String>("Screw it, Lets Do it"); Object ob = f[0].get(c); System.out.println(ob); /* * The above "System.out.println()" statement will print "Screw it, Lets Do it" * and not "Richard Branson" . What I want to do is , Print "Richard Branson" . * */ } } class Execute { public static void main(String[] args) { Container<String> cs = new Container<String>("Richard Branson"); cs.generic(); } }Please ignore the spelling mistake.Note that this is misspelled: getDeclaredFieds
Thanks for your help !.Last edited by wirdsmoth; 10-09-2010 at 03:13 PM.
-
We can't do this and shouldn't do this. If your code doesn't compile or run for me when we test it due to spelling mistakes, we really can't help you. For example your code above won't compile as you are using undeclared variables such as st and s as well as other problems. We recommend that you post only real code, if you want real help.
-
By the way, the code is doing what you tell it to. You are getting the data held by the first (and only) field for the String held by the c Container object which is the String "Screw it, Lets Do it", not the field held by the current object, this. In other words, there's a world of difference between these two statements:
Please note I had to do a bit of corrections to your code just to get it to run which isn't fair to me or others in this forum if you are asking for free help. Again, please post only real code.Java Code:ob = f[0].get(c); ob = f[0].get(this);
- 10-09-2010, 03:58 PM #12
I Thank you Fubarable for giving me a solution to the problem in hand . I am very grateful to you.You may mark the thread as SOLVED.Java Code:ob = f[0].get(c); ob = f[0].get(this);
I am real sorry,Point noted, I will make sure that I post only real code from now on :oPlease note I had to do a bit of corrections to your code just to get it to run which isn't fair to me or others in this forum if you are asking for free help. Again, please post only real code.
Similar Threads
-
java.lang.reflect.InvocationTargetException - help!!!
By gafa in forum Advanced JavaReplies: 13Last Post: 07-09-2010, 05:50 AM -
Problem with java.lang.reflect.InvocationTargetException
By George R. in forum NetBeansReplies: 0Last Post: 01-06-2010, 01:19 PM -
[SOLVED] [newbie] getting the constructors of a class (java.lang.reflect)
By jon80 in forum New To JavaReplies: 1Last Post: 05-19-2009, 11:03 PM -
Cast Error Caught (change) Class is really: java.lang.String
By barney in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 04:07 PM -
java.lang.NoClassDefFoundError Exception when I invoke to a class outside projectEJB
By Daniel in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-06-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks