Skip to content

Commit

Permalink
Fix unused private constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen committed Sep 2, 2024
1 parent 24efb01 commit d80d86e
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ private boolean isPrivateDeclaration(Tree tree) {
if (tree instanceof JCClassDecl classTree) {
return (classTree.getModifiers().flags & Flags.PRIVATE) != 0;
} else if (tree instanceof JCMethodDecl methodTree) {
return !isConstructor(methodTree) && (methodTree.getModifiers().flags & Flags.PRIVATE) != 0;
if (isConstructor(methodTree)) {
if (hasPackageVisibleConstructor(methodTree.sym.owner)) {
return (methodTree.getModifiers().flags & Flags.PRIVATE) != 0;
}
return false;
}
return (methodTree.getModifiers().flags & Flags.PRIVATE) != 0;
} else if (tree instanceof JCVariableDecl variable) {
Symbol owner = variable.sym == null ? null : variable.sym.owner;
if (owner instanceof ClassSymbol) {
Expand All @@ -184,6 +190,20 @@ private boolean isConstructor(JCMethodDecl methodDecl) {
&& methodDecl.sym.isConstructor();
}

private boolean hasPackageVisibleConstructor(Symbol symbol) {
if (symbol instanceof ClassSymbol clazz) {
for (var member : clazz.members().getSymbols()) {
if (member instanceof MethodSymbol method) {
if (method.isConstructor() && (method.flags() & Flags.PRIVATE) == 0) {
return true;
}
}
}
}

return false;
}

private boolean isPrivateSymbol(Symbol symbol) {
if (symbol instanceof ClassSymbol
|| symbol instanceof MethodSymbol) {
Expand Down

0 comments on commit d80d86e

Please sign in to comment.