Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8022701

Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError

XMLWordPrintable

        JSR 335 spec, chapter "15.28.2 Run-time Evaluation of Method References" contains following assertion:

        If the method reference has the form ExpressionName :: NonWildTypeArgumentsopt Identifier or Primary :: NonWildTypeArgumentsopt Identifier, the body of the invocation method has the effect of invoking the compile-time declaration of the method reference, as described in 15.12.4.3, 15.12.4.4, 15.12.4.5.[jsr335-15.28.2-41]

        This assertions refers to chapter "15.12.4.3. Check Accessibility of Type and Method" which specifies that IllegalAccessError should be thrown when invoking either inaccessible method or method from inaccessible class.

        For b100 in such cases InvocationTargetException is thrown instead of IllegalAccessError.

        The minimized test is attached. This test reproduces the problem for invoking private method from package private top class from the same package.

        In order to reproduce the problem following steps should be performed:
        1) compile Test.java from attached archive;
        2) _after_ that, compile bad/MethodSupplier.java from attached archive;
        3) execute Test.main.

        The following output is produced:

        Unexpected exception has been caught
        java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at Test.main(Test.java:22)
        Caused by: java.lang.IncompatibleClassChangeError
        at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:383)
        at MethodInvoker.invoke(Test.java:14)
        ... 5 more
        Caused by: java.lang.IllegalAccessException: member is private: MethodSupplier.m()void/invokeVirtual, from MethodInvoker
        at java.lang.invoke.MemberName.makeAccessException(MemberName.java:744)
        at java.lang.invoke.MethodHandles$Lookup.checkAccess(MethodHandles.java:1129)
        at java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:1092)
        at java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:1203)
        at java.lang.invoke.MethodHandles$Lookup.getDirectMethod(MethodHandles.java:1193)
        at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1285)
        at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:381)
        ... 6 more

        However IlleglaAccessError should have been thrown as it follows from the specification.

        The happens on Windows7x64, b100.

          1. src.zip
            0.7 kB
          2. Test2.java
            2 kB

              drchase David Chase (Inactive)
              grakov Georgiy Rakov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: