8318160: javac does not reject private method reference with type-variable receiver

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2023-10-24 14:45:10 +00:00
parent 54c613acd7
commit e2720987b9
3 changed files with 50 additions and 0 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/lambda/methodReference

@ -3605,6 +3605,18 @@ public class Resolve {
ReferenceKind.BOUND;
}
}
@Override
Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
if (originalSite.hasTag(TYPEVAR) && sym.kind == MTH) {
sym = (sym.flags() & Flags.PRIVATE) != 0 ?
new AccessError(env, site, sym) :
sym;
return accessBase(sym, pos, location, originalSite, name, true);
} else {
return super.access(env, pos, location, sym);
}
}
}
/**

@ -0,0 +1,34 @@
/*
* @test /nodynamiccopyright/
* @bug 8318160
* @summary javac does not reject private method reference with type-variable receiver
* @compile/fail/ref=PrivateMethodReferenceWithTypeVarTest.out -XDrawDiagnostics PrivateMethodReferenceWithTypeVarTest.java
*/
import java.util.function.*;
class PrivateMethodReferenceWithTypeVarTest {
class Foo<X> {
X get() { return null; }
}
private String asString() {
return "bar";
}
private String asString2(Object o) {
return "bar";
}
static <T extends PrivateMethodReferenceWithTypeVarTest> Function<T, String> m1() {
return T::asString;
}
static <T extends PrivateMethodReferenceWithTypeVarTest> Function<T, String> m2(T t) {
return t::asString2;
}
static Function<?, String> m2(Foo<? extends PrivateMethodReferenceWithTypeVarTest> foo) {
return foo.get()::asString2;
}
}

@ -0,0 +1,4 @@
PrivateMethodReferenceWithTypeVarTest.java:24:16: compiler.err.report.access: asString(), private, PrivateMethodReferenceWithTypeVarTest
PrivateMethodReferenceWithTypeVarTest.java:28:16: compiler.err.report.access: asString2(java.lang.Object), private, PrivateMethodReferenceWithTypeVarTest
PrivateMethodReferenceWithTypeVarTest.java:32:16: compiler.err.report.access: asString2(java.lang.Object), private, PrivateMethodReferenceWithTypeVarTest
3 errors