我可以使用 Java 反射获取有关局部变量的信息吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6816951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get information about the local variables using Java reflection?
提问by Easwaramoorthy K
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?
我可以使用 Java 反射获取有关局部变量的信息吗?
采纳答案by Stephen C
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
假设您正在谈论一个方法或构造函数的局部变量,您无法使用反射找到它们。你必须要么
- use a bytecode library such as BCEL or ASM, or
- use one of the remote debugger APIs.
- 使用字节码库,例如 BCEL 或 ASM,或
- 使用远程调试器 API 之一。
The latter will allow you to access the values of the local variables, but only while the JVM is suspended by the debug agent.
后者将允许您访问局部变量的值,但仅限于 JVM 被调试代理挂起时。
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.
这两种方法都依赖于使用调试信息编译的相关类。具体来说,需要用“局部变量调试信息”编译类;例如使用javac -g ...
. 默认情况下不包括“vars”调试信息。
回答by NPE
In a word, you can't. The names of local variables are not preserved by the compiler.
一句话,你不能。编译器不保留局部变量的名称。
As a quick experiment, I have compiled the following class using Java 6 and default compiler options:
作为快速实验,我使用 Java 6 和默认编译器选项编译了以下类:
public class X {
public static void main(String[] args) {
int var = 2;
System.out.println(var);
}
}
A quick examination of the resulting .class
file reveals that the name of the local variable (var
) didn't make it there.
对生成的.class
文件的快速检查显示局部变量 ( var
)的名称没有出现在那里。
回答by Hunter McMillen
No it is not possible with Java Reflection. Things like local variable names are usually removed by the compiler to provide some obfuscation and to optimize space. There is a byte code library ASMwhich can inspect the state of things at runtime, which may be useful to you.
不,Java 反射是不可能的。编译器通常会删除诸如局部变量名称之类的内容,以提供一些混淆并优化空间。有一个字节码库ASM可以在运行时检查事物的状态,这可能对您有用。
回答by Maurício Linhares
If my local variables you mean instance variables and class variables, here's how you would go:
如果我的局部变量是指实例变量和类变量,那么你会怎么做:
String s = new String("This is a sample");
Class<String> type = s.getClass();
for ( Field f : type.getFields() ) {
System.out.printf("Field %s is of type %s%n", f.getName(), f.getType().getName());
}
If what you mean is variables local to methods/constructors, you can not access them with reflection.
如果您的意思是方法/构造函数的局部变量,则无法通过反射访问它们。
回答by Tomasz Nurkiewicz
You can get access to local variable map using bytecode reverse engineering libraries like ASM. Note however that the name of local variables might not always be present in the bytecode, but the number and types will always be there.
您可以使用字节码逆向工程库(如ASM )访问局部变量映射。但是请注意,局部变量的名称可能并不总是出现在字节码中,但数字和类型将始终存在。
There is no way to obtain this information via reflection. Reflection works on method level, while local variables are on code block level.
没有办法通过反射获得这些信息。反射在方法级别工作,而局部变量在代码块级别。