use of org.sonar.plugins.java.api.tree.SwitchStatementTree 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.SwitchStatementTree 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);
}
}
Aggregations