通过反射访问main方法中的局部变量 [英] Accessing local variables in the main method via reflection
问题描述
只是玩Java反射,我想我大部分时间都在掌握它。我从这个问题/答案中了解在大多数情况下,我只限于静态变量。如果我有一个类的实例,我可以访问非静态变量,这是有道理的,我得到那么多。
Just having a play around with Java reflection and I think I'm getting the hang of it for the most part. I understand from this question/answer that, for the most part, I'm limited to static variables. If I have an instance of the class though, I can access non-static variables, which does make sense, I get that much.
说我有以下两个类:
public class A
{
private static int _staticInt;
public static void main(String[] args)
{
B instanceOfB = new B();
}
}
public class B
{
private int _nonStaticInt;
public Game() {}
}
我理解如何访问 _staticInt
,这不是问题。
我的理解是我可以以相同的方式获得 _nonStaticInt
的字段
(即 Field f = B.class.getDeclaredField(_ nonStaticInt);
)。从其他研究(javadocs,trail等)我收集到我需要一个 B
的实例,以获得 _nonStaticInt $的值c $ c>。
I understand how to access _staticInt
, that's not an issue.
My understanding is that I can get the Field
for _nonStaticInt
in the same way (i.e. Field f = B.class.getDeclaredField("_nonStaticInt");
). From other research (javadocs, trails, etc) I have gathered that I need an instance of B
in order to get the value of _nonStaticInt
.
所以我的问题; 由于 main
是静态的,可以访问 instanceOfB
以访问 _nonStaticInt
的值?我不认为这是可能的,但我认为在放弃这个想法之前,最好先咨询比我更了解的人。
So my question; Since main
is static, is it possible to access instanceOfB
in order to access the value of _nonStaticInt
? I don't think it is possible, but I thought it's always best to consult people that are more knowledgable than myself before giving up on the idea.
推荐答案
由于main是静态的,是否可以访问instanceOfB以访问_nonStaticInt的值?
Since main is static, is it possible to access instanceOfB in order to access the value of _nonStaticInt?
不。使用Java Reflection API无法访问的局部变量(是否在静态方法中)。反射仅适用于类型级别,而不是字节代码级别 2 。
"No." Local variables (being in a static method or not) cannot be accessed with the Java Reflection API. Reflection only works at the type level, not the byte-code level2.
对链接问题的所述理解是正确的; 非静态(实例)字段的反射访问逻辑需要实例。也就是说,问题不在于反映B类型,问题在于获取 B实例(分配给局部变量)以反映。
The stated understanding of the linked question is correct; reflection access of a non-static (instance) field logically requires an instance. That is, the issue then isn't about reflecting on the B type, the issue is about obtaining the B instance (which is assigned to a local variable) to reflect upon.
要做到这一点,B实例必须以某种方式流血 - 例如分配给静态字段或作为参数传递给main 1 中的方法/构造函数 - 以便稍后可以将其与反射一起用作访问实例成员的对象。
To do this the B instance has to be "bled" somehow - e.g. assigned to a static field or passed as an argument to a method/constructor from main1 - so that it can be used with reflection later as the object who's instance members are to be accessed.
最干净的方法可能是将B实例传递到适当的上下文(或DI),也许是IoC的助手......也许更改类型以避免完全使用反射。
The cleanest approach would probably be to pass the B instance down through the appropriate context (or "DI"), perhaps with the aide of IoC .. and maybe changing the type to avoid the use of reflection entirely.
1 另一种流血B实例的方法是附加一个调试器并检查/使用执行框架的主要方法中的局部变量 - 但这听起来像是试图用俱乐部拍打苍蝇。
1 Another possible way to "bleed" the B instance is to attach a debugger and inspect/use the local variable within the main methods executing frame - but this sounds like trying to swat a fly with a club.
2 即使是像BCEL / ASM这样的工具也不会在main方法的执行期间立即提供帮助。相反,它将用于解构方法,添加所需的钩子/代码以流血或使用创建的实例,然后构造一个修改的方法来执行。
2 Even tooling like BCEL/ASM wouldn't immediately help during the execution of the main method. Rather it would be used to deconstruct the method, add in the required hooks/code to "bleed" or use the instance created, and then construct a modified method to execute.
这篇关于通过反射访问main方法中的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!