8318160: javac does not reject private method reference with type-variable receiver
Reviewed-by: mcimadamore
This commit is contained in:
parent
54c613acd7
commit
e2720987b9
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
34
test/langtools/tools/javac/lambda/methodReference/PrivateMethodReferenceWithTypeVarTest.java
Normal file
34
test/langtools/tools/javac/lambda/methodReference/PrivateMethodReferenceWithTypeVarTest.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
4
test/langtools/tools/javac/lambda/methodReference/PrivateMethodReferenceWithTypeVarTest.out
Normal file
4
test/langtools/tools/javac/lambda/methodReference/PrivateMethodReferenceWithTypeVarTest.out
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user