Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

Commit 0f54707

Browse files
committedJul 14, 2021
8270056: Generated lambda class can not access protected static method of target class
Reviewed-by: mchung Backport-of: 07e9052
1 parent 8583aab commit 0f54707

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

‎src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
187187
// to invoke directly. (javac prefers to avoid this situation by
188188
// generating bridges in the target class)
189189
useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
190-
!VerifyAccess.isSamePackage(implClass, implInfo.getDeclaringClass())) ||
190+
!VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
191191
implKind == H_INVOKESPECIAL;
192192
cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
193193
int parameterCount = factoryType.parameterCount();
@@ -564,7 +564,10 @@ void generate(MethodType methodType) {
564564
convertArgumentTypes(methodType);
565565

566566
if (useImplMethodHandle) {
567-
MethodType mtype = implInfo.getMethodType().insertParameterTypes(0, implClass);
567+
MethodType mtype = implInfo.getMethodType();
568+
if (implKind != MethodHandleInfo.REF_invokeStatic) {
569+
mtype = mtype.insertParameterTypes(0, implClass);
570+
}
568571
visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
569572
"invokeExact", mtype.descriptorString(), false);
570573
} else {

‎test/jdk/java/lang/invoke/lambda/superProtectedMethod/SuperMethodTest.java ‎test/jdk/java/lang/invoke/lambda/superProtectedMethod/ProtectedMethodInOtherPackage.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
/*
2525
* @test
26-
* @bug 8227415 8254975
27-
* @run testng/othervm p.SuperMethodTest
26+
* @bug 8227415 8254975 8270056
27+
* @run testng/othervm p.ProtectedMethodInOtherPackage
2828
* @summary method reference to a protected method inherited from its
2929
* superclass in a different runtime package where
3030
* lambda proxy class has no access to it.
@@ -50,7 +50,7 @@
5050
import org.testng.annotations.Test;
5151
import static org.testng.Assert.*;
5252

53-
public class SuperMethodTest {
53+
public class ProtectedMethodInOtherPackage {
5454
@Test
5555
public static void remotePackageSameLoader() {
5656
Sub_I sub = new Sub_I();
@@ -101,6 +101,30 @@ public static void splitPackage() throws Throwable {
101101
((Runnable) get.invoke(b)).run();
102102
}
103103

104+
@Test
105+
public static void protectedStaticMethodInSplitPackage() throws Throwable {
106+
ClassLoader parent = new Loader("loader-A1", null, A1.class);
107+
ClassLoader loader = new Loader("loader-B1", parent, B1.class);
108+
Class<?> aClass1 = Class.forName(A1.class.getName(), false, loader);
109+
Class<?> bClass1 = Class.forName(B1.class.getName(), false, loader);
110+
assertTrue(aClass1.getClassLoader() == parent);
111+
assertTrue(bClass1.getClassLoader() == loader);
112+
assertEquals(aClass1.getPackageName(), bClass1.getPackageName());
113+
114+
// verify subclass can access a static protected method inherited from
115+
// its superclass in a split package
116+
MethodHandle test = MethodHandles.lookup()
117+
.findStatic(bClass1, "test", MethodType.methodType(void.class));
118+
test.invoke();
119+
120+
// verify lambda can access a static protected method inherited from
121+
// a superclass of the host class where the superclass is in
122+
// a split package (not the same runtime package as the host class)
123+
MethodHandle get = MethodHandles.lookup()
124+
.findStatic(bClass1, "get", MethodType.methodType(Runnable.class));
125+
((Runnable) get.invoke()).run();
126+
}
127+
104128
static class Loader extends URLClassLoader {
105129
static final Path CLASSES_DIR = Paths.get(System.getProperty("test.class.path"));
106130
private final Class<?> c;
@@ -138,4 +162,17 @@ public void test() {
138162
func();
139163
}
140164
}
165+
166+
public static class A1 {
167+
protected static void func() { }
168+
}
169+
170+
public static class B1 extends A1 {
171+
public static Runnable get() {
172+
return A1::func;
173+
}
174+
public static void test() {
175+
func();
176+
}
177+
}
141178
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jul 14, 2021

@openjdk-notifier[bot]
This repository has been archived.