From 8617962f3fce7be36ea4d6c7d61cb3f26e86df5e Mon Sep 17 00:00:00 2001 From: Leonardo Pilastri Date: Mon, 18 Mar 2024 10:08:36 +0100 Subject: [PATCH] SONARJAVA-4904 Migrate S1301 to IssuableSubscriptionVisitor --- .../checks/SwitchAtLeastThreeCasesCheck.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/java-checks/src/main/java/org/sonar/java/checks/SwitchAtLeastThreeCasesCheck.java b/java-checks/src/main/java/org/sonar/java/checks/SwitchAtLeastThreeCasesCheck.java index c0d253841b9..be6ed794915 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/SwitchAtLeastThreeCasesCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/SwitchAtLeastThreeCasesCheck.java @@ -20,34 +20,34 @@ package org.sonar.java.checks; import org.sonar.check.Rule; -import org.sonar.plugins.java.api.JavaFileScanner; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.JavaFileScannerContext; -import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.CaseGroupTree; import org.sonar.plugins.java.api.tree.SwitchStatementTree; +import org.sonar.plugins.java.api.tree.Tree; + +import java.util.List; @Rule(key = "S1301") -public class SwitchAtLeastThreeCasesCheck extends BaseTreeVisitor implements JavaFileScanner { +public class SwitchAtLeastThreeCasesCheck extends IssuableSubscriptionVisitor { private JavaFileScannerContext context; @Override - public void scanFile(JavaFileScannerContext context) { - this.context = context; - scan(context.getTree()); + public List nodesToVisit() { + return List.of(Tree.Kind.SWITCH_STATEMENT); } @Override - public void visitSwitchStatement(SwitchStatementTree tree) { + public void visitNode(Tree tree) { + SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree; int count = 0; - for (CaseGroupTree caseGroup : tree.cases()) { + for (CaseGroupTree caseGroup : switchStatementTree.cases()) { count += caseGroup.labels().size(); } if (count < 3) { - context.reportIssue(this, tree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability."); + reportIssue(switchStatementTree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability."); } - - super.visitSwitchStatement(tree); } }