mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
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:
parent
5d1255bf52
commit
226e852831
3 changed files with 40 additions and 19 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue