5 Replies Latest reply: Sep 15, 2008 7:57 AM by masijade RSS

    Using reflection to get private field of a class

    677253
      Hello,

      I need to get an Field reference to a private field of a class, I see that the api provides 2 methods for this :

      1. [getFields()|http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getFields()] which will only return publicly accessible fields.
      2. [getDeclaredFields()|http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getDeclaredFields()] which is not good for me because it will not return inherited fields.

      How can I get full list of fields which are both inherited and defined as private ?

      //1. Or do I need to implement as search that would go down the inheritance tree search for the bean?
      //2. Please see this post which is the reason for the question http://forums.sun.com/thread.jspa?threadID=5331440

      Thank you,
      Maxim.
        • 1. Re: Using reflection to get private field of a class
          791266
          //1. Or do I need to implement as search that would go down the inheritance tree search for the bean?
          Correct
          • 2. Re: Using reflection to get private field of a class
            677253
            kajbj wrote:
            //1. Or do I need to implement as search that would go down the inheritance tree search for the bean?
            Correct
            Yes, I've actually solved this in another thread http://forums.sun.com/thread.jspa?threadID=5331475

            Thank you.
            • 3. Re: Using reflection to get private field of a class
              masijade
              maxim.veksler wrote:
              kajbj wrote:
              //1. Or do I need to implement as search that would go down the inheritance tree search for the bean?
              Correct
              Yes, I've actually solved this in another thread http://forums.sun.com/thread.jspa?threadID=5331475

              Thank you.
              Well, that's a slightly different question, but, in any case, seeing as how it was "solved" well over an hour before your post here (and very nearly an hour before the post that you responded to), don't you think it would have been common courtesy to state that fact in this cross-posted thread, so that the person you responded to wouldn't have had to waste his/her time answering a now non-existant question?
              • 4. Re: Using reflection to get private field of a class
                677253
                I think that a chronological order of events should describe what happened :

                Issue #1 10:55 15/09/2008 - I try to find a solution to retrieving annotations of a field http://forums.sun.com/thread.jspa?threadID=5331440
                Issue #211:13 15/09/2008 - I learn that this can be done using the Field class, so I got looking for a way to getting all the fields http://forums.sun.com/thread.jspa?threadID=5331441
                Issue #3 13:15 15/09/2008 - I find a solution which is nearly working and post the last issue I have. http://forums.sun.com/thread.jspa?threadID=5331475
                Issue #313:26 15/09/2008 - thomas.behr helps me with the issue mentioned from thread 5331475 http://forums.sun.com/thread.jspa?messageID=10424090#10424090
                Issue #2 14:32 15/09/2008 - You (kajbj) comment that I need to use suggested solution. Please note that when I posted this I didn't know about the "right" solution.
                Issue #2 15:01 15/09/2008 - I thank you and link to an example where I implemented the search. I still don't know if the solution I found was the correct one.


                Please understand that I had no intention to create unnecessary hassle.
                • 5. Re: Using reflection to get private field of a class
                  masijade
                  maxim.veksler wrote:
                  I think that a chronological order of events should describe what happened :

                  Issue #1 10:55 15/09/2008 - I try to find a solution to retrieving annotations of a field http://forums.sun.com/thread.jspa?threadID=5331440
                  Issue #211:13 15/09/2008 - I learn that this can be done using the Field class, so I got looking for a way to getting all the fields http://forums.sun.com/thread.jspa?threadID=5331441
                  Issue #3 13:15 15/09/2008 - I find a solution which is nearly working and post the last issue I have. http://forums.sun.com/thread.jspa?threadID=5331475
                  Issue #313:26 15/09/2008 - thomas.behr helps me with the issue mentioned from thread 5331475 http://forums.sun.com/thread.jspa?messageID=10424090#10424090
                  Issue #2 14:32 15/09/2008 - You (kajbj) comment that I need to use suggested solution. Please note that when I posted this I didn't know about the "right" solution.
                  Issue #2 15:01 15/09/2008 - I thank you and link to an example where I implemented the search. I still don't know if the solution I found was the correct one.


                  Please understand that I had no intention to create unnecessary hassle.
                  kaj post above came at 11:32 (according to the time display here), and your post, in the other thread, acknowledging that you now had a solution was at 10:37. So how did you "not know" at that time, that you had a solution.

                  But nevermind, dead conversation anyway, the post would not have been made under any other circumstances than the present (if then) anyway, and we both know it.

                  Edit: And I didn't say you had any intention of purposely wasting people's time, but I don't believe that you had any intentions of not doing so, either (i.e. as in trying to prevent it).