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

Srikanth Adayapalam sadayapalam at openjdk.java.net
Thu Jun 24 04:17:32 UTC 2021


On Sun, 6 Jun 2021 02:17:17 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> Please review this PR, currently javac is accepting code like:
> 
> 
> import java.util.function.Supplier;
> 
> public class MethodReferenceInConstructorInvocation {
>     interface Bar {
>         default String getString() { return ""; }
>     }
> 
>     static class Foo implements Bar {
>         public Foo() {  this(Bar.super::getString);  }
>         public Foo(Supplier<String> sString) {}
>     }
> }
> 
> 
> but the spec states in `15.13 Method Reference Expressions`:
> 
> If a method reference expression has the form super :: [TypeArguments] Identifier
> or TypeName . super :: [TypeArguments] Identifier, it is a compile-time error if
> the expression occurs in a static context (§8.1.3).
> 
> and a constructor invocation is a static context. So method references of this form, qualified by `super`, should be rejected by the compiler if they appear in a static context.
> 
> TIA

Do we need to invent a new diagnostics for this ?

I think it is better to check why we fail to issue the existing diagnostic compiler.err.non-static.cant.be.ref for the test case in question.

See that in the following simple test case:


public class X {
    interface I {
        int doit();
    }
    void foo() {
        I i = super::hashCode; // OK.
    }
    static void goo() {
        I i = super::hashCode; // Error
    }


JDK16 does report correctly:

X.java:9: error: non-static variable super cannot be referenced from a static context
        I i = super::hashCode; // Error
              ^
1 error

-------------

PR: https://git.openjdk.java.net/jdk/pull/4376


More information about the compiler-dev mailing list