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

Alex Buckley alex.buckley at oracle.com
Wed Aug 12 18:14:26 UTC 2020

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:

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.

class {0} declares no explicit constructors, exposing a default 
constructor to clients of module {2}

(Where {0} is a fully qualified class name)


More information about the compiler-dev mailing list