mat*_*ell 5 java reflection final
有人可以解释为什么以下代码失败?我有以下五个班:
public class TestReplaceLogger {
public static void main(String[] arv) throws Exception {
ClassWithFinalFields classWithFinalFields = new ClassWithFinalFields();
Field field = ClassWithFinalFields.class.getDeclaredField("LOG");
// Comment this line and uncomment out the next line causes program work
Logger oldLogger = (Logger)field.get(null);
//Logger oldLogger = classWithFinalFields.LOG;
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, new MockLogger());
classWithFinalFields.log();
}
}
public class ClassWithFinalFields {
public static final Logger LOG = new RealLogger();
public void log() {
LOG.log("hello");
}
}
public interface Logger {
public void log(String msg);
}
public class RealLogger implements Logger{
public void log(String msg) {
System.out.println("RealLogger: " + msg);
}
}
public class MockLogger implements Logger {
public void log(String msg) {
System.out.println("MockLogger: " + msg);
}
}
Run Code Online (Sandbox Code Playgroud)
代码尝试做的是使用反射来替换ClassWithFinalFields类中的LOG变量.就目前而言,该类IllegalAccessException
在尝试设置字段结束时抛出TestReplaceLogger
.
但是,如果我更换
Logger oldLogger = (Logger)field.get(null);
Run Code Online (Sandbox Code Playgroud)
同
Logger oldLogger = classWithFinalFields.LOG;
Run Code Online (Sandbox Code Playgroud)
然后代码运行没有问题,并按预期打印日志"MockLogger:hello".
所以问题是,为什么通过反射阅读最终字段会使程序停止工作?看起来无法再删除最终修饰符,因此您获得了IllegalAccessException,但我不知道为什么.我可以推测这可能与编译器优化或类加载器排序有关,但是,尽管看过字节代码,我还不知道发生了什么.
如果人们想知道我为什么要这么做的话,那就开始寻找一种方法来模拟在单元测试期间的一些笨拙的日志记录,同时我们正在升级一些软件.现在我只是好奇地了解到底是什么.
如果有人想看到它,堆栈跟踪是
Exception in thread "main" java.lang.IllegalAccessException: Can not set static final org.matthelliwell.reflection.Logger field org.matthelliwell.reflection.ClassWithFinalFields.LOG to org.matthelliwell.reflection.MockLogger
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:73)
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:77)
at sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(UnsafeQualifiedStaticObjectFieldAccessorImpl.java:77)
at java.lang.reflect.Field.set(Field.java:741)
at org.matthelliwell.reflection.TestReplaceLogger.main(TestReplaceLogger.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)
您正在绕过其公共API访问字段对象.当然,如果你这样做,任何事情都可能发生.特别地,Java API的不同实现可以表现不同.
在Oracle JDK中,Field假定修饰符为final,因此缓存fieldAccessor(请参阅参考资料Field.getFieldAccessor()
).您已更改修饰符,但未使该缓存无效,从而导致使用旧的字段访问器,这仍然认为该字段是最终的.