Getting local variables from a stack frame on the JVM

大憨熊 提交于 2019-12-05 01:28:37
erickson

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.

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.

I don't think there is a way to do this. The compiler is always free to optimise away local variables, and providing such a method would prevent the compiler from taking advantage of this type of optimisation. When compiling a method, the compiler wouldn't be able to tell whether you called a hypothetical getLocalVariables() somewhere else in the program, so it would have to always put all declared local variables in such a map.

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