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

Joe Darcy joe.darcy at oracle.com
Wed Aug 12 18:44:45 UTC 2020


Hello,

The command-line help is a brief summary so the current proposed text of

     Warn about missing declared constructors in public classes in 
exported packages.

is more in keeping with javac conventions; expository discussion and 
explanation should go elsewhere. For example, the command-line help for 
the raw types warning is

     Warn about use of raw types.

There is no explanation or reference to what a raw type is, why it's 
bad, how to avoid it, etc.

For the warning proposed to be issued for the classes:

+# 0: symbol, 1: symbol, 2: symbol
+compiler.warn.missing-declared-ctor=\
+    class {0} in exported package {1} of module {2} does not declare 
any explicit constructors, exposing a default constructor to clients

the wording is meant to imply the conditions under which the warning is 
issued, namely that the class has to be in an exported package of a 
module (among other conditions). So besides adding a constructor, one 
could address the warning by moving the class to a non-exported package. 
As discussed at points earlier in thread, since the warning is 
fundamentally about the *absence* of code, I think it is clearer to 
explicitly including a statement that "you are getting this warning 
because there isn't an explicit constructor" rather than relying only on 
the "default constructor" term to convey that message.

-Joe

On 8/12/2020 11:23 AM, Jonathan Gibbons wrote:
> 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