JDK-8071961 : Add javac lint warning when a default constructor is created
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-01-29
  • Updated: 2024-11-20
  • Resolved: 2020-08-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 16
16 b12Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
JLS section 8.8.9 documents that if a class does not declare at least one constructor, the compiler will generate a constructor by default. While this policy may be convenient, for formal classes it is a poor programming practice, if for no other reason that the default constructor will have no javadoc.

Use of a default constructor may be a reasonable javac lint warning.
Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/f74d10596242 User: darcy Date: 2020-08-18 01:58:36 +0000
18-08-2020

Review thread: https://mail.openjdk.java.net/pipermail/compiler-dev/2020-July/014782.html
29-07-2020

An additional bit of logic to add to cut down on benign cases is to screen out public classes that are nested classes within a non-public class.
06-09-2019

Basic prototype implementation: diff -r d8f22418ca99 src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java Tue Sep 03 13:55:41 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java Tue Sep 03 15:45:56 2019 -0700 @@ -177,6 +177,11 @@ DEPRECATION("deprecation"), /** + * Warn about compiler generation of a default constructor. + */ + DEFAULT_CTOR("default-ctor"), + + /** * Warn about items which are documented with an {@code @deprecated} JavaDoc * comment, but which do not have {@code @Deprecated} annotation. */ diff -r d8f22418ca99 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Tue Sep 03 13:55:41 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Tue Sep 03 15:45:56 2019 -0700 @@ -3566,6 +3566,20 @@ } } + /** + * + */ + void checkDefaultConstructor(ClassSymbol c) { + // Kick out anonymous and private? + if (lint.isEnabled(Lint.LintCategory.DEFAULT_CTOR) && + (c.flags() & ENUM) == 0 && + !c.isAnonymous() && + (c.flags() & PUBLIC) != 0) { + System.out.println("Default ctor on " + c); + } + return; + } + private class ConversionWarner extends Warner { final String uncheckedKey; final Type found; diff -r d8f22418ca99 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java Tue Sep 03 13:55:41 2019 -0400 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java Tue Sep 03 15:45:56 2019 -0700 @@ -914,6 +914,8 @@ } } if (addConstructor) { + // Lint check here + chk.checkDefaultConstructor(sym); MethodSymbol basedConstructor = nc != null ? (MethodSymbol)nc.constructor : null; JCTree constrDef = DefaultConstructor(make.at(tree.pos), sym, Finds over 250 instances of default constructors when compiling java.base; see attached file. Important to *not* warn for enum and anonymous types. A real implementation will obviously need a new resource key, etc. Some tuning of the conditions to emit the warning may be needed as well.
03-09-2019