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

IncompatibleClassChangeError when creating method reference to outer class private method

XMLWordPrintable

    • b100

      Following code causes IncompatibleClassChangeError to be thrown:

      interface MyFunctionalInterface {
          void invokeMethodReference(int a);
      }

      public class Test {
          private void m(int a) {
              System.out.println(a);
          }
          void test() {
              class Inner {
                  MyFunctionalInterface createMethodReference() {
                      m(12);
                      return Test.this::m;
                  }
              }
              MyFunctionalInterface fi = new Inner().createMethodReference();
              fi.invokeMethodReference(1);
          }

          public static void main(String argv[]) {
              new Test().test();
          }
      }

      The output is:
      12
      Exception in thread "main" java.lang.IncompatibleClassChangeError
      at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:384)
      at Test$1Inner.createMethodReference(Test.java:13)
      at Test.test(Test.java:16)
      at Test.main(Test.java:21)
      Caused by: java.lang.IllegalAccessException: member is private: Test.m(int)void/invokeSpecial, from Test$1Inner
      at java.lang.invoke.MemberName.makeAccessException(MemberName.java:742)
      at java.lang.invoke.MethodHandles$Lookup.checkAccess(MethodHandles.java:1173)
      at java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:1136)
      at java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:1247)
      at java.lang.invoke.MethodHandles$Lookup.getDirectMethod(MethodHandles.java:1237)
      at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1329)
      at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:382)
      ... 3 more

      Method m is successfully invoked while creating method reference to it fails.

      This has occurred on lambda b100(x64), Windows7x64.

            rfield Robert Field (Inactive)
            grakov Georgiy Rakov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: