RFR: 8261006: 'super' qualified method references cannot occur in a static context
Vicente Romero
vromero at openjdk.java.net
Sat Jun 26 02:23:26 UTC 2021
On Thu, 24 Jun 2021 05:19:58 GMT, Srikanth Adayapalam <sadayapalam 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
>
> Also this test case:
>
>
> import java.util.function.Supplier;
>
> public class X {
>
> interface Feature {
> default String getString() {
> return "Hello World";
> }
> }
>
> private static class Broken implements Feature {
> private final String sString;
>
> public Broken() {
> this(Feature.super.getString());
> }
>
> public Broken(String sString) {
> this.sString = sString;
> }
> }
> }
>
>
> compiles both with JDK16 and your patch.
>
> Should we actually compile it ???
@sadayapalam, thanks for your comments, I have uploaded another iteration of the patch
-------------
PR: https://git.openjdk.java.net/jdk/pull/4376
More information about the compiler-dev
mailing list