Results 1 to 12 of 12
  1. #1
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Exclamation 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 :
    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();
        }
    }
    The above code is compiling successfully, but I am getting a IIegalArgumentException when I run the file.

    Here are the details of the Exception that I am getting :
    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
    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 .

    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 !.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    System.out.println(g.get(hm));

  3. #3
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Question I don't understand the get() method in Field class

    Quote Originally Posted by eRaaaa View Post
    System.out.println(g.get(hm));
    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 !.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Quote Originally Posted by wirdsmoth View Post
    If I am not, can anyone give an explanation of the function or the 'object' of the get() method .
    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

  5. #5
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Question

    Quote Originally Posted by eRaaaa View Post
    Parameters:
    obj - object from which the represented field's value is to be extracted
    Thanks for the decisive answer eRaaaa,but where is the field's value extracted to ?.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by wirdsmoth View Post
    Thanks for the decisive answer eRaaaa,but where is the field's value extracted to ?.
    It's returned by the method. Check out the API link in eRaaaa's post above.

  7. #7
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Question

    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

    Java 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 
        */
       
     }
    }
    Thank you for your help

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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

  9. #9
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Question

    Quote Originally Posted by Fubarable View Post
    I believe that get can get the data from the fields, and is the only way I know how to do this.
    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 ! .

    If you're having problems using it, let's see how you're calling it.
    I am modifying the previous illustration and adding code which effectively calls the get() method of the Field class .
    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();
     }
    }
    Note that this is misspelled: getDeclaredFieds
    Please ignore the spelling mistake.

    Thanks for your help !.
    Last edited by wirdsmoth; 10-09-2010 at 03:13 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by wirdsmoth View Post
    Please ignore the spelling mistake.
    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:

    Java Code:
             ob = f[0].get(c);
             
             ob = f[0].get(this);
    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.

  12. #12
    wirdsmoth's Avatar
    wirdsmoth is offline Member
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    6
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Fubarable View Post
    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:
    Java Code:
             ob = f[0].get(c);
             
             ob = f[0].get(this);
    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.
    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.
    I am real sorry,Point noted, I will make sure that I post only real code from now on :o

Similar Threads

  1. Replies: 2
    Last Post: 01-20-2014, 07:45 AM
  2. Replies: 13
    Last Post: 07-09-2010, 05:50 AM
  3. Replies: 0
    Last Post: 01-06-2010, 01:19 PM
  4. Replies: 1
    Last Post: 05-19-2009, 11:03 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •