From 6b671dda870d8059e4852f78802619027d23b9c1 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Fri, 30 Aug 2024 19:55:34 +0800 Subject: [PATCH] Skip constructor and serialVersionUID field in the unused checker --- .../jdt/internal/javac/UnusedTreeScanner.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) 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 357c158fca3..d0a380089dd 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 @@ -39,8 +39,9 @@ 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.code.Type.JCPrimitiveType; +import com.sun.tools.javac.code.TypeTag; import com.sun.tools.javac.tree.JCTree.JCClassDecl; -import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCImport; @@ -159,11 +160,11 @@ private boolean isPrivateDeclaration(Tree tree) { if (tree instanceof JCClassDecl classTree) { return (classTree.getModifiers().flags & Flags.PRIVATE) != 0; } else if (tree instanceof JCMethodDecl methodTree) { - return !isSynthesizedConstructor(methodTree) && (methodTree.getModifiers().flags & Flags.PRIVATE) != 0; + return !isConstructor(methodTree) && (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 (variable.getModifiers().flags & Flags.PRIVATE) != 0; + return !isSerialVersionConstant(variable) && (variable.getModifiers().flags & Flags.PRIVATE) != 0; } else if (owner instanceof MethodSymbol) { return true; } @@ -172,11 +173,9 @@ private boolean isPrivateDeclaration(Tree tree) { return false; } - private boolean isSynthesizedConstructor(JCMethodDecl methodDecl) { - boolean isDefaultConstructor = methodDecl.getParameters().isEmpty() && methodDecl.sym != null + private boolean isConstructor(JCMethodDecl methodDecl) { + return methodDecl.sym != null && methodDecl.sym.isConstructor(); - int endPos = methodDecl.getEndPosition(((JCCompilationUnit) unit).endPositions); - return isDefaultConstructor && endPos < 0; } private boolean isPrivateSymbol(Symbol symbol) { @@ -207,6 +206,15 @@ private boolean isMemberSymbol(Symbol symbol) { return false; } + private boolean isSerialVersionConstant(JCVariableDecl variable) { + long flags = variable.getModifiers().flags; + return (flags & Flags.FINAL) != 0 + && (flags & Flags.STATIC) != 0 + && variable.type instanceof JCPrimitiveType type + && type.getTag() == TypeTag.LONG + && "serialVersionUID".equals(variable.name.toString()); + } + public List getUnusedImports(UnusedProblemFactory problemFactory) { return problemFactory.addUnusedImports(this.unit, this.unusedImports); }