Java访问局部变量名称 [英] Java Access to Local Variable Names
问题描述
我正在编写一个程序,在程序中,我想在程序执行期间访问局部变量的变量名,并将其从外部传递出去。我知道Java将在编译期间转储本地变量名称,除非在调试模式下进行编译。
I'm currently writing a program in which I would like to access the variable names of local variables during execution of a program and pass them off externally. I'm aware that Java will dump local variable names during compilation unless compiled in debug mode.
在查看一些内容之后,看起来JDI / JPDA是要走的路对于这种工作。假设 ref
指的是 ThreadReference
,以下是我到目前为止:
After looking around some, it seems that JDI/JPDA is the way to go for this kind of work. Assuming ref
refers to a ThreadReference
, the following is what I have thus far:
ref.suspend();
StackFrame currentFrame = ref.frame(0);
List<LocalVariable> vars = currentFrame.visibleVariables();
ref.resume();
两个问题:
- 我在正确的轨道上,还是有更好的方法呢?
- 如何获取
ThreadReference
设置为ref
? LocatableEvent 似乎是我需要的,但任何人都可以提供一个如何使用它的例子?
- Am I on the right track, or is there a better way to do this?
- How do I acquire the
ThreadReference
to set toref
? LocatableEvent seems to be what I need, but can anyone provide an example on how to use it?
非常感谢提前!
推荐答案
没有很多人有这方面的经验。 >
如果我在你的鞋子我会看看示例代码( http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/technotes/guides/jpda/examples.html )和也可以在 JPDA文档
Not a lot of people have experience with this stuff.
If I were in you shoes I would look at the sample code ( http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/technotes/guides/jpda/examples.html ) and also at the JPDA docs
这篇关于Java访问局部变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!