From 7636f71f424abdc8d162e6e35b98358e950d40b3 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Thu, 5 Sep 2024 16:26:08 +0800 Subject: [PATCH] Use variable kind to check if it qualifies for unused checker --- .../internal/javac/UnusedProblemFactory.java | 19 ++++++++---------- .../jdt/internal/javac/UnusedTreeScanner.java | 20 ++++++------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedProblemFactory.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedProblemFactory.java index ecf57ad0e70..4a0c1a1bbb8 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedProblemFactory.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedProblemFactory.java @@ -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; @@ -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; @@ -169,22 +168,20 @@ public List 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); diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedTreeScanner.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedTreeScanner.java index 79896014fd8..27ffbcf204e 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedTreeScanner.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/UnusedTreeScanner.java @@ -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; @@ -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); } @@ -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); } @@ -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); } @@ -158,15 +156,13 @@ 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) { @@ -174,10 +170,6 @@ private boolean isPrivateDeclaration(Tree tree) { 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; } }