Results 1 to 8 of 8

Thread: Reflection

  1. #1
    minama's Avatar
    minama is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Reflection

    Hello,

    I know that it is possible to get variables and method names using reflection, but I just wonder is it possible to get variables from the method? Thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reflection

    Yup, the Reflection framework can find class and instance variables (but no locals!); those are the 'fields' of the class or instance.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    minama's Avatar
    minama is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Reflection

    so, you mean i can not get 'fields' which were declared in methods? :(( is there another option how can I make this?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default Re: Reflection

    WHat are you trying to do?
    Because I can't see a reason to know the internal variables of a method...at least offhand.

  5. #5
    minama's Avatar
    minama is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Reflection

    I have to write a Junit test, but the methods I test are void, thus I'm trying to test objects inside the method... and because I dont have GET's I'm trying to do it this way...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default Re: Reflection

    But why?
    Change the class so you can actually get at the data, or hook into it somehow to get at the data, if that's all you've got to identify if the test has worked.
    And if you can't change the class, then that begs the question of why bother writing a unit test because, if it shows the class is wrong, you can't change it!

    Anyway, you can't get at the internal workings of a method via reflection.

  7. #7
    minama's Avatar
    minama is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Reflection

    I'll change the class, I guess it's a better way

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reflection

    Quote Originally Posted by minama View Post
    so, you mean i can not get 'fields' which were declared in methods? :(( is there another option how can I make this?
    Nope, the method(s) you're talking about might not be running so all of its (local) variables simply don't exist. There is no way to refer to them (they may not be there at that moment) with the reflection mechanism.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Reflection
    By akki in forum Advanced Java
    Replies: 6
    Last Post: 05-04-2011, 08:02 AM
  2. reflection
    By rfabbri in forum Advanced Java
    Replies: 3
    Last Post: 12-07-2010, 08:55 AM
  3. how to use reflection
    By sunilpatel28 in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 10:51 PM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

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