繁体   English   中英

为什么使用反射访问外部类的私有成员会抛出IllegalAccessException?

[英]Why does accessing a private member of an outer class using reflection throw IllegalAccessException?

鉴于下面的代码示例,为什么accessUsingReflection - > theAnswer.get( outer )抛出IllegalAccessExceptionaccessDirectly打印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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM