Java Reflection with local variables

Discussion in 'Java' started by ash, Mar 15, 2008.

  1. ash

    ash Guest

    Hello ,

    I have just been introduced to java reflection , and i understand i
    could reflect Classes basic information like methods, constructors,
    and fields. But i would like to further reflect local variables ( say
    local variables in methods). How could i do that with java reflection
    api ? If it cant be done using java reflection api a hint about how it
    could be done would be greatly appreciated :D


    Thanx in advance for your help :D


    Ahmed Ashmawy
     
    ash, Mar 15, 2008
    #1
    1. Advertising

  2. ash

    Mark Space Guest

    ash wrote:
    > Hello ,
    >
    > I have just been introduced to java reflection , and i understand i
    > could reflect Classes basic information like methods, constructors,
    > and fields. But i would like to further reflect local variables ( say
    > local variables in methods). How could i do that with java reflection
    > api ? If it cant be done using java reflection api a hint about how it
    > could be done would be greatly appreciated :D


    Local variables don't belong to classes, they belong to threads. You'd
    need some sort of stack trace or thread dump to access local variables.
    I don't see such a facility in the Thread classs.

    JMX might get you what you want. It is basically a "debugger" tool,
    allowing you to poke at a running JVM and it's application.

    <http://java.sun.com/docs/books/tutorial/jmx/overview/index.html>

    <http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/docs.jsp>
     
    Mark Space, Mar 15, 2008
    #2
    1. Advertising

  3. ash

    ash Guest

    On Mar 15, 7:20 pm, Mark Space <> wrote:
    > ash wrote:
    > > Hello ,

    >
    > > I have just been introduced to java reflection , and i understand i
    > > could reflect Classes basic information like methods, constructors,
    > > and fields. But i would like to further reflect local variables ( say
    > > local variables in methods). How could i do that with java reflection
    > > api ? If it cant be done using java reflection api a hint about how it
    > > could be done would be greatly appreciated :D

    >
    > Local variables don't belong to classes, they belong to threads. You'd
    > need some sort of stack trace or thread dump to access local variables.
    > I don't see such a facility in the Thread classs.
    >
    > JMX might get you what you want. It is basically a "debugger" tool,
    > allowing you to poke at a running JVM and it's application.
    >
    > <http://java.sun.com/docs/books/tutorial/jmx/overview/index.html>
    >
    > <http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement...>




    Thanx for your reply , but i have 2 questions for you though. What do
    you mean local variables belong to threads ? If i am write a new class
    and im defining some variables , methods or any other stuff. these
    elements belong to this class. And second Im checking out JMX now but
    it looks too complicated for what i want to accomplish. What i need is
    an API to access a running Class information ( methods, fields, local
    variables, while loops, for loops , any other thing defined in that
    class).


    Thanx in advance for your help :D

    Ahmed Ashmawy
     
    ash, Mar 15, 2008
    #3
  4. ash

    Peter Duniho Guest

    On Sat, 15 Mar 2008 15:39:50 -0700, ash <> wrote:

    > Thanx for your reply , but i have 2 questions for you though. What do
    > you mean local variables belong to threads ? If i am write a new class
    > and im defining some variables , methods or any other stuff. these
    > elements belong to this class.


    Things in the class belong to the class, including the methods. Yes. But
    variables declared outside of a method exist as part of an instance or as
    part of the class itself (for instance members and static members,
    respectively).

    Variables declared inside a method (i.e. local variables) are stored on
    the stack, and since the stack exists as part of a thread, not part of an
    instance of the class, they belong to the thread. They also don't exist
    at all unless the method in which they are declared is _currently_
    executing (that is, it's been called and has not returned yet).

    I can't answer the more specific question about using reflection or
    similar tools to get at local variables. But you'll definitely need to
    understand why a local variable is different from a class member in order
    to use whatever mechanism might eventually turn out to be useful.

    I have to say: I have some skepticism that someone who doesn't already
    understand the difference between a local variable in a method and an
    instance or static variable in a class should be writing code that does
    any sort of reflection, never mind digs directly into the stack to get at
    local variables. Sounds to me like a recipe for some serious headaches,
    if not out and out disaster.

    Pete
     
    Peter Duniho, Mar 15, 2008
    #4
  5. ash

    ash Guest

    On Mar 16, 12:51 am, "Peter Duniho" <>
    wrote:
    > On Sat, 15 Mar 2008 15:39:50 -0700, ash <> wrote:
    > > Thanx for your reply , but i have 2 questions for you though. What do
    > > you mean local variables belong to threads ? If i am write a new class
    > > and im defining some variables , methods or any other stuff. these
    > > elements belong to this class.

    >
    > Things in the class belong to the class, including the methods. Yes. But
    > variables declared outside of a method exist as part of an instance or as
    > part of the class itself (for instance members and static members,
    > respectively).
    >
    > Variables declared inside a method (i.e. local variables) are stored on
    > the stack, and since the stack exists as part of a thread, not part of an
    > instance of the class, they belong to the thread. They also don't exist
    > at all unless the method in which they are declared is _currently_
    > executing (that is, it's been called and has not returned yet).
    >
    > I can't answer the more specific question about using reflection or
    > similar tools to get at local variables. But you'll definitely need to
    > understand why a local variable is different from a class member in order
    > to use whatever mechanism might eventually turn out to be useful.
    >
    > I have to say: I have some skepticism that someone who doesn't already
    > understand the difference between a local variable in a method and an
    > instance or static variable in a class should be writing code that does
    > any sort of reflection, never mind digs directly into the stack to get at
    > local variables. Sounds to me like a recipe for some serious headaches,
    > if not out and out disaster.
    >
    > Pete


    Thank you Pete for the explanation :D and also to answer you , if i am
    not clear on something , then i understand it and i move on to
    whatever the next step is. So dont worry about my coding. but thanx
    though. Another thing for whomever is checking out this discussion, if
    you can provide some hints on the direction i should be going to to
    solve my problem i would greatly appreciate it. I have already tried
    using the new Compiler API but it doesnt have much documentation to
    help me use it ...
     
    ash, Mar 15, 2008
    #5
  6. ash

    ash Guest

    On Mar 16, 12:51 am, "Peter Duniho" <>
    wrote:
    > On Sat, 15 Mar 2008 15:39:50 -0700, ash <> wrote:
    > > Thanx for your reply , but i have 2 questions for you though. What do
    > > you mean local variables belong to threads ? If i am write a new class
    > > and im defining some variables , methods or any other stuff. these
    > > elements belong to this class.

    >
    > Things in the class belong to the class, including the methods. Yes. But
    > variables declared outside of a method exist as part of an instance or as
    > part of the class itself (for instance members and static members,
    > respectively).
    >
    > Variables declared inside a method (i.e. local variables) are stored on
    > the stack, and since the stack exists as part of a thread, not part of an
    > instance of the class, they belong to the thread. They also don't exist
    > at all unless the method in which they are declared is _currently_
    > executing (that is, it's been called and has not returned yet).
    >
    > I can't answer the more specific question about using reflection or
    > similar tools to get at local variables. But you'll definitely need to
    > understand why a local variable is different from a class member in order
    > to use whatever mechanism might eventually turn out to be useful.
    >
    > I have to say: I have some skepticism that someone who doesn't already
    > understand the difference between a local variable in a method and an
    > instance or static variable in a class should be writing code that does
    > any sort of reflection, never mind digs directly into the stack to get at
    > local variables. Sounds to me like a recipe for some serious headaches,
    > if not out and out disaster.
    >
    > Pete


    Thank you Pete for the explanation :D and also to answer you , if i am
    not clear on something , then i understand it and i move on to
    whatever the next step is. So dont worry about my coding. but thanx
    though. Another thing for whomever is checking out this discussion, if
    you can provide some hints on the direction i should be going to to
    solve my problem i would greatly appreciate it. I have already tried
    using the new Compiler API but it doesnt have much documentation to
    help me use it ...
     
    ash, Mar 15, 2008
    #6
  7. ash

    ash Guest

    On Mar 16, 12:51 am, "Peter Duniho" <>
    wrote:
    > On Sat, 15 Mar 2008 15:39:50 -0700, ash <> wrote:
    > > Thanx for your reply , but i have 2 questions for you though. What do
    > > you mean local variables belong to threads ? If i am write a new class
    > > and im defining some variables , methods or any other stuff. these
    > > elements belong to this class.

    >
    > Things in the class belong to the class, including the methods. Yes. But
    > variables declared outside of a method exist as part of an instance or as
    > part of the class itself (for instance members and static members,
    > respectively).
    >
    > Variables declared inside a method (i.e. local variables) are stored on
    > the stack, and since the stack exists as part of a thread, not part of an
    > instance of the class, they belong to the thread. They also don't exist
    > at all unless the method in which they are declared is _currently_
    > executing (that is, it's been called and has not returned yet).
    >
    > I can't answer the more specific question about using reflection or
    > similar tools to get at local variables. But you'll definitely need to
    > understand why a local variable is different from a class member in order
    > to use whatever mechanism might eventually turn out to be useful.
    >
    > I have to say: I have some skepticism that someone who doesn't already
    > understand the difference between a local variable in a method and an
    > instance or static variable in a class should be writing code that does
    > any sort of reflection, never mind digs directly into the stack to get at
    > local variables. Sounds to me like a recipe for some serious headaches,
    > if not out and out disaster.
    >
    > Pete


    Thank you Pete for the explanation :D and also to answer you , if i am
    not clear on something , then i understand it and i move on to
    whatever the next step is. So dont worry about my coding. but thanx
    though. Another thing for whomever is checking out this discussion, if
    you can provide some hints on the direction i should be going to to
    solve my problem i would greatly appreciate it. I have already tried
    using the new Compiler API but it doesnt have much documentation to
    help me use it ...
     
    ash, Mar 15, 2008
    #7
  8. ash

    Arne Vajhøj Guest

    Mark Space wrote:
    > JMX might get you what you want. It is basically a "debugger" tool,
    > allowing you to poke at a running JVM and it's application.
    >
    > <http://java.sun.com/docs/books/tutorial/jmx/overview/index.html>
    >
    > <http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/docs.jsp>


    I am not sure that I would consider JMX a "debugger" tool.

    It is a management tool.

    JPDA/JVMTI/JDWP/JDI are the acronyms for debugging.

    Arne
     
    Arne Vajhøj, Mar 15, 2008
    #8
  9. ash

    Mark Space Guest

    Arne Vajhøj wrote:

    > I am not sure that I would consider JMX a "debugger" tool.
    >
    > It is a management tool.
    >
    > JPDA/JVMTI/JDWP/JDI are the acronyms for debugging.


    I think you are right. I'd looked up some debugging tools on the 'net a
    while ago, and JMX seemed to have some tools that would do the same
    thing. After a bit more poking, I think it's not, really. JPDA looks
    like what I was thinking of.

    Thanks for the correction.
     
    Mark Space, Mar 15, 2008
    #9
  10. ash wrote:
    > On Mar 15, 7:20 pm, Mark Space <> wrote:
    >> ash wrote:
    >>> Hello ,
    >>> I have just been introduced to java reflection , and i understand i
    >>> could reflect Classes basic information like methods, constructors,
    >>> and fields. But i would like to further reflect local variables ( say
    >>> local variables in methods). How could i do that with java reflection
    >>> api ? If it cant be done using java reflection api a hint about how it
    >>> could be done would be greatly appreciated :D

    >> Local variables don't belong to classes, they belong to threads. You'd
    >> need some sort of stack trace or thread dump to access local variables.
    >> I don't see such a facility in the Thread classs.
    >>
    >> JMX might get you what you want. It is basically a "debugger" tool,
    >> allowing you to poke at a running JVM and it's application.
    >>
    >> <http://java.sun.com/docs/books/tutorial/jmx/overview/index.html>
    >>
    >> <http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement...>

    >
    >
    >
    > Thanx for your reply , but i have 2 questions for you though. What do
    > you mean local variables belong to threads ? If i am write a new class
    > and im defining some variables , methods or any other stuff. these
    > elements belong to this class. And second Im checking out JMX now but
    > it looks too complicated for what i want to accomplish. What i need is
    > an API to access a running Class information ( methods, fields, local
    > variables, while loops, for loops , any other thing defined in that
    > class).


    I would put it slightly differently, and say that local variables belong
    to method activations. Every method activation happens in some thread.
    However, because of recursion there can be several activations of the
    same method in the same thread. There is a set of local variables for
    each activation.

    I agree with the idea that you need to look at this as a debug type of
    operation, processing stack frames, rather than as reflection.

    Can you explain at a higher level what you are trying to do?

    Patricia
     
    Patricia Shanahan, Mar 16, 2008
    #10
  11. ash

    ash Guest


    > I agree with the idea that you need to look at this as a debug type of
    > operation, processing stack frames, rather than as reflection.
    >
    > Can you explain at a higher level what you are trying to do?
    >
    > Patricia


    What i am trying to do is to be able to invoke some code (maybe a
    Class) dynamically ( this can be done in many ways and i know how )
    and then be able to access all of my Class's information ( local
    variables , global variables , objects, methods, while loop
    structures, .... etc. everything written in the code) and specify some
    kind of condition to generate an event ( for example a specific
    variable value has been changed) from the event i can then access to
    variable's new value. But all of this should happen in run time.
     
    ash, Mar 16, 2008
    #11
  12. ash

    Mark Space Guest

    ash wrote:
    >> I agree with the idea that you need to look at this as a debug type of
    >> operation, processing stack frames, rather than as reflection.
    >>
    >> Can you explain at a higher level what you are trying to do?
    >>
    >> Patricia

    >
    > What i am trying to do is to be able to invoke some code (maybe a
    > Class) dynamically ( this can be done in many ways and i know how )
    > and then be able to access all of my Class's information ( local
    > variables , global variables , objects, methods, while loop
    > structures, .... etc. everything written in the code) and specify some
    > kind of condition to generate an event ( for example a specific
    > variable value has been changed) from the event i can then access to
    > variable's new value. But all of this should happen in run time.


    This sounds exactly like what a debugger does. You set a "watch" and
    the watch gets updated so that the user can see the new value.

    Also, you seem at least somewhat experienced, but I want to point out
    that most of the time this is done not with a debugger but by having the
    class do this work for you.

    In other words, the class has been designed so that you can receive
    messages when its internal values change, and the class itself will send
    the event.

    In Java, you can do this with a Listener (often called an Event
    Listener), an Observer and an Observable, or a Property Change Listener.
    This will let you receive changes with out using a debugger.

    But if you have to see everything, right down to the while loops, a
    debugger it is. JPDA looks like a good start. Also check out things
    called de-compilers:

    <http://www.program-transformation.org/Transform/JavaDecompilers>
     
    Mark Space, Mar 16, 2008
    #12
  13. ash

    ash Guest


    > This sounds exactly like what a debugger does. You set a "watch" and
    > the watch gets updated so that the user can see the new value.
    >
    > Also, you seem at least somewhat experienced, but I want to point out
    > that most of the time this is done not with a debugger but by having the
    > class do this work for you.
    >
    > In other words, the class has been designed so that you can receive
    > messages when its internal values change, and the class itself will send
    > the event.
    >
    > In Java, you can do this with a Listener (often called an Event
    > Listener), an Observer and an Observable, or a Property Change Listener.
    > This will let you receive changes with out using a debugger.
    >
    > But if you have to see everything, right down to the while loops, a
    > debugger it is. JPDA looks like a good start. Also check out things
    > called de-compilers:
    >
    > <http://www.program-transformation.org/Transform/JavaDecompilers>



    Thank you Mark :D
     
    ash, Mar 16, 2008
    #13
  14. ash

    Lew Guest

    ash wrote:
    > Thank you Pete for the explanation :D and also to answer you , if i am
    > not clear on something , then i understand it and i move on to
    > whatever the next step is. So dont worry about my coding. but thanx
    > though. Another thing for whomever is checking out this discussion, if
    > you can provide some hints on the direction i should be going to to
    > solve my problem i would greatly appreciate it. I have already tried
    > using the new Compiler API but it doesnt have much documentation to
    > help me use it ...


    The problem is that we don't really know what the problem is. We know that
    you have a particular /strategy/ - to use a complicated library to achieve
    some kind of dynamic, run-time knowledge of an object. We do not know the
    /goal/ you imagine this will achieve.

    Peter Duniho warned you about the approach:
    > Sounds to me like a recipe for some serious headaches, if not out and out disaster.


    and Patricia Shanahan asked after your goals:
    > Can you explain at a higher level what you are trying to do?


    It is near certain that you can accomplish your goal with normal old
    object-oriented programming without impaling yourself on the Judas chair of
    reflection and dynamic compilation.

    --
    Lew
     
    Lew, Mar 16, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pek
    Replies:
    2
    Views:
    1,102
  2. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    695
    Antoon Pardon
    Nov 8, 2007
  3. Gunther Hebein
    Replies:
    8
    Views:
    352
    Daniele Futtorovic
    Aug 26, 2008
  4. Tammo Tjarks
    Replies:
    2
    Views:
    298
    Tammo Tjarks
    Sep 13, 2007
  5. king
    Replies:
    2
    Views:
    181
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page