use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class SwitchWithTooManyCasesCheck method visitNode.
@Override
public void visitNode(Tree tree) {
SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
if (isSwitchOverEnum(switchStatementTree)) {
return;
}
List<CaseGroupTree> cases = switchStatementTree.cases();
int size = cases.size();
if (size > maximumCases) {
List<JavaFileScannerContext.Location> secondary = new ArrayList<>();
for (CaseGroupTree element : cases) {
secondary.add(new JavaFileScannerContext.Location("+1", element.labels().get(0)));
}
reportIssue(switchStatementTree.switchKeyword(), "Reduce the number of non-empty switch cases from " + size + " to at most " + maximumCases + ".", secondary, null);
}
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method switch_statement.
/**
* 14.11. The switch Statement
*/
@Test
public void switch_statement() {
SwitchStatementTree tree = (SwitchStatementTree) firstMethodFirstStatement("class T { void m() { switch (e) { case 1: case 2: ; default: ; } } }");
assertThat(tree.is(Tree.Kind.SWITCH_STATEMENT)).isTrue();
assertThat(tree.switchKeyword().text()).isEqualTo("switch");
assertThat(tree.openParenToken().text()).isEqualTo("(");
assertThat(tree.expression()).isNotNull();
assertThat(tree.closeParenToken().text()).isEqualTo(")");
assertThat(tree.cases()).hasSize(2);
assertThatChildrenIteratorHasSize(tree, 8);
CaseGroupTree c = tree.cases().get(0);
assertThat(c.is(Tree.Kind.CASE_GROUP)).isTrue();
assertThat(c.labels()).hasSize(2);
CaseLabelTree caseLabelTree = c.labels().get(0);
assertThat(caseLabelTree.is(Tree.Kind.CASE_LABEL)).isTrue();
assertThat(caseLabelTree.caseOrDefaultKeyword().text()).isEqualTo("case");
assertThat(caseLabelTree.expression()).isNotNull();
assertThat(caseLabelTree.colonToken().text()).isEqualTo(":");
assertThatChildrenIteratorHasSize(caseLabelTree, 3);
caseLabelTree = c.labels().get(1);
assertThat(caseLabelTree.is(Tree.Kind.CASE_LABEL)).isTrue();
assertThat(caseLabelTree.caseOrDefaultKeyword().text()).isEqualTo("case");
assertThat(caseLabelTree.expression()).isNotNull();
assertThat(caseLabelTree.colonToken().text()).isEqualTo(":");
assertThatChildrenIteratorHasSize(caseLabelTree, 3);
assertThat(c.body()).hasSize(1);
c = tree.cases().get(1);
assertThat(c.is(Tree.Kind.CASE_GROUP)).isTrue();
assertThat(c.labels()).hasSize(1);
caseLabelTree = c.labels().get(0);
assertThat(caseLabelTree.is(Tree.Kind.CASE_LABEL)).isTrue();
assertThat(caseLabelTree.caseOrDefaultKeyword().text()).isEqualTo("default");
assertThat(caseLabelTree.expression()).isNull();
assertThat(caseLabelTree.colonToken().text()).isEqualTo(":");
assertThatChildrenIteratorHasSize(caseLabelTree, 2);
assertThat(c.body()).hasSize(1);
tree = (SwitchStatementTree) firstMethodFirstStatement("class T { void m() { switch (e) { default: } } }");
assertThat(tree.cases()).hasSize(1);
assertThat(tree.cases().get(0).body()).isEmpty();
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class AllBranchesAreIdenticalCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.SWITCH_STATEMENT)) {
SwitchStatementTree switchStatement = (SwitchStatementTree) tree;
Multimap<CaseGroupTree, CaseGroupTree> identicalBranches = checkSwitchStatement(switchStatement);
if (hasDefaultClause(switchStatement) && allBranchesSame(identicalBranches, switchStatement.cases().size())) {
reportIssue(switchStatement.switchKeyword(), IF_SWITCH_MSG);
}
} else if (tree.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatementTree = (IfStatementTree) tree;
if (hasElseClause(ifStatementTree) && !tree.parent().is(Tree.Kind.IF_STATEMENT)) {
IfElseChain ifElseChain = checkIfStatement(ifStatementTree);
if (allBranchesSame(ifElseChain.branches, ifElseChain.totalBranchCount)) {
reportIssue(ifStatementTree.ifKeyword(), IF_SWITCH_MSG);
}
}
} else {
checkConditionalExpression((ConditionalExpressionTree) tree);
}
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class IndentationCheck method visitSwitchStatement.
@Override
public void visitSwitchStatement(SwitchStatementTree tree) {
newBlock();
scan(tree.expression());
for (CaseGroupTree caseGroupTree : tree.cases()) {
newBlock();
checkCaseGroup(caseGroupTree);
scan(caseGroupTree);
leaveNode(caseGroupTree);
}
leaveNode(tree);
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class IdenticalCasesInSwitchCheck method checkSwitchStatement.
protected Multimap<CaseGroupTree, CaseGroupTree> checkSwitchStatement(SwitchStatementTree node) {
SetMultimap<CaseGroupTree, CaseGroupTree> identicalBranches = HashMultimap.create();
int index = 0;
List<CaseGroupTree> cases = node.cases();
for (CaseGroupTree caseGroupTree : cases) {
index++;
if (identicalBranches.containsValue(caseGroupTree)) {
continue;
}
for (int i = index; i < cases.size(); i++) {
if (SyntacticEquivalence.areEquivalent(caseGroupTree.body(), cases.get(i).body())) {
identicalBranches.put(caseGroupTree, cases.get(i));
}
}
}
return identicalBranches;
}
Aggregations