Search in sources :

Example 6 with SwitchStatementTree

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);
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) Test(org.junit.Test)

Example 7 with SwitchStatementTree

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);
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Aggregations

SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)7 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)5 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 CaseLabelTree (org.sonar.plugins.java.api.tree.CaseLabelTree)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 LinesOfCodeVisitor (org.sonar.java.ast.visitors.LinesOfCodeVisitor)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)1