[英]Why does accessing a private member of an outer class using reflection throw IllegalAccessException?
鑒於下面的代碼示例,為什么accessUsingReflection
- > theAnswer.get( outer )
拋出IllegalAccessException
而accessDirectly
打印42就好了?
Exception in thread "main" java.lang.IllegalAccessException: Class Outer$Inner can not access a member of class Outer with modifiers "private"
根據這個SO答案 ,我希望它能夠正常工作,因為訪問確實發生了“(......)來自允許訪問它的類”。
import java.lang.reflect.Field;
public class Outer
{
private int theAnswer = 42;
public static class Inner
{
public void accessDirectly( Outer outer )
{
System.out.println( outer.theAnswer );
}
public void accessUsingReflection( Outer outer ) throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
Field theAnswer = Outer.class.getDeclaredField( "theAnswer" );
// Of course, uncommenting the next line will make the access using reflection work.
// field.setAccessible( true );
System.out.println( theAnswer.get( outer ) );
}
}
public static void main( String[] args ) throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
Outer outer = new Outer();
Inner inner = new Inner();
inner.accessDirectly( outer );
inner.accessUsingReflection( outer );
}
}
“為什么”這樣的問題可能很難回答 - 有很多層面的“為什么”,包括為什么設計師選擇這樣做? 規范的哪一部分允許這個? 什么是底層技術細節?
我會回答最后一個,但我不確定這是不是你要追求的。
Java運行時(JVM,安全模型等)在很大程度上不了解內部類。 在大多數情況下,它們是語言問題。
其中一個后果是編譯器使用一些隱藏的技巧來允許內部/外部類訪問彼此的私有成員,即使運行時通常不允許它。
其中一個技巧是你的accessDirectly
方法實際上並沒有直接訪問該字段。 編譯器將一個隱藏方法添加到返回theAnswer
值的外部類。
字段( theAnswer
)仍然是私有的,就運行時安全模型而言,不能在擁有(外部)類之外訪問。
因此,您可以(看似)在Java代碼中執行一些您無法通過反射執行的操作,因為它們依賴於編譯器中未在反射庫中復制的特殊行為。
你可以在這里閱讀更多
你的public static void main( String[] args )
和field private int theAnswer = 42
屬於同一個類,因為accessDirectly
打印42就好了(你可以訪問外部類中的字段),但是當你使用反射時,您正在加載對象Class Class Outer
,其中字段private int theAnswer = 42
是私有的(無法訪問另一個類私有字段)。 如果沒有field.setAccessible( true )
調用,則拋出異常java.lang.IllegalAccessException
。
在此示例中,方法是從同一個類調用,但是您從內部類public static class Inner
調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.