8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
authormchung
Tue, 13 Aug 2019 15:49:11 -0700
changeset 57735 7ba5e49258de
parent 57734 18f4d3d46d54
child 57736 7f75db20c209
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works 8209078: Unable to call default method from interface in another module from named module Reviewed-by: dfuchs, plevart
src/java.base/share/classes/java/lang/invoke/MethodHandles.java
test/jdk/java/lang/invoke/findSpecial/FindSpecialTest.java
test/jdk/java/lang/invoke/findSpecial/m1/module-info.java
test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java
--- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Aug 13 16:13:22 2019 -0400
+++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Aug 13 15:49:11 2019 -0700
@@ -334,7 +334,7 @@
      * </tr>
      * <tr>
      *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</th>
-     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td>
+     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code (FT) C.f;}</td>
      * </tr>
      * <tr>
      *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</th>
@@ -377,8 +377,8 @@
      *     <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td>
      * </tr>
      * <tr>
-     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</th>
-     *     <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
+     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectSpecial lookup.unreflectSpecial(aMethod,this.class)}</th>
+     *     <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
      * </tr>
      * <tr>
      *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findClass lookup.findClass("C")}</th>
@@ -403,7 +403,7 @@
      * stands for a null reference if the accessed method or field is static,
      * and {@code this} otherwise.
      * The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
-     * for reflective objects corresponding to the given members.
+     * for reflective objects corresponding to the given members declared in type {@code C}.
      * <p>
      * The bytecode behavior for a {@code findClass} operation is a load of a constant class,
      * as if by {@code ldc CONSTANT_Class}.
@@ -2504,7 +2504,7 @@
          * @throws NullPointerException if any argument is null
          */
         public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
-            checkSpecialCaller(specialCaller, null);
+            checkSpecialCaller(specialCaller, m.getDeclaringClass());
             Lookup specialLookup = this.in(specialCaller);
             MemberName method = new MemberName(m, true);
             assert(method.isMethod());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/invoke/findSpecial/FindSpecialTest.java	Tue Aug 13 15:49:11 2019 -0700
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8209005 8209078
+ * @library /test/lib
+ * @build m1/* FindSpecialTest
+ * @run testng/othervm FindSpecialTest
+ * @summary Test findSpecial and unreflectSpecial of the declaring class
+ *          of the method and the special caller are not in the same module
+ *          as the lookup class.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.process.ProcessTools;
+
+import org.testng.annotations.Test;
+
+public class FindSpecialTest {
+    static final String JAVA_LAUNCHER = JDKToolFinder.getJDKTool("java");
+    static final String TEST_CLASSES = System.getProperty("test.classes", ".");
+    static final String TEST_CLASS_PATH = System.getProperty("test.class.path");
+    static final String TEST_MAIN_CLASS = "test.FindSpecial";
+    static final String TEST_MODULE = "m1";
+
+    /*
+     * Run test.FindSpecial in unnamed module
+     */
+    @Test
+    public static void callerInUnnamedModule() throws Throwable {
+        Path m1 = Paths.get(TEST_CLASSES, "modules", TEST_MODULE);
+        if (Files.notExists(m1)) {
+            throw new Error(m1 + " not exist");
+        }
+        String classpath = m1.toString() + File.pathSeparator + TEST_CLASS_PATH;
+        ProcessTools.executeCommand(JAVA_LAUNCHER, "-cp", classpath, TEST_MAIN_CLASS)
+                    .shouldHaveExitValue(0);
+    }
+
+    /*
+     * Run test.FindSpecial in a named module
+     */
+    @Test
+    public static void callerInNamedModule() throws Throwable {
+        Path modules = Paths.get(TEST_CLASSES, "modules");
+        if (Files.notExists(modules)) {
+            throw new Error(modules + " not exist");
+        }
+        ProcessTools.executeCommand(JAVA_LAUNCHER,
+                                    "-cp", TEST_CLASS_PATH,
+                                    "-p", modules.toString(),
+                                    "-m", TEST_MODULE + "/" + TEST_MAIN_CLASS)
+                    .shouldHaveExitValue(0);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/invoke/findSpecial/m1/module-info.java	Tue Aug 13 15:49:11 2019 -0700
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+module m1 {
+    exports test;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java	Tue Aug 13 15:49:11 2019 -0700
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package test;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Comparator;
+
+/*
+ * java.util.Comparator is in java.base.  MyComparator is a Comparator
+ * subclass and it's in a different module.
+ *
+ * Test findSpecial and unreflectSpecial with Comparator and MyComparator
+ * as the special caller.
+ */
+public class FindSpecial {
+    private static final Lookup LOOKUP = MethodHandles.lookup();
+
+    public static void main(String... args) throws Throwable {
+        findSpecialTest();
+        unreflectSpecialTest();
+        reflectMethodInvoke();
+    }
+
+    static void findSpecialTest() throws Throwable {
+        Method m = Comparator.class.getMethod("reversed");
+        MethodType mt = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
+        // refc and specialCaller are both in java.base
+        MethodHandle mh = LOOKUP.findSpecial(Comparator.class, m.getName(), mt, Comparator.class);
+        // refc in java.base, specialCaller in this module
+        MethodHandle mh1 = LOOKUP.findSpecial(m.getDeclaringClass(), m.getName(), mt,
+                                                MyComparator.class);
+        Comparator<Object> cmp = new MyComparator();
+        // invokespecial to invoke Comparator::reversed.
+        Object o = mh.invoke(cmp);
+        Object o1 = mh1.invoke(cmp);
+    }
+
+    static void unreflectSpecialTest() throws Throwable {
+        Method m = Comparator.class.getMethod("reversed");
+        // refc and specialCaller are both in java.base
+        MethodHandle mh = LOOKUP.unreflectSpecial(m, Comparator.class);
+        // refc in java.base, specialCaller in this module
+        MethodHandle mh1 = LOOKUP.unreflectSpecial(m, MyComparator.class);
+        Comparator<Object> cmp = new MyComparator();
+        // invokespecial to invoke Comparator::reversed.
+        Object o = mh.invoke(cmp);
+        Object o1 = mh1.invoke(cmp);
+    }
+
+    static void reflectMethodInvoke() throws Throwable {
+        Method m = Comparator.class.getMethod("reversed");
+        try {
+            // invokevirtual dispatch
+            Object o = m.invoke(new MyComparator());
+            throw new RuntimeException("should throw an exception");
+        } catch (InvocationTargetException e) {
+            if (!(e.getCause() instanceof Error &&
+                  e.getCause().getMessage().equals("should not reach here"))) {
+                throw e.getCause();
+            }
+        }
+    }
+
+    static class MyComparator implements Comparator<Object> {
+        public int compare(Object o1, Object o2) {
+            return 0;
+        }
+
+        @Override
+        public Comparator<Object> reversed() {
+            throw new Error("should not reach here");
+        }
+    }
+}
+