Search in sources :

Example 26 with BinaryExpressionTree

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

the class ExplodedGraphWalker method handleBlockExit.

private void handleBlockExit(ProgramPoint programPosition) {
    CFG.Block block = (CFG.Block) programPosition.block;
    Tree terminator = block.terminator();
    cleanUpProgramState(block);
    boolean exitPath = node.exitPath;
    if (terminator != null) {
        switch(terminator.kind()) {
            case IF_STATEMENT:
                ExpressionTree ifCondition = ((IfStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(ifCondition), verifyCondition(ifCondition));
                return;
            case CONDITIONAL_OR:
            case CONDITIONAL_AND:
                handleBranch(block, ((BinaryExpressionTree) terminator).leftOperand());
                return;
            case CONDITIONAL_EXPRESSION:
                handleBranch(block, ((ConditionalExpressionTree) terminator).condition());
                return;
            case FOR_STATEMENT:
                ExpressionTree condition = ((ForStatementTree) terminator).condition();
                if (condition != null) {
                    handleBranch(block, condition, false);
                    return;
                }
                break;
            case WHILE_STATEMENT:
                ExpressionTree whileCondition = ((WhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(whileCondition), verifyCondition(whileCondition));
                return;
            case DO_STATEMENT:
                ExpressionTree doCondition = ((DoWhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(doCondition), verifyCondition(doCondition));
                return;
            case SYNCHRONIZED_STATEMENT:
                resetFieldValues(false);
                break;
            case RETURN_STATEMENT:
                ExpressionTree returnExpression = ((ReturnStatementTree) terminator).expression();
                if (returnExpression != null) {
                    programState.storeExitValue();
                }
                break;
            case THROW_STATEMENT:
                ProgramState.Pop unstack = programState.unstackValue(1);
                SymbolicValue sv = unstack.values.get(0);
                if (sv instanceof SymbolicValue.CaughtExceptionSymbolicValue) {
                    // retrowing the exception from a catch block
                    sv = ((SymbolicValue.CaughtExceptionSymbolicValue) sv).exception();
                } else {
                    sv = constraintManager.createExceptionalSymbolicValue(((ThrowStatementTree) terminator).expression().symbolType());
                }
                programState = unstack.state.stackValue(sv);
                programState.storeExitValue();
                break;
            default:
        }
    }
    // unconditional jumps, for-statement, switch-statement, synchronized:
    if (exitPath) {
        if (block.exitBlock() != null) {
            enqueue(new ProgramPoint(block.exitBlock()), programState, true);
        } else {
            for (CFG.Block successor : block.successors()) {
                enqueue(new ProgramPoint(successor), programState, true);
            }
        }
    } else {
        for (CFG.Block successor : block.successors()) {
            if (!block.isFinallyBlock() || isDirectFlowSuccessorOf(successor, block)) {
                enqueue(new ProgramPoint(successor), programState, successor == block.exitBlock());
            }
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) CFG(org.sonar.java.cfg.CFG) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 27 with BinaryExpressionTree

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

the class MapComputeIfAbsentOrPresentCheck method isNullCheck.

private static boolean isNullCheck(ExpressionTree condition) {
    if (condition.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
        BinaryExpressionTree bet = (BinaryExpressionTree) condition;
        ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
        ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
        return rightOperand.is(Tree.Kind.NULL_LITERAL) || leftOperand.is(Tree.Kind.NULL_LITERAL);
    }
    return false;
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 28 with BinaryExpressionTree

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

the class InvalidDateValuesCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    super.visitNode(tree);
    if (hasSemantic() && tree.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
        BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
        String name = getThresholdToCheck(binaryExpressionTree.leftOperand());
        ExpressionTree argToCheck = null;
        if (name == null) {
            name = getThresholdToCheck(binaryExpressionTree.rightOperand());
            if (name != null) {
                argToCheck = binaryExpressionTree.leftOperand();
            }
        } else {
            argToCheck = binaryExpressionTree.rightOperand();
        }
        if (argToCheck != null) {
            checkArgument(argToCheck, name, "\"{0}\" is not a valid value for \"{1}\".");
        }
    }
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 29 with BinaryExpressionTree

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

the class NullCheckWithInstanceofCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    BinaryExpressionTree binaryExpression = (BinaryExpressionTree) tree;
    ExpressionTree leftOp = ExpressionUtils.skipParentheses(binaryExpression.leftOperand());
    ExpressionTree rightOp = ExpressionUtils.skipParentheses(binaryExpression.rightOperand());
    if ((is(Tree.Kind.EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_OR)) || (is(Tree.Kind.NOT_EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_AND))) {
        reportIssue(treeToReport(leftOp, rightOp), "Remove this unnecessary null check; \"instanceof\" returns false for nulls.");
    }
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 30 with BinaryExpressionTree

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

the class NullShouldNotBeUsedWithOptionalCheck method visitBinaryExpression.

@Override
public void visitBinaryExpression(BinaryExpressionTree binaryExpression) {
    // check that an @Optional is not compared to "null"
    if (binaryExpression.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
        ExpressionTree left = binaryExpression.leftOperand();
        ExpressionTree right = binaryExpression.rightOperand();
        if ((isOptional(left) && isNull(right)) || (isNull(left) && isOptional(right))) {
            context.reportIssue(this, binaryExpression, "Remove this null-check of an \"Optional\".");
        }
    }
    super.visitBinaryExpression(binaryExpression);
}
Also used : ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Aggregations

BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)40 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)24 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 Test (org.junit.Test)10 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)9 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)9 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)5 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)4 Tree (org.sonar.plugins.java.api.tree.Tree)4 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)3 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)3 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)2 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)2