Java反射不按预期工作

3

我刚刚编写了这段代码,以便更好地理解反射。

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

这段代码输出了“test”,但我预期它应该输出“text”。这是什么原因导致它不能正常工作,我该如何修复?


我对反射不是很了解,但你的“value”声明为“final”可能是原因吗? - Sandeep Singh
但我还是觉得它会工作。 - stonar96
2个回答

5
虽然变量已经正确更新,但它并没有传播到getValue()方法中。
原因是编译器为您优化了程序。
由于编译器知道变量value没有改变,所以它将其编译为直接访问字符串池的内联访问,而不是通过变量。可以通过在类文件上运行java -p来查看此内容。
这可以通过使用初始化块或构造函数来解决字符串常量,或者使语句更复杂以“欺骗”编译器。
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;
    }

}

你可以使用 "test".toString() 代替 "test".concat("")。这不仅仅是关于聪明程度的问题,因为Java语言规范明确规定了这种行为,这比优化更重要,例如指定 "test"+"" 仍然是一个编译时常量意味着你可以在 switch 语句或注解中将其用作 case 标签。 - Holger

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.

所以你在这里使用反射的方式是不正确的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接