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) {
if (rcvr == null) return null;
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) {
// Map the receiver type to the actually type, not just "array"
rcvrType = tree.getQualifierExpression().type;
@ -2270,11 +2273,6 @@ public class LambdaToMethod extends TreeTranslator {
types.erasure(owner.enclClass().asType()));
}
boolean isProtectedInSuperClassOfEnclosingClassInOtherPackage() {
return ((tree.sym.flags() & PROTECTED) != 0 &&
tree.sym.packge() != owner.packge());
}
/**
* Erasure destroys the implementation parameter subtype
* relationship for intersection types.
@ -2311,7 +2309,7 @@ public class LambdaToMethod extends TreeTranslator {
needsVarArgsConversion() ||
isArrayOp() ||
(!nestmateLambdas && isPrivateInOtherClass()) ||
isProtectedInSuperClassOfEnclosingClassInOtherPackage() ||
isProtectedInSuperClassOfEnclosingClassInOtherPackage(tree.sym, owner) ||
!receiverAccessible() ||
(tree.getMode() == ReferenceMode.NEW &&
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
*/

View File

@ -23,45 +23,58 @@
/*
* @test
* @bug 8234729
* @bug 8234729 8242214
* @summary Javac should eagerly change code generation for method references to avert IllegalAccessError in future.
* @compile ProtectedInaccessibleMethodRefTest2.java
* @run main ProtectedInaccessibleMethodRefTest2
*/
import pack.I;
import pack.J;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Function;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
public final class ProtectedInaccessibleMethodRefTest2 extends I {
public static void main(String... args) {
ProtectedInaccessibleMethodRefTest2 m = new ProtectedInaccessibleMethodRefTest2();
m.test(Paths.get("test"));
// Verify that the method reference has been folded into a lambda.
boolean lambdaFound = false;
// Verify that the method references have been folded into lambdas:
Set<String> methodNames = new HashSet<>();
for (Method meth : ProtectedInaccessibleMethodRefTest2.class.getDeclaredMethods()) {
if (meth.getName().equals("lambda$test$0")) {
lambdaFound = true;
break;
}
methodNames.add(meth.getName());
}
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");
}
}
void test(Path outputDir) {
Sub c = new Sub(this::readFile);
c.check(outputDir);
Sub c1 = new Sub(this::readFile);
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 {
Sub(Function<Path,String> 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) {
return file.toString();
}
protected static String readFile2(Path file) {
return file.toString();
}
}