Skip to content

Commit

Permalink
Use variable kind to check if it qualifies for unused checker
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen committed Sep 5, 2024
1 parent 26e31ed commit 7636f71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.lang.model.element.ElementKind;
import javax.tools.JavaFileObject;

import org.eclipse.jdt.core.compiler.CategorizedProblem;
Expand All @@ -33,8 +34,6 @@

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCImport;
Expand Down Expand Up @@ -169,22 +168,20 @@ public List<CategorizedProblem> addUnusedPrivateMembers(CompilationUnitTree unit
int line = (int) unit.getLineMap().getLineNumber(pos);
int column = (int) unit.getLineMap().getColumnNumber(pos);
int problemId = IProblem.LocalVariableIsNeverUsed;
String[] arguments = null;
String name = variableDecl.name.toString();
String[] arguments = new String[] { name };
VarSymbol varSymbol = variableDecl.sym;
if (varSymbol.owner instanceof ClassSymbol) {
ElementKind varKind = varSymbol == null ? null : varSymbol.getKind();
if (varKind == ElementKind.FIELD) {
problemId = IProblem.UnusedPrivateField;
String typeName = varSymbol.owner.name.toString();
arguments = new String[] {
typeName, name
};
} else if (varSymbol.owner instanceof MethodSymbol methodSymbol) {
if (methodSymbol.type != null && methodSymbol.params().indexOf(varSymbol) >= 0) {
problemId = IProblem.ArgumentIsNeverUsed;
} else {
problemId = IProblem.LocalVariableIsNeverUsed;
}
arguments = new String[] { name };
} else if (varKind == ElementKind.PARAMETER) {
problemId = IProblem.ArgumentIsNeverUsed;
} else if (varKind == ElementKind.EXCEPTION_PARAMETER) {
problemId = IProblem.ExceptionParameterIsNeverUsed;
}

int severity = this.toSeverity(problemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.ElementKind;

import org.eclipse.jdt.core.compiler.CategorizedProblem;

import com.sun.source.tree.ClassTree;
Expand Down Expand Up @@ -76,7 +74,7 @@ public R visitImport(ImportTree node, P p) {

@Override
public R visitClass(ClassTree node, P p) {
if (node instanceof JCClassDecl classDecl && this.isPrivateDeclaration(classDecl)) {
if (node instanceof JCClassDecl classDecl && this.isPotentialUnusedDeclaration(classDecl)) {
this.privateDecls.add(classDecl);
}

Expand Down Expand Up @@ -119,7 +117,7 @@ public R visitMemberSelect(MemberSelectTree node, P p) {

@Override
public R visitMethod(MethodTree node, P p) {
boolean isPrivateMethod = this.isPrivateDeclaration(node);
boolean isPrivateMethod = this.isPotentialUnusedDeclaration(node);
if (isPrivateMethod) {
this.privateDecls.add(node);
}
Expand All @@ -129,7 +127,7 @@ public R visitMethod(MethodTree node, P p) {

@Override
public R visitVariable(VariableTree node, P p) {
boolean isPrivateVariable = this.isPrivateDeclaration(node);
boolean isPrivateVariable = this.isPotentialUnusedDeclaration(node);
if (isPrivateVariable) {
this.privateDecls.add(node);
}
Expand Down Expand Up @@ -158,26 +156,20 @@ public R visitNewClass(NewClassTree node, P p) {
return super.visitNewClass(node, p);
}

private boolean isPrivateDeclaration(Tree tree) {
private boolean isPotentialUnusedDeclaration(Tree tree) {
if (tree instanceof JCClassDecl classTree) {
return (classTree.getModifiers().flags & Flags.PRIVATE) != 0;
} else if (tree instanceof JCMethodDecl methodTree) {
if (isConstructor(methodTree)) {
if (hasPackageVisibleConstructor(methodTree.sym.owner)) {
return (methodTree.getModifiers().flags & Flags.PRIVATE) != 0;
}
return false;
return (methodTree.getModifiers().flags & Flags.PRIVATE) != 0
&& hasPackageVisibleConstructor(methodTree.sym.owner);
}
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) {
return !isSerialVersionConstant(variable) && (variable.getModifiers().flags & Flags.PRIVATE) != 0;
} else if (owner instanceof MethodSymbol) {
if (variable.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
return false;
}

return true;
}
}
Expand Down

0 comments on commit 7636f71

Please sign in to comment.