Missing warning when using an instance main method

Alex Buckley alex.buckley at oracle.com
Mon Sep 18 17:29:58 UTC 2023


I'm not suggesting warnings around `main` signatures in the long term. 
Just that, if someone says --enable-preview, they receive warnings about 
any `main` signatures specified to be "candidate methods" in JLS 12.1 
[1]. In other words, I think declaring `void main() {}` === using a 
preview language feature (assuming "Unnamed Classes and Instance main 
Methods" will have a second preview in 22).

Alex

[1] 
https://cr.openjdk.org/~gbierman/jep445/jep445-20230524/specs/unnamed-classes-instance-main-methods-jls.html#jls-12.1.4

On 9/18/2023 10:17 AM, Brian Goetz wrote:
> I think I'm closer to Tagir on this one.  We have never tried to warn 
> the user when they mangle the "main" entry point before; we didn't warn on
> 
>      void main(String[] args)  // no static
> 
> or
> 
>      static void main()        // no args
> 
> or
> 
>      static void main(Sling[] args)  // wrong args type
> 
> 
> These were all valid Java method declarations, but might not do what the 
> author expected, because the launch protocol has always been nearly 
> extralinguistic.  This JEP doesn't really bring the launch protocol that 
> much more into the spotlight.
> 
> I do think it is fair game for an IDE inspection, though (as if Tagir 
> doesn't already have one.)
> 
> 
> On 9/18/2023 12:47 PM, Alex Buckley wrote:
>> Traditionally, `void main()` was not an entrypoint, but under JEP 445, 
>> it is. This is a source compatible change, but not a behaviorally 
>> compatible change. For example, consider a slightly modified example:
>>
>> class MissingWarningsSuper {
>>     public static void main(String[] args) {}
>> }
>>
>> class MissingWarnings extends MissingWarningsSuper {
>>     void main() {}
>> }
>>
>> Traditionally, `java MissingWarnings` would have run the inherited 
>> static main. Under JEP 445, `java MissingWarnings` will run the 
>> declared instance main. (JLS 12.1.4 has a nice discussion of this 
>> policy.)
>>
>> I think a preview warning is appropriate in order to highlight the new 
>> role enjoyed by various methods called "main".
>>
>> Alex
>>
>> On 9/18/2023 7:17 AM, Tagir Valeev wrote:
>>> But this code is a perfectly valid code in non-preview Java version
>>> (unless you try to launch this class, but you don't know this during
>>> the compilation). I don't think there should be a warning.
>>>
>>> With best regards,
>>> Tagir Valeev.
>>>
>>> On Mon, Sep 18, 2023 at 9:37 AM Remi Forax <forax at univ-mlv.fr> wrote:
>>>>
>>>> Hello,
>>>> I believe this code should emit a warning when compiled given it 
>>>> uses an instance main method
>>>>
>>>> class MissingWarnings {
>>>>    void main() {
>>>>    }
>>>> }
>>>>
>>>> But the command
>>>>    javac --enable-preview --source 21 MissingWarnings.java
>>>> does not print a warning.
>>>>
>>>> regards,
>>>> Rémi
> 


More information about the amber-dev mailing list