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

MethodHandles.Lookup.findVirtual() Javadoc fails to consider private interface methods

XMLWordPrintable

      MethodHandles.Lookup.findVirtual() throws IllegalAccessException if the method MH is being created for is private and declared in an interface.

      Example:
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodType;

      public interface PrivateInterfaceMethodLookupTest {
          public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
              MethodHandles.lookup()
                      .findVirtual(PrivateInterfaceMethodLookupTest.class, "m", MethodType.methodType(void.class));
          }

          private void m() {}
      }

      Output:
      Exception in thread "main" java.lang.IllegalAccessException: no such method: PrivateInterfaceMethodLookupTest.m()void/invokeInterface
              at java.lang.invoke.MemberName.makeAccessException(MemberName.java:869)
              at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)
              at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1382)
              at java.lang.invoke.MethodHandles$Lookup.findVirtual(MethodHandles.java:858)
              at PrivateInterfaceMethodLookupTest.main(PrivateInterfaceMethodLookupTest.java:7)
      Caused by: java.lang.IncompatibleClassChangeError: private interface method requires invokespecial, not invokeinterface: method PrivateInterfaceMethodLookupTest.m()V
              at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
              at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962)
              at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:987)
              ... 3 more

            srastogi Shilpi Rastogi (Inactive)
            slukyanov Stanislav Lukyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: