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

Inconsistency in MethodHandles.Lookup.findVirtual() behaviour

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • 9
    • 9
    • core-libs
    • None

      MethodHandles.Lookup.findVirtual() works fine for class's private method

      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.*;
      import java.lang.invoke.MethodType;

      public class PrivateClassMethodLookupTest{
          public static void main(String[] args) throws Throwable{
           new PrivateClassMethodLookupTest().m();
           MethodHandle mh = MethodHandles.lookup()
                      .findVirtual(PrivateClassMethodLookupTest.class, "m", MethodType.methodType(void.class));
            mh.invoke(new PrivateClassMethodLookupTest());
          }

          private void m() { System.out.println("in m");}
      }
      (working fine)

      while

      MethodHandles.Lookup.findVirtual() throws IllegalAccessException if the method MH is being created for is private and declared in an interface. With in interface methods have full privilege then why can't use findVirtual.

      import java.lang.invoke.MethodHandle;
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodType;

      interface PrivateInterfaceMethodLookupTest {
          default void boot() throws Throwable {
              System.out.println("main");
              m();
              MethodHandles.Lookup lookup = MethodHandles.lookup();
              MethodHandle mh= lookup
                      .findVirtual(PrivateInterfaceMethodLookupTest.class, "m", MethodType.methodType(void.class));
            // mh.invokeExact(this);
          }
          private void m() { System.out.println("in m");
          }
       }
      public class TestIm implements PrivateInterfaceMethodLookupTest{
          
              public static void main(String args[]) throws Throwable
                {
                        TestIm a =new TestIm();
                        a.boot();
                }

      }

      If above behavior is correct then while accessing interface's private method with MethodHandles.lookup().unreflect() should also throw the same exception.


      public interface Unreflect {
                public static void main(String[] args) throws Throwable {
                     Method pm = Unreflect.class.getDeclaredMethod("m");
      // pm.setAccessible(true);
                     MethodHandle pmh = MethodHandles.lookup().unreflect(pm);
      // pmh.invoke(new Unreflect());
               }
               private void m() {
                      System.out.println("in m");
                  }


      }

            srastogi Shilpi Rastogi (Inactive)
            srastogi Shilpi Rastogi (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: