Search in sources :

Example 1 with SwitchStatementTree

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

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

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

the class SwitchCaseTooBigCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
    LinesOfCodeVisitor locVisitor = new LinesOfCodeVisitor();
    switchStatementTree.cases().forEach(cgt -> {
        int lines = cgt.body().stream().mapToInt(locVisitor::linesOfCode).sum();
        if (lines > max) {
            reportIssue(cgt.labels().get(cgt.labels().size() - 1), "Reduce this switch case number of lines from " + lines + " to at most " + max + ", for example by extracting code into methods.");
        }
    });
}
Also used : LinesOfCodeVisitor(org.sonar.java.ast.visitors.LinesOfCodeVisitor) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree)

Example 4 with SwitchStatementTree

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

the class SwitchDefaultLastCaseCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
    getDefaultLabelAtWrongPosition(switchStatementTree).ifPresent(defaultLabel -> reportIssue(defaultLabel, "Move this default to the end of the switch."));
}
Also used : SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree)

Example 5 with SwitchStatementTree

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

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