Java Forum / General / March 2008
Java Reflection with local variables
ash - 15 Mar 2008 18:02 GMT 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
Mark Space - 15 Mar 2008 18:20 GMT > Hello , > [quoted text clipped - 4 lines] > 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>
ash - 15 Mar 2008 23:39 GMT > > Hello , > [quoted text clipped - 15 lines] > > <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
Peter Duniho - 15 Mar 2008 23:51 GMT > 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
ash - 16 Mar 2008 00:08 GMT On Mar 16, 12:51 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> 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 [quoted text clipped - 25 lines] > > 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 - 16 Mar 2008 00:08 GMT On Mar 16, 12:51 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> 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 [quoted text clipped - 25 lines] > > 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 - 16 Mar 2008 00:13 GMT On Mar 16, 12:51 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> 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 [quoted text clipped - 25 lines] > > 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 ...
Lew - 16 Mar 2008 18:09 GMT > 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 [quoted text clipped - 4 lines] > 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.
 Signature Lew
Patricia Shanahan - 16 Mar 2008 01:49 GMT >>> Hello , >>> I have just been introduced to java reflection , and i understand i [quoted text clipped - 22 lines] > 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
ash - 16 Mar 2008 02:03 GMT > 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.
Mark Space - 16 Mar 2008 03:13 GMT >> 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. [quoted text clipped - 11 lines] > 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>
ash - 16 Mar 2008 16:07 GMT > 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. [quoted text clipped - 16 lines] > > <http://www.program-transformation.org/Transform/JavaDecompilers> Thank you Mark :D
Arne Vajhøj - 16 Mar 2008 00:17 GMT > 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
Mark Space - 16 Mar 2008 00:47 GMT > 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|