[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-compiler-dev
Subject:    Re: Instance qualifier for static access warning and anonymous classes
From:       Maurizio Cimadamore <maurizio.cimadamore () oracle ! com>
Date:       2022-04-29 15:42:26
Message-ID: 09f94ab8-be38-90ee-e528-12d473be79db () oracle ! com
[Download RAW message or body]

Filed:
https://bugs.openjdk.java.net/browse/JDK-8285935

Thanks
Maurizio

On 29/04/2022 16:37, Tagir Valeev wrote:
> We in JetBrains, decided to remove a warning in this case, as there's
> no obvious fix and it's not a suspicious or buggy code, it's likely to
> be intended in a way it is.
>
> With best regards,
> Tagir Valeev.
>
> On Fri, Apr 29, 2022 at 5:25 PM Maurizio Cimadamore
> <maurizio.cimadamore@oracle.com> wrote:
>> Well spotted - this seems like a dark corner.
>>
>> IMHO there are two possible interpretation, which depend on where we
>> want to go with the warning:
>>
>> 1. static methods accessed through instance qualifiers are Bad, and we
>> should try to wean developer off them over time (and align this with
>> interface static methods). In this case the warning should stay (but of
>> course the message needs to be fixed :-) ).
>> 2. static methods accessed through instance qualifiers are a wart, but
>> something we should be prepared to live with forever - best thing we can
>> do in this space is to issue some "style" warnings, but nothing more
>> will ever happen. In this case, for this example we should turn off the
>> warning altogether.
>>
>> Maurizio
>>
>> On 29/04/2022 15:54, Tagir Valeev wrote:
>>> Hello!
>>>
>>> Consider the following code:
>>>
>>> public class Demo {
>>>       public static void main(String[] args) {
>>>           var obj = new Object() {
>>>               static void foo() {
>>>                   System.out.println("Static method of anonymous class");
>>>               }
>>>           };
>>>           obj.foo(); // Cannot replace instance qualifier with class reference
>>>       }
>>> }
>>>
>>> With -Xlint:static, javac reports the following warning:
>>>
>>> Demo.java:8: warning: [static] static method should be qualified by
>>> type name, <anonymous Demo$1>, instead of by an expression
>>>           obj.foo(); // Cannot replace instance qualifier with class reference
>>>              ^
>>> 1 warning
>>>
>>> It's questionable whether this warning should be reported at all, as
>>> it's impossible to call the static method of anonymous class with
>>> class name qualifier. But in any case, suggesting to use `<anonymous
>>> Demo$1>` as a qualifier is invalid.
>>>
>>> With best regards,
>>> Tagir Valeev.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic