From c9383c1c1b893509920dd4d075dba844db12a11d Mon Sep 17 00:00:00 2001 From: Sander Ploegsma Date: Sun, 21 Jan 2024 20:26:20 +0100 Subject: [PATCH] Refactor VoidVisitorAdapter analyzers to pass Analysis as argument --- .../analyzer/exercises/GlobalAnalyzer.java | 18 ++++--- .../analyzer/exercises/leap/LeapAnalyzer.java | 47 +++++++++---------- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/java/analyzer/exercises/GlobalAnalyzer.java b/src/main/java/analyzer/exercises/GlobalAnalyzer.java index b35c2da1..0f31f99b 100644 --- a/src/main/java/analyzer/exercises/GlobalAnalyzer.java +++ b/src/main/java/analyzer/exercises/GlobalAnalyzer.java @@ -11,33 +11,31 @@ import java.util.List; -public class GlobalAnalyzer extends VoidVisitorAdapter implements Analyzer { - private Analysis analysis; +public class GlobalAnalyzer extends VoidVisitorAdapter implements Analyzer { @Override public void analyze(List compilationUnits, Analysis analysis) { - this.analysis = analysis; for (CompilationUnit compilationUnit : compilationUnits) { - compilationUnit.accept(this, null); + compilationUnit.accept(this, analysis); } } @Override - public void visit(MethodDeclaration n, Void arg) { - if (isMainMethod(n)) { + public void visit(MethodDeclaration node, Analysis analysis) { + if (isMainMethod(node)) { analysis.addComment(new DoNotUseMainMethod()); } - super.visit(n, arg); + super.visit(node, analysis); } @Override - public void visit(MethodCallExpr n, Void arg) { - if (isPrintStatement(n)) { + public void visit(MethodCallExpr node, Analysis analysis) { + if (isPrintStatement(node)) { analysis.addComment(new AvoidPrintStatements()); } - super.visit(n, arg); + super.visit(node, analysis); } private static boolean isMainMethod(MethodDeclaration node) { diff --git a/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java b/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java index acd66a7e..fd5289cf 100644 --- a/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java +++ b/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java @@ -16,7 +16,7 @@ import java.util.List; import java.util.Set; -public class LeapAnalyzer extends VoidVisitorAdapter implements Analyzer { +public class LeapAnalyzer extends VoidVisitorAdapter implements Analyzer { private static final Set TEST_CASES = Set.of(1960, 1996, 2000, 2400); private static final Set DISALLOWED_IMPORTS = Set.of( "java.time", @@ -24,65 +24,62 @@ public class LeapAnalyzer extends VoidVisitorAdapter implements Analyzer { ); private final Set intLiterals = new HashSet<>(); - private Analysis analysis; @Override public void analyze(List compilationUnits, Analysis analysis) { - this.analysis = analysis; - for (CompilationUnit compilationUnit : compilationUnits) { - compilationUnit.accept(this, null); + compilationUnit.accept(this, analysis); } } @Override - public void visit(CompilationUnit n, Void arg) { + public void visit(CompilationUnit node, Analysis analysis) { // Reset state for each compilation unit this.intLiterals.clear(); - super.visit(n, arg); + super.visit(node, analysis); } @Override - public void visit(ImportDeclaration n, Void arg) { - if (isUsingBuiltInMethods(n)) { - this.analysis.addComment(new NoBuiltInMethods()); + public void visit(ImportDeclaration node, Analysis analysis) { + if (isUsingBuiltInMethods(node)) { + analysis.addComment(new NoBuiltInMethods()); } - super.visit(n, arg); + super.visit(node, analysis); } @Override - public void visit(IntegerLiteralExpr n, Void arg) { - if (n.asNumber() instanceof Integer i) { + public void visit(IntegerLiteralExpr node, Analysis analysis) { + if (node.asNumber() instanceof Integer i) { this.intLiterals.add(i); } if (this.intLiterals.containsAll(TEST_CASES)) { - this.analysis.addComment(new AvoidHardCodedTestCases()); + analysis.addComment(new AvoidHardCodedTestCases()); } - super.visit(n, arg); + super.visit(node, analysis); } @Override - public void visit(IfStmt n, Void arg) { - this.analysis.addComment(new AvoidConditionalLogic()); - super.visit(n, arg); + public void visit(IfStmt node, Analysis analysis) { + analysis.addComment(new AvoidConditionalLogic()); + super.visit(node, analysis); } @Override - public void visit(ConditionalExpr n, Void arg) { - this.analysis.addComment(new AvoidConditionalLogic()); - super.visit(n, arg); + public void visit(ConditionalExpr node, Analysis analysis) { + analysis.addComment(new AvoidConditionalLogic()); + super.visit(node, analysis); } @Override - public void visit(MethodDeclaration n, Void arg) { - if (n.getNameAsString().equals("isLeapYear") && hasMoreThanThreeChecks(n)) { - this.analysis.addComment(new UseMinimumNumberOfChecks()); + public void visit(MethodDeclaration node, Analysis analysis) { + if (node.getNameAsString().equals("isLeapYear") && hasMoreThanThreeChecks(node)) { + analysis.addComment(new UseMinimumNumberOfChecks()); } - super.visit(n, arg); + super.visit(node, analysis); } private static boolean isUsingBuiltInMethods(ImportDeclaration node) {