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

Unable to call default method from interface in an other module from named module

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Unable to call default method from interface in an other module from named module.

      Using Lookup.findSpecial it is possible to get a MethodHandle for the default method of an interface.
      This only works if the code is in an unnamed module.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to find a MethodHandle for a default method of an interface in an other module from a named module.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A MethodHandle for the default method is returned.
      ACTUAL -
      An exception is thrown.

      ---------- BEGIN SOURCE ----------
      package bug;

      import java.lang.invoke.MethodHandle;
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodType;
      import java.lang.invoke.MethodHandles.Lookup;
      import java.lang.reflect.Method;
      import java.util.Comparator;

      public class ModuleFindDefault {
      private static final Lookup LOOKUP = MethodHandles.lookup();

      public static void main(String[] args) throws Throwable {
      Method method = Comparator.class.getMethod("reversed");
      Comparator<Object> cmp = new TestInterf();

      MethodHandle mhFind = findSpecial(method);
      Object findRev = mhFind.invoke(cmp);
      System.out.println(findRev);
      }

      static MethodHandle findSpecial(Method m) throws ReflectiveOperationException {
      return LOOKUP.findSpecial(m.getDeclaringClass(), m.getName(),
      MethodType.methodType(m.getReturnType(), m.getParameterTypes()),
      m.getDeclaringClass());
      }

      static class TestInterf implements Comparator<Object> {

      public int compare(Object o1, Object o2) {
      return 0;
      }

      @Override
      public Comparator<Object> reversed() {
      System.out.println("ModuleFindDefault.TestInterf.reversed()");
      return this;
      }

      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Get somehow a private lookup in an unnamed module:

      Create a ClassLoader, define a class, get the Lookup from this class?

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              mchung Mandy Chung
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: