我可以使用Java反射获取有关局部变量的信息吗? [英] Can I get information about the local variables using Java reflection?
问题描述
我需要知道局部变量的类型。我正在使用Java反射,使用它我无法得到它。能告诉我如何知道局部变量的类型/名称。
I need to know the type of the local variables. I am using Java reflection, using which I could not get it. Can you please let me know how to know the type/name of the local variables.
我可以使用Java反射获取有关局部变量的信息吗?
Can I get information about the local variables using Java reflection?
推荐答案
假设您正在讨论方法或构造函数的局部变量,您无法使用反射找到它们。你必须
Assuming that you are talking about a method or constructor's local variables, you cannot find out about them using reflection. You have to either
- 使用字节码库,如BCEL或ASM,或
- 使用一个远程调试器API。
后者将允许您访问局部变量的值,但仅在JVM被挂起时调试代理。
The latter will allow you to access the values of the local variables, but only while the JVM is suspended by the debug agent.
这两种方法都依赖于使用调试信息编译的有问题的类。具体来说,需要使用局部变量调试信息编译类;例如使用 javac -g ...
。默认情况下不包含vars调试信息。
Both of these approaches rely on the classes in question being compiled with debug information. Specifically, the classes need to be compiled with "local variable debugging information"; e.g. using javac -g ...
. The "vars" debug information is not included by default.
这篇关于我可以使用Java反射获取有关局部变量的信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!