从JVM上的堆栈框架获取局部变量 [英] Getting local variables from a stack frame on the JVM

查看:379
本文介绍了从JVM上的堆栈框架获取局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在JVM的当前范围内获取地图或其他数据结构的地方变量,而不使用调试器?也就是说,要获取当前栈帧的局部变量?

Is there any way to get a map or other data structure of the local variables in the current scope in on the JVM without using a debugger? That is, to get the locals of the current stack frame?

我知道有stacktrace对象,但 StackTraceElement 无法访问任何状态。

I know that there are stacktrace objects, but StackTraceElement has no way to get access to any state. It just tells you what method was called where, but not what was in it.

推荐答案

变量名可以包含在类文件中以帮助调试器,但 javac 不会默认做。它需要 -g:vars 选项。

Variable names can be included in class files to aid debuggers, but javac doesn't do it by default. It requires the -g:vars option.

如果存在,程序可以使用字节码工程图书馆(如 ASM )访问本地变量名称和范围。

If it's present, a program could use a byte-code engineering library like ASM to access the local variable names and scope.

此问题是不好的措辞,我认为这是一个重复,其答案可能与您的问题相关。我想补充说,如果你不在乎名字,局部变量类型和范围总是包含在每个方法的属性表中。

While this question is poorly phrased, I think it is a duplicate, and its answers may be relevant to your problem. I would add that if you don't care about the names, the local variable type and scope are always included in each method's attribute table.

这篇关于从JVM上的堆栈框架获取局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆