Search in sources :

Example 1 with CaseGroupTree

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

the class IdenticalCasesInSwitchCheck method visitNode.

@Override
public void visitNode(Tree node) {
    if (node.is(Tree.Kind.SWITCH_STATEMENT)) {
        SwitchStatementTree switchStatement = (SwitchStatementTree) node;
        Multimap<CaseGroupTree, CaseGroupTree> identicalBranches = checkSwitchStatement(switchStatement);
        boolean allBranchesSame = allBranchesSame(identicalBranches, switchStatement.cases().size());
        boolean allBranchesSameWithoutDefault = allBranchesSame && !hasDefaultClause(switchStatement);
        if (!allBranchesSame || allBranchesSameWithoutDefault) {
            identicalBranches.asMap().forEach((first, others) -> {
                if (!isTrivialCase(first.body()) || allBranchesSameWithoutDefault) {
                    others.forEach(other -> createIssue(other, issueMessage("case", first), first));
                }
            });
        }
    } else if (node.is(Tree.Kind.IF_STATEMENT) && !node.parent().is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatement = (IfStatementTree) node;
        IfElseChain ifElseChain = checkIfStatement(ifStatement);
        reportIdenticalIfChainBranches(ifElseChain.branches, ifElseChain.totalBranchCount, hasElseClause(ifStatement));
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 2 with CaseGroupTree

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

the class CFG method buildSwitchStatement.

private void buildSwitchStatement(SwitchStatementTree switchStatementTree) {
    Block switchSuccessor = currentBlock;
    // process condition
    currentBlock = createBlock();
    currentBlock.terminator = switchStatementTree;
    switches.addLast(currentBlock);
    build(switchStatementTree.expression());
    Block conditionBlock = currentBlock;
    // process body
    currentBlock = createBlock(switchSuccessor);
    breakTargets.addLast(switchSuccessor);
    boolean hasDefaultCase = false;
    if (!switchStatementTree.cases().isEmpty()) {
        CaseGroupTree firstCase = switchStatementTree.cases().get(0);
        for (CaseGroupTree caseGroupTree : Lists.reverse(switchStatementTree.cases())) {
            build(caseGroupTree.body());
            caseGroupTree.labels().forEach(l -> {
                if (l.expression() != null) {
                    build(l.expression());
                }
            });
            if (!hasDefaultCase) {
                hasDefaultCase = containsDefaultCase(caseGroupTree.labels());
            }
            switches.getLast().addSuccessor(currentBlock);
            if (!caseGroupTree.equals(firstCase)) {
                // No block predecessing the first case group.
                currentBlock = createBlock(currentBlock);
            }
        }
    }
    breakTargets.removeLast();
    // process condition
    currentBlock = switches.removeLast();
    if (!hasDefaultCase) {
        currentBlock.addSuccessor(switchSuccessor);
    }
    currentBlock = conditionBlock;
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree)

Example 3 with CaseGroupTree

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

the class SwitchCaseWithoutBreakCheck method intentionalFallThrough.

private static boolean intentionalFallThrough(SwitchStatementTree switchStatement, CaseGroupTree caseGroup) {
    FallThroughCommentVisitor visitor = new FallThroughCommentVisitor();
    // Check first token of next case group when comment is last element of case group it is attached to next group.
    CaseGroupTree nextCaseGroup = switchStatement.cases().get(switchStatement.cases().indexOf(caseGroup) + 1);
    List<Tree> treesToScan = ImmutableList.<Tree>builder().addAll(caseGroup.body()).add(nextCaseGroup.firstToken()).build();
    visitor.scan(treesToScan);
    return visitor.hasComment;
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree)

Example 4 with CaseGroupTree

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

the class SwitchAtLeastThreeCasesCheck method visitSwitchStatement.

@Override
public void visitSwitchStatement(SwitchStatementTree tree) {
    int count = 0;
    for (CaseGroupTree caseGroup : tree.cases()) {
        count += caseGroup.labels().size();
    }
    if (count < 3) {
        context.reportIssue(this, tree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability.");
    }
    super.visitSwitchStatement(tree);
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree)

Example 5 with CaseGroupTree

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

the class SwitchWithLabelsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CaseGroupTree cgt = (CaseGroupTree) tree;
    for (StatementTree statementTree : cgt.body()) {
        if (statementTree.is(LABELED_STATEMENT)) {
            IdentifierTree label = ((LabeledStatementTree) statementTree).label();
            reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
        }
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree)

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