8242214: NullPointerException in JDK 14 javac compiling a method reference

Ensuring a proper receiver is used to invoke protected method when method reference is unrolled into a lambda.

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2020-06-19 14:26:09 +02:00
parent 5d1255bf52
commit 226e852831
3 changed files with 40 additions and 19 deletions

View File

@ -999,7 +999,10 @@ public class LambdaToMethod extends TreeTranslator {
private JCExpression makeReceiver(VarSymbol rcvr) { private JCExpression makeReceiver(VarSymbol rcvr) {
if (rcvr == null) return null; if (rcvr == null) return null;
JCExpression rcvrExpr = make.Ident(rcvr); JCExpression rcvrExpr = make.Ident(rcvr);
Type rcvrType = tree.ownerAccessible ? tree.sym.enclClass().type : tree.expr.type; boolean protAccess =
isProtectedInSuperClassOfEnclosingClassInOtherPackage(tree.sym, owner);
Type rcvrType = tree.ownerAccessible && !protAccess ? tree.sym.enclClass().type
: tree.expr.type;
if (rcvrType == syms.arrayClass.type) { if (rcvrType == syms.arrayClass.type) {
// Map the receiver type to the actually type, not just "array" // Map the receiver type to the actually type, not just "array"
rcvrType = tree.getQualifierExpression().type; rcvrType = tree.getQualifierExpression().type;
@ -2270,11 +2273,6 @@ public class LambdaToMethod extends TreeTranslator {
types.erasure(owner.enclClass().asType())); types.erasure(owner.enclClass().asType()));
} }
boolean isProtectedInSuperClassOfEnclosingClassInOtherPackage() {
return ((tree.sym.flags() & PROTECTED) != 0 &&
tree.sym.packge() != owner.packge());
}
/** /**
* Erasure destroys the implementation parameter subtype * Erasure destroys the implementation parameter subtype
* relationship for intersection types. * relationship for intersection types.
@ -2311,7 +2309,7 @@ public class LambdaToMethod extends TreeTranslator {
needsVarArgsConversion() || needsVarArgsConversion() ||
isArrayOp() || isArrayOp() ||
(!nestmateLambdas && isPrivateInOtherClass()) || (!nestmateLambdas && isPrivateInOtherClass()) ||
isProtectedInSuperClassOfEnclosingClassInOtherPackage() || isProtectedInSuperClassOfEnclosingClassInOtherPackage(tree.sym, owner) ||
!receiverAccessible() || !receiverAccessible() ||
(tree.getMode() == ReferenceMode.NEW && (tree.getMode() == ReferenceMode.NEW &&
tree.kind != ReferenceKind.ARRAY_CTOR && tree.kind != ReferenceKind.ARRAY_CTOR &&
@ -2386,6 +2384,12 @@ public class LambdaToMethod extends TreeTranslator {
} }
} }
private boolean isProtectedInSuperClassOfEnclosingClassInOtherPackage(Symbol targetReference,
Symbol currentClass) {
return ((targetReference.flags() & PROTECTED) != 0 &&
targetReference.packge() != currentClass.packge());
}
/** /**
* Signature Generation * Signature Generation
*/ */

View File

@ -23,45 +23,58 @@
/* /*
* @test * @test
* @bug 8234729 * @bug 8234729 8242214
* @summary Javac should eagerly change code generation for method references to avert IllegalAccessError in future. * @summary Javac should eagerly change code generation for method references to avert IllegalAccessError in future.
* @compile ProtectedInaccessibleMethodRefTest2.java
* @run main ProtectedInaccessibleMethodRefTest2 * @run main ProtectedInaccessibleMethodRefTest2
*/ */
import pack.I; import pack.I;
import pack.J; import pack.J;
import java.lang.reflect.Method;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.function.Function; import java.util.function.Function;
import java.lang.reflect.Method; import java.util.Arrays;
import java.util.concurrent.Callable; import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
public final class ProtectedInaccessibleMethodRefTest2 extends I { public final class ProtectedInaccessibleMethodRefTest2 extends I {
public static void main(String... args) { public static void main(String... args) {
ProtectedInaccessibleMethodRefTest2 m = new ProtectedInaccessibleMethodRefTest2(); ProtectedInaccessibleMethodRefTest2 m = new ProtectedInaccessibleMethodRefTest2();
m.test(Paths.get("test")); m.test(Paths.get("test"));
// Verify that the method reference has been folded into a lambda. // Verify that the method references have been folded into lambdas:
boolean lambdaFound = false; Set<String> methodNames = new HashSet<>();
for (Method meth : ProtectedInaccessibleMethodRefTest2.class.getDeclaredMethods()) { for (Method meth : ProtectedInaccessibleMethodRefTest2.class.getDeclaredMethods()) {
if (meth.getName().equals("lambda$test$0")) { methodNames.add(meth.getName());
lambdaFound = true;
break;
}
} }
if (!lambdaFound) { List<String> expectedMethods =
Arrays.asList("lambda$test$0", "lambda$test$1", "lambda$test$2");
if (!methodNames.containsAll(expectedMethods)) {
throw new AssertionError("Did not find evidence of new code generation"); throw new AssertionError("Did not find evidence of new code generation");
} }
} }
void test(Path outputDir) { void test(Path outputDir) {
Sub c = new Sub(this::readFile); Sub c1 = new Sub(this::readFile);
c.check(outputDir); c1.check(outputDir);
Sub c2 = new Sub(ProtectedInaccessibleMethodRefTest2::readFile, this);
c2.check(outputDir);
Sub c3 = new Sub(ProtectedInaccessibleMethodRefTest2::readFile2);
c3.check(outputDir);
} }
public class Sub extends J { public class Sub extends J {
Sub(Function<Path,String> fileReader) { Sub(Function<Path,String> fileReader) {
super(fileReader); super(fileReader);
} }
Sub(BiFunction<ProtectedInaccessibleMethodRefTest2, Path,String> fileReader,
ProtectedInaccessibleMethodRefTest2 instance) {
super(p -> fileReader.apply(instance, p));
}
} }
} }

View File

@ -29,4 +29,8 @@ public class I {
protected String readFile(Path file) { protected String readFile(Path file) {
return file.toString(); return file.toString();
} }
protected static String readFile2(Path file) {
return file.toString();
}
} }