开发者

Is there a way to get a list of all the variables declared in a java method?

Is there a way to programatically access the variables defined inside a method ( any method, static开发者_高级运维 or not ) ? How are the IDE's showing them when you debug code ?


You may like to read this it explains this quite well. The bit you are looking for is RemoteStackFrame, that is how things can get things that are currently in scope

Its a complicated subject but I hope this helps.


I don't know about the other IDE, but Eclipse uses The Abstract Syntax Tree (AST)


If you are using eclipse, you may use JDT API provided with Eclipse. Those will be available if you will create Plugin project.


IDE's usually use a combination of a debugging api, such as the one provided by the [Java Platform Debugger Architecture][1] and it's knowledge about the structure of the code it is debugging.

Reading the values of local variable (even while debugging), requires that the class files be compiled with debugging information. In Java (as in many other languages), debugging information must be explicitly generated for it to be accessible at runtime. Specially local variables and a mapping from bytecode to source line numbers.

The [JDI][2] api is fairly easy to use to implement a simple debugger. But if what you're looking for is some sort of reflection for local variables, you're out of luck.

The only thing I can think of for this last case is using some sort of bytecode manipulation library such as [BCEL][3] or [ASM][4] and modify the bytecode before loading it to allow you to access the variable values at runtime.

This will surely slow-down the code and be difficult to implement. In the end it will probably be easier to avoid the problem altogether if you step back and think about it.

(sorry for the mangled links but StackOverflow is limiting the number of links)

[1]: http:// java.sun.com/javase/technologies/core/toolsapis/jpda/ [2]: http:// java.sun.com/javase/6/docs/jdk/api/jpda/jdi/index.html [3]: http:// jakarta.apache.org/bcel/ [4]: http:// asm.ow2.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜