Java Reflection with local variables

A

ash

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
 
M

Mark Space

ash said:
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>
 
A

ash

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
 
P

Peter Duniho

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
 
A

ash

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

ash

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

ash

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

Mark Space

Arne said:
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.
 
P

Patricia Shanahan

ash said:
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
 
A

ash

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

Mark Space

ash said:
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>
 
A

ash

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
 
L

Lew

ash said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top