Search in sources :

Example 1 with IfStatementTree

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

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

the class CFG method buildIfStatement.

private void buildIfStatement(IfStatementTree ifStatementTree) {
    Block next = currentBlock;
    // process else-branch
    Block elseBlock = next;
    StatementTree elseStatement = ifStatementTree.elseStatement();
    if (elseStatement != null) {
        // if statement will create the required block.
        if (!elseStatement.is(Tree.Kind.IF_STATEMENT)) {
            currentBlock = createBlock(next);
        }
        build(elseStatement);
        elseBlock = currentBlock;
    }
    // process then-branch
    currentBlock = createBlock(next);
    build(ifStatementTree.thenStatement());
    Block thenBlock = currentBlock;
    // process condition
    currentBlock = createBranch(ifStatementTree, thenBlock, elseBlock);
    buildCondition(ifStatementTree.condition(), thenBlock, elseBlock);
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree)

Example 3 with IfStatementTree

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

the class MapComputeIfAbsentOrPresentCheck method isInsideIfStatementWithNullCheckWithoutElse.

private static boolean isInsideIfStatementWithNullCheckWithoutElse(MethodInvocationTree mit) {
    Tree parent = mit.parent();
    while (parent != null && !parent.is(Tree.Kind.IF_STATEMENT)) {
        parent = parent.parent();
    }
    if (parent == null) {
        return false;
    }
    IfStatementTree ifStatementTree = (IfStatementTree) parent;
    return ifStatementTree.elseStatement() == null && isNullCheck(ExpressionUtils.skipParentheses(ifStatementTree.condition()));
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 4 with IfStatementTree

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

the class NestedIfStatementsCheck method visit.

private void visit(IfStatementTree tree) {
    scan(tree.condition());
    scan(tree.thenStatement());
    StatementTree elseStatementTree = tree.elseStatement();
    if (elseStatementTree != null && elseStatementTree.is(Tree.Kind.IF_STATEMENT)) {
        visit((IfStatementTree) elseStatementTree);
    } else {
        scan(elseStatementTree);
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree)

Example 5 with IfStatementTree

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

the class ReturnOfBooleanExpressionsCheck method getStatementTree.

private static StatementTree getStatementTree(IfStatementTree ifStatementTree) {
    StatementTree elseStatementOrNextStatement = ifStatementTree.elseStatement();
    if (elseStatementOrNextStatement == null) {
        JavaTree parent = (JavaTree) ifStatementTree.parent();
        List<Tree> children = parent.getChildren();
        int indexOfIf = children.indexOf(ifStatementTree);
        if (indexOfIf < children.size() - 1) {
            // Defensive, this condition should always be true as if necessarily followed by a statement or a token.
            Tree next = children.get(indexOfIf + 1);
            if (!next.is(Kind.TOKEN)) {
                elseStatementOrNextStatement = (StatementTree) next;
            }
        }
    }
    return elseStatementOrNextStatement;
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) JavaTree(org.sonar.java.model.JavaTree)

Aggregations

IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)19 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)11 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)5 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)5 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 Test (org.junit.Test)2 JavaTree (org.sonar.java.model.JavaTree)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)2 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)2 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2