--- old/src/java.base/share/classes/java/lang/invoke/MethodHandles.java 2019-07-26 10:36:34.000000000 -0700 +++ new/src/java.base/share/classes/java/lang/invoke/MethodHandles.java 2019-07-26 10:36:34.000000000 -0700 @@ -377,8 +377,8 @@ * {@code C(A*);}{@code (C) aConstructor.newInstance(arg*);} * * - * {@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)} - * ({@code static})?
{@code T m(A*);}{@code (T) aMethod.invoke(thisOrNull, arg*);} + * {@link java.lang.invoke.MethodHandles.Lookup#unreflectSpecial lookup.unreflectSpecial(aMethod,this.class)} + * {@code T m(A*);}{@code (T) aMethod.invoke(this, arg*);} * * * {@link java.lang.invoke.MethodHandles.Lookup#findClass lookup.findClass("C")} @@ -2497,7 +2497,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 2019-07-26 10:36:35.000000000 -0700 +++ new/test/jdk/java/lang/invoke/findSpecial/FindSpecialTest.java 2019-07-26 10:36:35.000000000 -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 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 2019-07-26 10:36:36.000000000 -0700 +++ new/test/jdk/java/lang/invoke/findSpecial/m1/module-info.java 2019-07-26 10:36:35.000000000 -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 2019-07-26 10:36:36.000000000 -0700 +++ new/test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java 2019-07-26 10:36:36.000000000 -0700 @@ -0,0 +1,85 @@ +/* + * 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.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(); + } + + 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 cmp = new MyComparator(); + // invoke successfully + 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 cmp = new MyComparator(); + // invoke successfully + Object o = mh.invoke(cmp); + Object o1 = mh1.invoke(cmp); + } + + static class MyComparator implements Comparator { + public int compare(Object o1, Object o2) { + return 0; + } + + @Override + public Comparator reversed() { + throw new Error("should not reach here"); + } + } +} +