Java Access to Local Variable Names

久未见 提交于 2019-12-07 08:41:09

问题


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.

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();

Two questions:

  1. Am I on the right track, or is there a better way to do this?
  2. How do I acquire the ThreadReference to set to ref? LocatableEvent seems to be what I need, but can anyone provide an example on how to use it?

Many thanks in advance!


回答1:


Not a lot of people have experience with this stuff.

Look elsewhere for an answer. I had links to code but they are no longer. Cannot delete this answer because it was accepted answer.




回答2:


Yes, you are on the right track!

For anyone who wants to try to get started with the JDI, the "Trace" example is invaluable:

http://www.docjar.com/docs/api/com/sun/tools/example/trace/package-index.html

It's a usable backbone. It shows you how to use events, which indeed will give you a ThreadReference. It also shows you how to start up the second JVM programmatically.



来源:https://stackoverflow.com/questions/3228770/java-access-to-local-variable-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!