field.setAccessible(false)
对非私有领域有什么影响?
我预计对此成员的访问会被拒绝,但事实并非如此!
我试图挖掘源代码,但在某些时候(Field#getFieldAccessor(Object))我忘记了正在发生的事情。
我为这种情况创建了一个特殊的类:
public class DemoObject {
public Object publicMember = new Object();
protected Object protectedMember = new Object();
Object defaultMember = new Object();
private Object privateMember = new Object();
public final Object publicfinalMember = new Object();
protected final Object protectedfinalMember = new Object();
final Object defaultfinalMember = new Object();
private final Object privatefinalMember = new Object();
}
并创建了一个Demo:
import java.lang.reflect.Field;
public class MyMain {
public static void main(final String[] args) {
DemoObject object = new DemoObject();
System.out.println("'reflective' member access after field.setAccessible(false)");
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(false);
try {
System.out.println(field.getName() + ": " + field.get(object));
// only throws exception for the privateMember and privatefinalMember
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("'reflective' member access after field.setAccessible(true)");
for (Field field : object.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
System.out.println(field.getName() + ": " + field.get(object));
// throws no exception
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
输出:
'reflective' member access after field.setAccessible(false)
publicMember: java.lang.Object@e9bfee2
protectedMember: java.lang.Object@237aaeec
defaultMember: java.lang.Object@4b721a26
java.lang.IllegalAccessException: Class MyMain can not access a member of class DemoObject with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
at java.lang.reflect.Field.doSecurityCheck(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at MyMain.main(MyMain.java:13)
publicfinalMember: java.lang.Object@7c30cd64
protectedfinalMember: java.lang.Object@67fc9fee
defaultfinalMember: java.lang.Object@7a67f797
java.lang.IllegalAccessException: Class MyMain can not access a member of class DemoObject with modifiers "private final"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
at java.lang.reflect.Field.doSecurityCheck(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at MyMain.main(MyMain.java:13)
'reflective' member access after field.setAccessible(true)
publicMember: java.lang.Object@e9bfee2
protectedMember: java.lang.Object@237aaeec
defaultMember: java.lang.Object@4b721a26
privateMember: java.lang.Object@424c2849
publicfinalMember: java.lang.Object@7c30cd64
protectedfinalMember: java.lang.Object@67fc9fee
defaultfinalMember: java.lang.Object@7a67f797
privatefinalMember: java.lang.Object@15e8f2a0