--- old/make/CompileJavaModules.gmk 2020-07-31 14:04:28.285167000 -0700 +++ new/make/CompileJavaModules.gmk 2020-07-31 14:04:27.657167000 -0700 @@ -76,6 +76,7 @@ ################################################################################ +java.desktop_DISABLED_WARNINGS += default-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 @@ ################################################################################ +jdk.accessibility_DISABLED_WARNINGS += default-ctor + +################################################################################ + jdk.charsets_COPY += .dat ################################################################################ @@ -347,10 +352,19 @@ ################################################################################ +jdk.jartool_DISABLED_WARNINGS += default-ctor jdk.jartool_JAVAC_FLAGS += -XDstringConcat=inline ################################################################################ +jdk.httpserver_DISABLED_WARNINGS += default-ctor + +################################################################################ + +jdk.unsupported.desktop_DISABLED_WARNINGS += default-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 \ --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java 2020-07-31 14:04:29.361167000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java 2020-07-31 14:04:28.721167000 -0700 @@ -211,6 +211,11 @@ FINALLY("finally"), /** + * Warn about compiler generation of a default constructor. + */ + MISSING_DECLARED_CTOR("missing-declared-ctor"), + + /** * Warn about module system related issues. */ MODULE("module"), --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java 2020-07-31 14:04:31.009167000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java 2020-07-31 14:04:30.341167000 -0700 @@ -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.*; @@ -2366,7 +2367,7 @@ tv.setUpperBound(types.createErrorType(t)); log.error(pos, Errors.CyclicInheritance(t)); } else if (t.hasTag(TYPEVAR)) { - tv = (TypeVar)t; + tv = (TypeVar)t; seen = seen.prepend(tv); for (Type b : types.getBounds(tv)) checkNonCyclic1(pos, b, seen); @@ -3827,6 +3828,54 @@ } } + /** + * Check for a default constructor in an exported package. + */ + void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) { + if (lint.isEnabled(LintCategory.MISSING_DECLARED_CTOR) && + !lint.isSuppressed(LintCategory.MISSING_DECLARED_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()) { + log.warning(LintCategory.MISSING_DECLARED_CTOR, + pos, Warnings.MissingDeclaredCtor(c, pkg, modle)); + } else { + return; + } + } + } + } + } + return; + } + private class ConversionWarner extends Warner { final String uncheckedKey; final Type found; --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java 2020-07-31 14:04:32.701167000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java 2020-07-31 14:04:32.077167000 -0700 @@ -1000,6 +1000,7 @@ // 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); } --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2020-07-31 14:04:34.457167000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2020-07-31 14:04:33.769167000 -0700 @@ -1760,6 +1760,10 @@ compiler.warn.dir.path.element.not.directory=\ bad path element "{0}": not a directory +# 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 + compiler.warn.finally.cannot.complete=\ finally clause cannot complete normally --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties 2020-07-31 14:04:36.213167000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties 2020-07-31 14:04:35.573167000 -0700 @@ -182,6 +182,9 @@ javac.opt.Xlint.desc.classfile=\ Warn about issues related to classfile contents. +javac.opt.Xlint.desc.missing-declared-ctor=\ + Warn about missing declared constructors in public classes in exported packages. + javac.opt.Xlint.desc.deprecation=\ Warn about use of deprecated items. --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/diags/examples/WarnDefaultCtor/DefaultCtor.java 2020-07-31 14:04:36.649167000 -0700 @@ -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.default-ctor +// options: -Xlint:default-ctor --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/diags/examples/WarnDefaultCtor/modulesourcepath/defaultctor/module-info.java 2020-07-31 14:04:37.837167000 -0700 @@ -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; +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/diags/examples/WarnDefaultCtor/modulesourcepath/defaultctor/pkg/Foo.java 2020-07-31 14:04:39.045167000 -0700 @@ -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. +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/NoWarningCases.java 2020-07-31 14:04:40.213167000 -0700 @@ -0,0 +1,39 @@ +/* + * 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. + */ + +/* + * @test + * @bug 8071961 + * @compile -Xlint:missing-declared-ctor -Werror --release 8 NoWarningCases.java + * @compile -Xlint:missing-declared-ctor -Werror NoWarningCases.java + */ + +public class NoWarningCases { + // No explicit constructor; use a default. + + public enum NestedEnum { + FOO, + BAR; + // No explicit constructor; use implicit one. + } +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/NoWarningRecord.java 2020-07-31 14:04:41.461167000 -0700 @@ -0,0 +1,33 @@ +/* + * 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. + */ + +/* + * @test + * @bug 8071961 + * @compile -Xlint:missing-declared-ctor -Werror + * --enable-preview -source ${jdk.version} NoWarningRecord.java + */ + +public record NoWarningRecord(/* no components */) { + // No explicit constructor; use canonical one. +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/defaultctor.out 2020-07-31 14:04:42.661167000 -0700 @@ -0,0 +1,5 @@ +Foo.java:4:8: compiler.warn.missing-declared-ctor: pkg1.Foo, pkg1, mod +Foo.java:12:12: compiler.warn.missing-declared-ctor: pkg1.Foo.FooNest, pkg1, mod +Foo.java:16:19: compiler.warn.missing-declared-ctor: pkg1.Foo.StaticFooNest, pkg1, mod +Foo.java:21:19: compiler.warn.missing-declared-ctor: pkg1.Foo.SuppressedStaticFooNest, pkg1, mod +4 warnings --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/module-info.java 2020-07-31 14:04:43.877167000 -0700 @@ -0,0 +1,37 @@ +/* + * 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. + */ + +/* + * @test + * @bug 8071961 + * @compile -Xlint:-missing-declared-ctor -Werror + * module-info.java pkg1/Foo.java pkg1/Bar.java pkg2/Baz.java pkg2/Quux.java pkg3/Corge.java pkg3/Grault.java + * @compile/ref=defaultctor.out -XDrawDiagnostics -Xlint:missing-declared-ctor + * module-info.java pkg1/Foo.java pkg1/Bar.java pkg2/Baz.java pkg2/Quux.java pkg3/Corge.java pkg3/Grault.java + */ + +module mod { + exports pkg1; + // Do *not* export pkg2. + exports pkg3 to java.base; +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg1/Bar.java 2020-07-31 14:04:45.089167000 -0700 @@ -0,0 +1,36 @@ +package pkg1; + +// Neither the top-level class nor the nested classes should generate +// a warning since Bar is not public. + +// No explicit constructor; use a default. +class Bar { + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg1/Foo.java 2020-07-31 14:04:46.269167000 -0700 @@ -0,0 +1,43 @@ +package pkg1; + +// No explicit constructor; use a default. +public class Foo { + + /* + * Of the nexted classes, only FooNest and StaticFooNest should + * generate warnings. + */ + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // No explicit constructor; use a default. + @SuppressWarnings("missing-declared-ctor") + public static class SuppressedStaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg2/Baz.java 2020-07-31 14:04:47.465167000 -0700 @@ -0,0 +1,36 @@ +package pkg2; + +// None of these classes should generate warnings since pkg2 is not +// exported unconditionally. + +// No explicit constructor; use a default. +public class Baz { + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg2/Quux.java 2020-07-31 14:04:48.681167000 -0700 @@ -0,0 +1,36 @@ +package pkg2; + +// Neither the top-level class nor the nested classes should generate +// a warning since Bar is not public. + +// No explicit constructor; use a default. +class Quux { + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg3/Corge.java 2020-07-31 14:04:49.857167000 -0700 @@ -0,0 +1,36 @@ +package pkg3; + +// None of these classes should generate warnings since pkg3 is not +// exported unconditionally. + +// No explicit constructor; use a default. +public class Corge { + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +} --- /dev/null 2020-07-24 14:18:40.280000000 -0700 +++ new/test/langtools/tools/javac/warnings/DefaultCtor/pkg3/Grault.java 2020-07-31 14:04:51.053167000 -0700 @@ -0,0 +1,36 @@ +package pkg3; + +// None of these classes should generate warnings since pkg3 is not +// exported unconditionally. + +// No explicit constructor; use a default. +class Grault { + + // No explicit constructor; use a default. + public class FooNest { + } + + // No explicit constructor; use a default. + public static class StaticFooNest { + } + + // Package-access classes + + // No explicit constructor; use a default. + /*package*/ class PkgFooNest { + } + + // No explicit constructor; use a default. + /*package*/ static class PkgStaticFooNest { + } + // Private classes + + // No explicit constructor; use a default. + private class PrvFooNest { + } + + // No explicit constructor; use a default. + private static class PrvStaticFooNest { + } + +}