Java 访问局部变量名

发布于 2024-09-09 05:49:04 字数 679 浏览 9 评论 0原文

我目前正在编写一个程序,我想在程序执行期间访问局部变量的变量名并将它们传递到外部。我知道 Java 会在编译期间转储局部变量名称,除非在调试模式下编译。

环顾四周后,似乎 JDI/JPDA 是此类工作的最佳选择。假设 ref 引用 ThreadReference ,以下是我到目前为止所得到的:

ref.suspend();
StackFrame currentFrame = ref.frame(0);
List<LocalVariable> vars = currentFrame.visibleVariables();
ref.resume();

两个问题:

  1. 我走在正确的轨道上,还是有更好的方法来做到这一点?
  2. 如何获取 ThreadReference 并将其设置为 refLocatableEvent 似乎是我所需要的,但是任何人都可以提供如何使用它的示例吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-09-16 05:49:04

没有多少人有这方面的经验。

到别处寻找答案。我有代码链接,但现在没有了。无法删除此答案,因为它已被接受。

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.

不及他 2024-09-16 05:49:04

是的,您走在正确的道路上!

对于任何想要尝试开始使用 JDI 的人来说,“Trace”示例非常宝贵:

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

这是一个可用的主干。它向您展示了如何使用事件,这确实会给您一个 ThreadReference。它还向您展示了如何以编程方式启动第二个 JVM。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文