Field的public void setAccessible(boolean flag) 说明
* Set the {@code accessible} flag for this object to * the indicated boolean value. A value of {@code true} indicates that * the reflected object should suppress Java language access * checking when it is used. A value of {@code false} indicates * that the reflected object should enforce Java language access checks.
如果flag设置为true,那么就会压制java语言的访问校验。
Java String 类定义
1 | public final class String |
测试代码:
1 | private static void directChangeStringFinalRef() throws Exception { |
设置为false时,机会出现如下异常:
1 | Exception in thread "main" java.lang.IllegalAccessException: Class book.base.collections.HelloCollections can not access a member of class java.lang.String with modifiers "private final" |