8261006: 'super' qualified method references cannot occur in a static context

Reviewed-by: sadayapalam
This commit is contained in:
Vicente Romero 2021-07-15 15:33:23 +00:00
parent 99d7f9a772
commit c962e6ec0b
3 changed files with 36 additions and 3 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/lambda/methodReference

@ -4339,11 +4339,14 @@ public class Attr extends JCTree.Visitor {
}
if (isType(sitesym)) {
if (sym.name == names._this) {
if (sym.name == names._this || sym.name == names._super) {
// If `C' is the currently compiled class, check that
// C.this' does not appear in a call to a super(...)
// `C.this' does not appear in an explicit call to a constructor
// also make sure that `super` is not used in constructor invocations
if (env.info.isSelfCall &&
site.tsym == env.enclClass.sym) {
((sym.name == names._this &&
site.tsym == env.enclClass.sym) ||
sym.name == names._super)) {
chk.earlyRefError(tree.pos(), sym);
}
} else {

@ -0,0 +1,27 @@
/*
* @test /nodynamiccopyright/
* @bug 8261006
* @summary 'super' qualified method references cannot occur in a static context
* @compile/fail/ref=MethodReferenceInConstructorInvocation.out -XDrawDiagnostics MethodReferenceInConstructorInvocation.java
*/
import java.util.function.Supplier;
public class MethodReferenceInConstructorInvocation {
interface Bar {
default String getString() {
return "";
}
}
static class Foo implements Bar {
Foo() {
this(Bar.super::getString);
}
Foo(Supplier<String> sString) {}
Foo(int i) { this(Bar.super.getString()); }
Foo(String s) {}
}
}

@ -0,0 +1,3 @@
MethodReferenceInConstructorInvocation.java:20:21: compiler.err.cant.ref.before.ctor.called: super
MethodReferenceInConstructorInvocation.java:24:30: compiler.err.cant.ref.before.ctor.called: super
2 errors