Search in sources :

Example 6 with CaseGroupTree

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

Example 7 with CaseGroupTree

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);
}
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 8 with CaseGroupTree

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);
    }
}
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)

Example 9 with CaseGroupTree

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

Example 10 with CaseGroupTree

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

Aggregations

CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)10 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)5 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 CaseLabelTree (org.sonar.plugins.java.api.tree.CaseLabelTree)2 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)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 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 LabeledStatementTree (org.sonar.plugins.java.api.tree.LabeledStatementTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)1