mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
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
This commit is contained in:
parent
2207d884d5
commit
182c19e8c4
4 changed files with 213 additions and 5 deletions
|
@ -334,7 +334,7 @@ public class MethodHandles {
|
||||||
* </tr>
|
* </tr>
|
||||||
* <tr>
|
* <tr>
|
||||||
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</th>
|
* <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>
|
||||||
* <tr>
|
* <tr>
|
||||||
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</th>
|
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</th>
|
||||||
|
@ -377,8 +377,8 @@ public class MethodHandles {
|
||||||
* <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td>
|
* <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td>
|
||||||
* </tr>
|
* </tr>
|
||||||
* <tr>
|
* <tr>
|
||||||
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</th>
|
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectSpecial lookup.unreflectSpecial(aMethod,this.class)}</th>
|
||||||
* <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
|
* <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
|
||||||
* </tr>
|
* </tr>
|
||||||
* <tr>
|
* <tr>
|
||||||
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findClass lookup.findClass("C")}</th>
|
* <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findClass lookup.findClass("C")}</th>
|
||||||
|
@ -403,7 +403,7 @@ public class MethodHandles {
|
||||||
* stands for a null reference if the accessed method or field is static,
|
* stands for a null reference if the accessed method or field is static,
|
||||||
* and {@code this} otherwise.
|
* and {@code this} otherwise.
|
||||||
* The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
|
* 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>
|
* <p>
|
||||||
* The bytecode behavior for a {@code findClass} operation is a load of a constant class,
|
* The bytecode behavior for a {@code findClass} operation is a load of a constant class,
|
||||||
* as if by {@code ldc CONSTANT_Class}.
|
* as if by {@code ldc CONSTANT_Class}.
|
||||||
|
@ -2504,7 +2504,7 @@ return mh1;
|
||||||
* @throws NullPointerException if any argument is null
|
* @throws NullPointerException if any argument is null
|
||||||
*/
|
*/
|
||||||
public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
|
public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
|
||||||
checkSpecialCaller(specialCaller, null);
|
checkSpecialCaller(specialCaller, m.getDeclaringClass());
|
||||||
Lookup specialLookup = this.in(specialCaller);
|
Lookup specialLookup = this.in(specialCaller);
|
||||||
MemberName method = new MemberName(m, true);
|
MemberName method = new MemberName(m, true);
|
||||||
assert(method.isMethod());
|
assert(method.isMethod());
|
||||||
|
|
81
test/jdk/java/lang/invoke/findSpecial/FindSpecialTest.java
Normal file
81
test/jdk/java/lang/invoke/findSpecial/FindSpecialTest.java
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
26
test/jdk/java/lang/invoke/findSpecial/m1/module-info.java
Normal file
26
test/jdk/java/lang/invoke/findSpecial/m1/module-info.java
Normal file
|
@ -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;
|
||||||
|
}
|
101
test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java
Normal file
101
test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java
Normal file
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue