8251921: Expand default constructor warning to cover more cases

Reviewed-by: jjg, abuckley
This commit is contained in:
Joe Darcy 2020-08-27 13:01:41 -07:00
parent 235ef8e6df
commit ba7f7fe417
3 changed files with 27 additions and 11 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac/warnings/DefaultCtor

@ -3835,7 +3835,7 @@ public class Check {
if (lint.isEnabled(LintCategory.MISSING_EXPLICIT_CTOR) &&
((c.flags() & (ENUM | RECORD)) == 0) &&
!c.isAnonymous() &&
((c.flags() & PUBLIC) != 0) &&
((c.flags() & (PUBLIC | PROTECTED)) != 0) &&
Feature.MODULES.allowedInSource(source)) {
NestingKind nestingKind = c.getNestingKind();
switch (nestingKind) {
@ -3844,10 +3844,10 @@ public class Check {
case TOP_LEVEL -> {;} // No additional checks needed
case MEMBER -> {
// For nested member classes, all the enclosing
// classes must be public.
// classes must be public or protected.
Symbol owner = c.owner;
while (owner != null && owner.kind == TYP) {
if ((owner.flags() & PUBLIC) == 0)
if ((owner.flags() & (PUBLIC | PROTECTED)) == 0)
return;
owner = owner.owner;
}

@ -183,7 +183,7 @@ 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.
Warn about missing explicit constructors in public and protected classes in exported packages.
javac.opt.Xlint.desc.deprecation=\
Warn about use of deprecated items.

@ -97,7 +97,9 @@ public class DefaultCtorWarningToolBox extends TestRunner {
List.of("Foo.java:4:8: compiler.warn.missing-explicit-ctor: pkg1.Foo, pkg1, mod",
"Foo.java:12:12: compiler.warn.missing-explicit-ctor: pkg1.Foo.FooNest, pkg1, mod",
"Foo.java:16:19: compiler.warn.missing-explicit-ctor: pkg1.Foo.StaticFooNest, pkg1, mod",
"3 warnings");
"Foo.java:25:15: compiler.warn.missing-explicit-ctor: pkg1.Foo.ProtectedFooNest, pkg1, mod",
"Foo.java:27:19: compiler.warn.missing-explicit-ctor: pkg1.Foo.ProtectedFooNest.ProtectedFooNestNest, pkg1, mod",
"5 warnings");
// Warning enable,
log = new JavacTask(tb)
@ -137,30 +139,34 @@ public class DefaultCtorWarningToolBox extends TestRunner {
class Bar {
// No explicit constructor; use a default.
public class FooNest {
public class BarNest {
}
// No explicit constructor; use a default.
public static class StaticFooNest {
public static class StaticBarNest {
}
// No explicit constructor; use a default.
protected class ProtectedBarNest {
}
// Package-access classes
// No explicit constructor; use a default.
/*package*/ class PkgFooNest {
/*package*/ class PkgBarNest {
}
// No explicit constructor; use a default.
/*package*/ static class PkgStaticFooNest {
/*package*/ static class PkgStaticBarNest {
}
// Private classes
// No explicit constructor; use a default.
private class PrvFooNest {
private class PrvBarNest {
}
// No explicit constructor; use a default.
private static class PrvStaticFooNest {
private static class PrvStaticBarNest {
}
}
""";
@ -190,10 +196,18 @@ public class DefaultCtorWarningToolBox extends TestRunner {
public static class SuppressedStaticFooNest {
}
// No explicit constructor; use a default.
protected class ProtectedFooNest {
// No explicit constructor; use a default.
protected class ProtectedFooNestNest {}
}
// Package-access classes
// No explicit constructor; use a default.
/*package*/ class PkgFooNest {
// No explicit constructor; use a default.
protected class PkgFooNestNest {}
}
// No explicit constructor; use a default.
@ -203,6 +217,8 @@ public class DefaultCtorWarningToolBox extends TestRunner {
// No explicit constructor; use a default.
private class PrvFooNest {
// No explicit constructor; use a default.
protected class PrvFooNestNest {}
}
// No explicit constructor; use a default.