added record support to finalguard plugin
Some checks failed
CI / build (push) Failing after 1m18s

This commit is contained in:
2025-11-19 00:50:23 +08:00
parent d78b8d443e
commit 9dcadec69f
4 changed files with 39 additions and 34 deletions

View File

@@ -104,8 +104,14 @@ public class FinalGuardPlugin implements Plugin {
} }
} }
private static boolean isJava17OrHigher() {
return System.getProperty("java.version").compareTo("17") >= 0;
}
private static final Configuration configuration = new Configuration(); private static final Configuration configuration = new Configuration();
private static final boolean isJava17OrHigher = isJava17OrHigher();
@Override @Override
public String getName() { public String getName() {
return getClass().getName(); return getClass().getName();
@@ -140,7 +146,6 @@ public class FinalGuardPlugin implements Plugin {
private final Trees trees; private final Trees trees;
private final Map<String, VariableInfo> variableInfoMap = new LinkedHashMap<>(); private final Map<String, VariableInfo> variableInfoMap = new LinkedHashMap<>();
private final Set<String> reassignedVariables = new HashSet<>(); private final Set<String> reassignedVariables = new HashSet<>();
private String currentMethod;
public FinalVariableAnalyzer(CompilationUnitTree compilationUnit, JavacTask task) { public FinalVariableAnalyzer(CompilationUnitTree compilationUnit, JavacTask task) {
this.compilationUnit = compilationUnit; this.compilationUnit = compilationUnit;
@@ -149,50 +154,45 @@ public class FinalGuardPlugin implements Plugin {
@Override @Override
public Void visitMethod(MethodTree node, Void p) { public Void visitMethod(MethodTree node, Void p) {
final String previousMethod = currentMethod;
currentMethod = node.getName().toString();
variableInfoMap.clear(); variableInfoMap.clear();
reassignedVariables.clear(); reassignedVariables.clear();
// Analyze parameters first
for (VariableTree param : node.getParameters()) {
final String varName = param.getName().toString();
variableInfoMap.put(varName, new VariableInfo(param, VariableType.METHOD_PARAM));
}
// Then analyze method body
super.visitMethod(node, p); super.visitMethod(node, p);
// Check for variables that could be final // Check for variables that could be final
checkForFinalCandidates(); checkForFinalCandidates();
currentMethod = previousMethod;
return null; return null;
} }
@Override @Override
public Void visitVariable(VariableTree node, Void p) { public Void visitVariable(VariableTree node, Void p) {
if (currentMethod != null) { final String varName = node.getName().toString();
final String varName = node.getName().toString(); final TreePath currentPath = getCurrentPath();
final Tree parent = getCurrentPath().getParentPath().getLeaf(); final TreePath parentPath = currentPath.getParentPath();
final VariableType type; final Tree parent = parentPath.getLeaf();
if (parent instanceof LambdaExpressionTree) { final VariableType type;
type = VariableType.LAMBDA_PARAM; if (parent instanceof LambdaExpressionTree) {
} else if (parent instanceof ForLoopTree || parent instanceof EnhancedForLoopTree) { type = VariableType.LAMBDA_PARAM;
type = VariableType.LOOP_PARAM; } else if (parent instanceof ForLoopTree || parent instanceof EnhancedForLoopTree) {
} else if (parent instanceof CatchTree) { type = VariableType.LOOP_PARAM;
type = VariableType.CATCH_PARAM; } else if (parent instanceof CatchTree) {
} else if (parent instanceof TryTree) { type = VariableType.CATCH_PARAM;
type = VariableType.TRY_WITH_PARAM; } else if (parent instanceof TryTree) {
} else if (parent instanceof MethodTree) { type = VariableType.TRY_WITH_PARAM;
type = VariableType.METHOD_PARAM; } else if (parent instanceof MethodTree) {
} else if (parent instanceof BlockTree) { type = VariableType.METHOD_PARAM;
type = VariableType.LOCAL_VAR; if(isJava17OrHigher && ((MethodTree) parent).getName().contentEquals("<init>")) {
} else { final TreePath grandParentPath = parentPath.getParentPath();
type = VariableType.LOCAL_VAR; if(grandParentPath.getLeaf().getKind() == Tree.Kind.RECORD) {
return super.visitVariable(node, p);
}
} }
variableInfoMap.put(varName, new VariableInfo(node, type)); } else if (parent instanceof BlockTree) {
type = VariableType.LOCAL_VAR;
} else {
type = VariableType.LOCAL_VAR;
} }
variableInfoMap.put(varName, new VariableInfo(node, type));
return super.visitVariable(node, p); return super.visitVariable(node, p);
} }

View File

@@ -165,7 +165,8 @@ public class PluginTest {
LOCAL_VAR.getMessage("result"), LOCAL_VAR.getMessage("result"),
LOCAL_VAR.getMessage("size"), LOCAL_VAR.getMessage("size"),
METHOD_PARAM.getMessage("t1s") METHOD_PARAM.getMessage("t1s")
)) )),
Arguments.of(prefix + "TestCase12.java", Collections.emptyList())
); );
} }
} }

View File

@@ -0,0 +1,4 @@
import java.util.Arrays;
public record TestCase12<T, U>(double x, double y) {
}

View File

@@ -1,5 +1,5 @@
lys.catalog.version=2025.09.30 lys.catalog.version=2025.11.18
version.myGradlePlugins=2025.11.18 version.myGradlePlugins=2025.11.19
version.gradle=8.12 version.gradle=8.12
gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven