Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8071961: Add javac lint warning when a default constructor is created
Reviewed-by: jjg, jlahoda, abuckley, erikj, mcimadamore
  • Loading branch information
jddarcy committed Aug 18, 2020
1 parent 9ddc381 commit 607b761ff8045e9da9a231370faa502d7b369b7e
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 2 deletions.
@@ -76,6 +76,7 @@ java.datatransfer_COPY += flavormap.properties

################################################################################

java.desktop_DISABLED_WARNINGS += missing-explicit-ctor
java.desktop_DOCLINT += -Xdoclint:all/protected,-reference \
'-Xdoclint/package:java.*,javax.*'
java.desktop_COPY += .gif .png .wav .txt .xml .css .pf
@@ -298,6 +299,10 @@ java.xml.crypto_CLEAN += .properties

################################################################################

jdk.accessibility_DISABLED_WARNINGS += missing-explicit-ctor

################################################################################

jdk.charsets_COPY += .dat

################################################################################
@@ -347,10 +352,19 @@ jdk.javadoc_COPY += .xml .css .js .png

################################################################################

jdk.jartool_DISABLED_WARNINGS += missing-explicit-ctor
jdk.jartool_JAVAC_FLAGS += -XDstringConcat=inline

################################################################################

jdk.httpserver_DISABLED_WARNINGS += missing-explicit-ctor

################################################################################

jdk.unsupported.desktop_DISABLED_WARNINGS += missing-explicit-ctor

################################################################################

# No SCTP implementation on Mac OS X or AIX. These classes should be excluded.
SCTP_IMPL_CLASSES = \
$(TOPDIR)/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/AssociationChange.java \
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -210,6 +210,11 @@ public enum LintCategory {
*/
FINALLY("finally"),

/**
* Warn about compiler generation of a default constructor.
*/
MISSING_EXPLICIT_CTOR("missing-explicit-ctor"),

/**
* Warn about module system related issues.
*/
@@ -29,6 +29,7 @@
import java.util.function.Supplier;

import javax.lang.model.element.ElementKind;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileManager;

import com.sun.tools.javac.code.*;
@@ -3827,6 +3828,59 @@ void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> en
}
}

/**
* Check for a default constructor in an exported package.
*/
void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) {
if (lint.isEnabled(LintCategory.MISSING_EXPLICIT_CTOR) &&
((c.flags() & (ENUM | RECORD)) == 0) &&
!c.isAnonymous() &&
((c.flags() & PUBLIC) != 0) &&
Feature.MODULES.allowedInSource(source)) {
NestingKind nestingKind = c.getNestingKind();
switch (nestingKind) {
case ANONYMOUS,
LOCAL -> {return;}
case TOP_LEVEL -> {;} // No additional checks needed
case MEMBER -> {
// For nested member classes, all the enclosing
// classes must be public.
Symbol owner = c.owner;
while (owner != null && owner.kind == TYP) {
if ((owner.flags() & PUBLIC) == 0)
return;
owner = owner.owner;
}
}
}

// Only check classes in named packages exported by its module
PackageSymbol pkg = c.packge();
if (!pkg.isUnnamed()) {
ModuleSymbol modle = pkg.modle;
for (ExportsDirective exportDir : modle.exports) {
// Report warning only if the containing
// package is unconditionally exported
if (exportDir.packge.equals(pkg)) {
if (exportDir.modules == null || exportDir.modules.isEmpty()) {
// Warning may be suppressed by
// annotations; check again for being
// enabled in the deferred context.
deferredLintHandler.report(() -> {
if (lint.isEnabled(LintCategory.MISSING_EXPLICIT_CTOR))
log.warning(LintCategory.MISSING_EXPLICIT_CTOR,
pos, Warnings.MissingExplicitCtor(c, pkg, modle));
});
} else {
return;
}
}
}
}
}
return;
}

private class ConversionWarner extends Warner {
final String uncheckedKey;
final Type found;
@@ -1000,6 +1000,7 @@ protected void runPhase(Env<AttrContext> env) {
// Add default constructor if needed.
DefaultConstructorHelper helper = getDefaultConstructorHelper(env);
if (helper != null) {
chk.checkDefaultConstructor(sym, tree.pos());
defaultConstructor = defaultConstructor(make.at(tree.pos), helper);
tree.defs = tree.defs.prepend(defaultConstructor);
}
@@ -1760,6 +1760,10 @@ compiler.warn.dir.path.element.not.found=\
compiler.warn.dir.path.element.not.directory=\
bad path element "{0}": not a directory

# 0: symbol, 1: symbol, 2: symbol
compiler.warn.missing-explicit-ctor=\
class {0} in exported package {1} declares no explicit constructors, thereby exposing a default constructor to clients of module {2}

compiler.warn.finally.cannot.complete=\
finally clause cannot complete normally

@@ -1,5 +1,5 @@
#
# Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@@ -182,6 +182,9 @@ javac.opt.Xlint.desc.cast=\
javac.opt.Xlint.desc.classfile=\
Warn about issues related to classfile contents.

javac.opt.Xlint.desc.missing-explicit-ctor=\
Warn about missing explicit constructors in public classes in exported packages.

javac.opt.Xlint.desc.deprecation=\
Warn about use of deprecated items.

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

// key: compiler.warn.missing-explicit-ctor
// options: -Xlint:missing-explicit-ctor
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

module defaultctor {
exports pkg;
}
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package pkg;

public class Foo {
// No explicit constructor.
}

0 comments on commit 607b761

Please sign in to comment.