Skip to content

Commit

Permalink
Refactor VoidVisitorAdapter analyzers to pass Analysis as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Jan 21, 2024
1 parent 1e8a9cf commit c9383c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
18 changes: 8 additions & 10 deletions src/main/java/analyzer/exercises/GlobalAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@

import java.util.List;

public class GlobalAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer {
private Analysis analysis;
public class GlobalAnalyzer extends VoidVisitorAdapter<Analysis> implements Analyzer {

@Override
public void analyze(List<CompilationUnit> 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) {
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,70 @@
import java.util.List;
import java.util.Set;

public class LeapAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer {
public class LeapAnalyzer extends VoidVisitorAdapter<Analysis> implements Analyzer {
private static final Set<Integer> TEST_CASES = Set.of(1960, 1996, 2000, 2400);
private static final Set<String> DISALLOWED_IMPORTS = Set.of(
"java.time",
"java.util.GregorianCalendar"
);

private final Set<Integer> intLiterals = new HashSet<>();
private Analysis analysis;

@Override
public void analyze(List<CompilationUnit> 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) {
Expand Down

0 comments on commit c9383c1

Please sign in to comment.