JDK 16 RFR of JDK-8071961: Add javac lint warning when a default constructor is created

Jonathan Gibbons jonathan.gibbons at oracle.com
Wed Aug 12 18:23:34 UTC 2020


Exposition about why default constructors are not encouraged should be 
put in the man page, not in the command line help, and should be more 
informative than saying that they "may not be suitable for clients".

-- Jon

On 8/12/20 11:14 AM, Alex Buckley wrote:
> On 8/12/2020 10:01 AM, Maurizio Cimadamore wrote:
>> On 31/07/2020 17:23, Alex Buckley wrote:
>>> On 7/30/2020 10:39 PM, Joe Darcy wrote:
>>>> PS In the next iteration of the webrev,
>>>>
>>>>      http://cr.openjdk.java.net/~darcy/8071961.6
>>>>
>>>> the messages have been updated to
>>>>
>>>>   185 javac.opt.Xlint.desc.missing-declared-ctor=\
>>>>   186     Warn about missing declared constructors in public 
>>>> classes in exported packages.
>>>>
>>>> 1763 # 0: symbol, 1: symbol, 2: symbol
>>>> 1764 compiler.warn.missing-declared-ctor=\
>>>> 1765     class {0} in exported package {1} of module {2} does not 
>>>> declare any explicit constructors, exposing a default constructor 
>>>> to clients
>>>
>>> These messages look very good.
>>
>> I'm afraid I disagree on the second message :-(
>>
>> I'm having issues to parse it - mostly for two reasons:
>>
>> 1) the ceremony of class/package/module takes the first half of the 
>> message, w/o significantly add info (provided that javac would also 
>> have generated the file name somewhere in the diagnostic)
>> 2) the use of "ing" (as in "exposing") seems confusing, and leaves 
>> the reader wondering as to what exactly the problem with the code is
>>
>> I'd go for something more direct, like:
>>
>> class {0} exposes a default constructor to clients of module {2}
>
> I agree the ceremony can be cut down, but I don't agree that 
> "exposing" is confusing.
>
> IMO, the connection between "missing {declared,explicit} ctor" and "a 
> default ctor is exposed" should be spelled out in the warning text.
>
> Continuing on: if the exposure (or, less pejoratively, the existence) 
> of a default ctor is not widely understood to be bad, then we should 
> explain that it is bad in the option's documentation.
>
> So, a further suggestion:
>
> javac.opt.Xlint.desc.missing-explicit-ctor=\
> Warn about omission of explicit constructors in public classes of 
> exported packages. Omitting an explicit constructor causes javac to 
> generate a default constructor, which may not be suitable for clients.
>
> compiler.warn.missing-explicit-ctor=\
> class {0} declares no explicit constructors, exposing a default 
> constructor to clients of module {2}
>
> (Where {0} is a fully qualified class name)
>
> Alex


More information about the compiler-dev mailing list