Java反射无法按预期工作

sto*_*r96 3 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是ReflectionTest类.

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

    private String getValue() {
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Fer*_*big 5

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

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

由于编译器知道变量value没有被更改,因此它将其编译为直接访问字符串池的内联访问,而不是通过变量.这可以通过java -p在类文件上运行来看到

这可以通过对字符串常量使用initizer块或构造函数来解决,或者使语句更复杂以"欺骗"编译器.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)