简体   繁体   English

通过反射获取私有字段,在 Java

[英]Get a private field via reflection, in Java

I have a Java class named "MyClass" with a private attribute, of type "AnotherClass".我有一个名为“MyClass”的 Java class 具有私有属性,类型为“AnotherClass”。 MyClass has a private constructor, and "AnotherClass" has public constructor. MyClass 有一个私有构造函数,而“AnotherClass”有一个公共构造函数。 "AnotherClass" has also a private String field, "value", which is initialized in constructor. “AnotherClass”还有一个私有字符串字段“value”,它在构造函数中初始化。 I want to access in "Main" class this String.我想在“主要”class 中访问这个字符串。

The first class:第一个class:

public class MyClass {
    private AnotherClass obj;

    private MyClass() {
        obj = new AnotherClass();
    }
}

The second class:第二个class:

public class AnotherClass {
    private String value;

    public AnotherClass() {
        value = "You got the private value!";
    }
}

The main class:主要class:

public class Main {

    static String name;
    static AnotherClass obj;

    public static void main(String[] args) throws Exception {
        Class myClass;
        myClass = Class.forName("main.MyClass");
        Constructor<MyClass>[] constructors = myClass
            .getDeclaredConstructors();
        for (Constructor c : constructors) {
            if (c.getParameters().length == 0) {
                c.setAccessible(true);
                MyClass myCls= (MyClass) c.newInstance();
                Field myObjField = myClass
                    .getDeclaredField("obj");
                myObjField.setAccessible(true);
                obj = (AnotherClass) myObjField.get(myCls);

                // If "value" is public, the program prints "You got the private value!"
                // So "obj" is returned correctly, via reflection
                // name = obj.value; 
                // System.out.println(name); 

                // Now I want to get the field "value" of "obj"
                Field myStringField = obj.getClass().getDeclaredField("value");
                myStringField.setAccessible(true);

                // This line throws an exception
                name = (String) myStringField.get(obj.getClass());

                System.out.println(name); 
            }
        }
    }
}

I expect to see in the console "You got the private value,": but the program throws an exception:我希望在控制台中看到“你得到了私有值”:但是程序抛出了一个异常:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field main.AnotherClass.value to java.lang.Class

So, I want to retrieve the private field, "value", without modifying "MyClass" and "AnotherClass", and without calling the public constructor from AnotherClass directly in main().所以,我想检索私有字段“value”,而不修改“MyClass”和“AnotherClass”,并且不直接在 main() 中从 AnotherClass 调用公共构造函数。 I want to get the value from "obj".我想从“obj”中获取值。

Change this line更改此行

name = (String) myStringField.get(obj.getClass());

to this对此

name = (String) myStringField.get(obj);

The get method requires an object to access the field of (unless it's a static field) get 方法需要 object 来访问字段(除非它是 static 字段)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM