对本地变量名称的Java访问(Java Access to Local Variable Names)

编程入门 行业动态 更新时间:2024-10-28 07:27:34
对本地变量名称的Java访问(Java Access to Local Variable Names)

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

看过一些后,看来JDI / JPDA是这种工作的方式。 假设ref引用一个ThreadReference ,以下是我到目前为止:

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

两个问题:

我在正确的轨道上,还是有更好的方法来做到这一点? 我如何获得设置为ref的ThreadReference ? LocatableEvent似乎是我需要的,但任何人都可以提供如何使用它的例子吗?

提前谢谢了!

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:

Am I on the right track, or is there a better way to do this? 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!

最满意答案

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

寻找答案。 我有链接到代码,但他们不再。 无法删除这个答案,因为它被接受的答案。

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.

更多推荐

本文发布于:2023-08-07 00:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   名称   Java   Access   Variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!