Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8251921: Expand default constructor warning to cover more cases
Reviewed-by: jjg, abuckley
  • Loading branch information
jddarcy committed Aug 27, 2020
1 parent 461a4d5 commit 1845dd2a4a6b197c19d3016434df5afdad3c624b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
@@ -3835,7 +3835,7 @@ void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) {
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 @@ void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) {
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 void testWithAndWithOutLint(Path base) throws IOException {
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 @@ protected void runTests() throws Exception {
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 static class StaticFooNest {
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 static class SuppressedStaticFooNest {

// No explicit constructor; use a default.
private class PrvFooNest {
// No explicit constructor; use a default.
protected class PrvFooNestNest {}
}

// No explicit constructor; use a default.

0 comments on commit 1845dd2

Please sign in to comment.