Java反射无法按预期工作

时间:2018-03-15 09:52:34

标签: java reflection

我刚刚编写了这段代码来测试一些东西,以便更好地理解反射。

这是ReflectionTestMain类:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionTestMain {
    public static void main(String[] args) {
        try {
            ReflectionTest rt = new ReflectionTest();
            Class<ReflectionTest> c = ReflectionTest.class;
            Field f = c.getDeclaredField("value");
            f.setAccessible(true);
            f.set(rt, "text");
            Method m = c.getDeclaredMethod("getValue");
            m.setAccessible(true);
            String value = (String) m.invoke(rt);
            System.out.println(value);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

这是ReflectionTest类。

public class ReflectionTest {
    private final String value = "test";

    private String getValue() {
        return value;
    }
}

此代码打印测试但我希望它打印文本。这不起作用的原因是什么,我该如何解决?

2 个答案:

答案 0 :(得分:5)

虽然变量已正确更新,但它不会传播到getValue()方法。

原因是编译器为您优化了程序。

由于编译器知道变量value未被更改,因此它将其编译为直接访问字符串池的内联访问,而不是通过变量。通过在类文件

上运行java -p可以看到这一点

这可以通过对字符串常量使用initizer块或构造函数来解决,或者使语句更加复杂到“傻瓜”#34;编译器。

class ReflectionTest {


    // Use either
    private final String value;
    {
        value = "test";
    }
    // Or 
    private final String value;
    public ReflectionTest () {
        value = "test";
    }
    // Or
    private final String value = Function.identity().apply("test");
    // Or

    //   Do not replace with + as the compiler is too smart
    private final String value = "test".concat(""); 
    // Depending on your required performance/codestyling analyses

    private String getValue() {
        return value;
    }

}

答案 1 :(得分:1)

来自Field.set(..)

上的javadoc
 * <p>If the underlying field is final, the method throws an
 * {@code IllegalAccessException} unless {@code setAccessible(true)}
 * has succeeded for this {@code Field} object
 * and the field is non-static. Setting a final field in this way
 * is meaningful only during deserialization or reconstruction of
 * instances of classes with blank final fields, before they are
 * made available for access by other parts of a program. Use in
 * any other context may have unpredictable effects, including cases
 * in which other parts of a program continue to use the original
 * value of this field.

所以你只是在这里以错误的方式使用反射。