mardi 28 juin 2016

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

Given the code example below, why does theAnswer.get( outer ) throw an IllegalAccessException?

Exception in thread "main" java.lang.IllegalAccessException: Class com.dpd.Outer$Inner can not access a member of class com.dpd.Outer with modifiers "private"

According to this SO answer, I'd expect it to work as the access does happen "(...) from a class that is allowed to access it".

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 );
   }
}





Aucun commentaire:

Enregistrer un commentaire