Search in sources :

Example 1 with CaseLabelTree

use of org.sonar.plugins.java.api.tree.CaseLabelTree in project sonar-java by SonarSource.

the class SwitchCaseWithoutBreakCheck method visitSwitchStatement.

@Override
public void visitSwitchStatement(SwitchStatementTree switchStatement) {
    switchStatement.cases().stream().limit(Math.max(0, switchStatement.cases().size() - 1)).forEach(caseGroup -> {
        // Assign issues to the last label in the group
        CaseLabelTree caseLabel = caseGroup.labels().get(caseGroup.labels().size() - 1);
        // Reverse the body as commonly the unconditional exit will be at the end of the body.
        if (Lists.reverse(caseGroup.body()).stream().noneMatch(SwitchCaseWithoutBreakCheck::isUnconditionalExit) && !intentionalFallThrough(switchStatement, caseGroup)) {
            context.reportIssue(this, caseLabel, "End this switch case with an unconditional break, return or throw statement.");
        }
    });
    super.visitSwitchStatement(switchStatement);
}
Also used : CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree)

Example 2 with CaseLabelTree

use of org.sonar.plugins.java.api.tree.CaseLabelTree in project sonar-java by SonarSource.

the class SwitchDefaultLastCaseCheck method getDefaultLabelAtWrongPosition.

private static Optional<CaseLabelTree> getDefaultLabelAtWrongPosition(SwitchStatementTree switchStatementTree) {
    for (int i = 0; i < switchStatementTree.cases().size(); i++) {
        List<CaseLabelTree> labels = switchStatementTree.cases().get(i).labels();
        for (int j = 0; j < labels.size(); j++) {
            CaseLabelTree label = labels.get(j);
            boolean defaultExists = isDefault(label);
            if (defaultExists && ((j != labels.size() - 1) || (j == labels.size() - 1 && i == switchStatementTree.cases().size() - 1))) {
                /*
           * we return Optional.empty() because either we have default at the end which is a best practise
           * or it is in a place in a case group where it can not affect the result of the execution
           */
                return Optional.empty();
            } else if (defaultExists) {
                return Optional.of(label);
            }
        }
    }
    return Optional.empty();
}
Also used : CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree)

Example 3 with CaseLabelTree

use of org.sonar.plugins.java.api.tree.CaseLabelTree 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 4 with CaseLabelTree

use of org.sonar.plugins.java.api.tree.CaseLabelTree in project sonar-java by SonarSource.

the class IndentationCheck method checkCaseGroup.

private void checkCaseGroup(CaseGroupTree tree) {
    List<CaseLabelTree> labels = tree.labels();
    if (labels.size() >= 2) {
        CaseLabelTree previousCaseLabelTree = labels.get(labels.size() - 2);
        excludeIssueAtLine = previousCaseLabelTree.lastToken().line();
    }
    List<StatementTree> body = tree.body();
    List<StatementTree> newBody = body;
    int bodySize = body.size();
    if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
        expectedLevel -= indentationLevel;
        checkIndentation(body.get(0), Iterables.getLast(labels).colonToken().column() + 2);
        newBody = body.subList(1, bodySize);
    }
    checkIndentation(newBody);
    if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
        expectedLevel += indentationLevel;
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree)

Aggregations

CaseLabelTree (org.sonar.plugins.java.api.tree.CaseLabelTree)4 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)2 Test (org.junit.Test)1 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1