mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8270056: Generated lambda class can not access protected static method of target class
Co-authored-by: NekoCaffeine <nekocaffeine@qq.com> Reviewed-by: mchung
This commit is contained in:
parent
afe957cd97
commit
07e9052457
2 changed files with 45 additions and 5 deletions
|
@ -187,7 +187,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
||||||
// to invoke directly. (javac prefers to avoid this situation by
|
// to invoke directly. (javac prefers to avoid this situation by
|
||||||
// generating bridges in the target class)
|
// generating bridges in the target class)
|
||||||
useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
|
useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
|
||||||
!VerifyAccess.isSamePackage(implClass, implInfo.getDeclaringClass())) ||
|
!VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
|
||||||
implKind == H_INVOKESPECIAL;
|
implKind == H_INVOKESPECIAL;
|
||||||
cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||||
int parameterCount = factoryType.parameterCount();
|
int parameterCount = factoryType.parameterCount();
|
||||||
|
@ -564,7 +564,10 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
||||||
convertArgumentTypes(methodType);
|
convertArgumentTypes(methodType);
|
||||||
|
|
||||||
if (useImplMethodHandle) {
|
if (useImplMethodHandle) {
|
||||||
MethodType mtype = implInfo.getMethodType().insertParameterTypes(0, implClass);
|
MethodType mtype = implInfo.getMethodType();
|
||||||
|
if (implKind != MethodHandleInfo.REF_invokeStatic) {
|
||||||
|
mtype = mtype.insertParameterTypes(0, implClass);
|
||||||
|
}
|
||||||
visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
|
visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
|
||||||
"invokeExact", mtype.descriptorString(), false);
|
"invokeExact", mtype.descriptorString(), false);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8227415 8254975
|
* @bug 8227415 8254975 8270056
|
||||||
* @run testng/othervm p.SuperMethodTest
|
* @run testng/othervm p.ProtectedMethodInOtherPackage
|
||||||
* @summary method reference to a protected method inherited from its
|
* @summary method reference to a protected method inherited from its
|
||||||
* superclass in a different runtime package where
|
* superclass in a different runtime package where
|
||||||
* lambda proxy class has no access to it.
|
* lambda proxy class has no access to it.
|
||||||
|
@ -50,7 +50,7 @@ import java.util.function.Function;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
public class SuperMethodTest {
|
public class ProtectedMethodInOtherPackage {
|
||||||
@Test
|
@Test
|
||||||
public static void remotePackageSameLoader() {
|
public static void remotePackageSameLoader() {
|
||||||
Sub_I sub = new Sub_I();
|
Sub_I sub = new Sub_I();
|
||||||
|
@ -101,6 +101,30 @@ public class SuperMethodTest {
|
||||||
((Runnable) get.invoke(b)).run();
|
((Runnable) get.invoke(b)).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public static void protectedStaticMethodInSplitPackage() throws Throwable {
|
||||||
|
ClassLoader parent = new Loader("loader-A1", null, A1.class);
|
||||||
|
ClassLoader loader = new Loader("loader-B1", parent, B1.class);
|
||||||
|
Class<?> aClass1 = Class.forName(A1.class.getName(), false, loader);
|
||||||
|
Class<?> bClass1 = Class.forName(B1.class.getName(), false, loader);
|
||||||
|
assertTrue(aClass1.getClassLoader() == parent);
|
||||||
|
assertTrue(bClass1.getClassLoader() == loader);
|
||||||
|
assertEquals(aClass1.getPackageName(), bClass1.getPackageName());
|
||||||
|
|
||||||
|
// verify subclass can access a static protected method inherited from
|
||||||
|
// its superclass in a split package
|
||||||
|
MethodHandle test = MethodHandles.lookup()
|
||||||
|
.findStatic(bClass1, "test", MethodType.methodType(void.class));
|
||||||
|
test.invoke();
|
||||||
|
|
||||||
|
// verify lambda can access a static protected method inherited from
|
||||||
|
// a superclass of the host class where the superclass is in
|
||||||
|
// a split package (not the same runtime package as the host class)
|
||||||
|
MethodHandle get = MethodHandles.lookup()
|
||||||
|
.findStatic(bClass1, "get", MethodType.methodType(Runnable.class));
|
||||||
|
((Runnable) get.invoke()).run();
|
||||||
|
}
|
||||||
|
|
||||||
static class Loader extends URLClassLoader {
|
static class Loader extends URLClassLoader {
|
||||||
static final Path CLASSES_DIR = Paths.get(System.getProperty("test.class.path"));
|
static final Path CLASSES_DIR = Paths.get(System.getProperty("test.class.path"));
|
||||||
private final Class<?> c;
|
private final Class<?> c;
|
||||||
|
@ -138,4 +162,17 @@ public class SuperMethodTest {
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class A1 {
|
||||||
|
protected static void func() { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class B1 extends A1 {
|
||||||
|
public static Runnable get() {
|
||||||
|
return A1::func;
|
||||||
|
}
|
||||||
|
public static void test() {
|
||||||
|
func();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue