Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8061877 Description of nested class access checks is incorrect



      Description claims that the VM will enforce, at runtime, the access flags declared by a member class, and throw an error if separate compilation has rendered a class inaccessible.

      But because the JVM has no first-class semantic support for member classes, it is unable to check 'protected' and 'private' flags of member classes, nor can it restrict access to the member classes of a package-access class. (Note that the JVM encodes reflective information, including source-level access flags, with an InnerClasses attribute, but never uses this information for runtime behavior.)

      So, if the accessibility of a member class or its declaring class changes due to separate compilation, the only time an IllegalAccessError occurs is when the access occurs outside of the member class's package, and the member class is declared with 'private' or package access.


      package p;
      public class Outer {
        public static class Inner {
          public static void m() {}

      public class Test {
        public static void main(String... args) { p.Outer.Inner.m(); }

      First, compile everything. Then:

      - Change the accessibility of 'Inner' from 'public' to 'protected'. No runtime error can be enforced. (This is because Inner must be compiled as a 'public' class; the VM doesn't have a 'protected' flag.)

      - Change the accessibility of 'Outer' from 'public' to package. No runtime error can be enforced. (This is because the VM doesn't know what source-level name was used. Due to inheritance, some source-level names may allow access, while others do not.)

      The spec and reference implementation appear to be unchanged since Java 1.1, despite the longstanding inconsistency.


          Issue Links



              abuckley Alex Buckley
              dlsmith Dan Smith
              0 Vote for this issue
              7 Start watching this issue