Search in sources :

Example 36 with BinaryExpressionTree

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

the class UnusedReturnedDataCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
    } else {
        BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
        ExpressionTree leftOperand = expressionTree.leftOperand();
        ExpressionTree rightOperand = expressionTree.rightOperand();
        for (MethodMatcher matcher : CHECKED_METHODS) {
            MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
            if (leftMit != null && isTreeLiteralNull(rightOperand)) {
                raiseIssue(ExpressionUtils.methodName(leftMit));
            }
            MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
            if (rightMit != null && isTreeLiteralNull(leftOperand)) {
                raiseIssue(ExpressionUtils.methodName(rightMit));
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 37 with BinaryExpressionTree

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

the class DoubleCheckedLockingCheck method isIfFieldEqNull.

private static Optional<IfFieldEqNull> isIfFieldEqNull(Tree tree) {
    if (!tree.is(IF_STATEMENT)) {
        return Optional.empty();
    }
    IfStatementTree ifTree = (IfStatementTree) tree;
    if (!ifTree.condition().is(EQUAL_TO)) {
        return Optional.empty();
    }
    BinaryExpressionTree eqRelation = (BinaryExpressionTree) ifTree.condition();
    if (eqRelation.rightOperand().is(NULL_LITERAL)) {
        return isField(eqRelation.leftOperand()).map(f -> new IfFieldEqNull(ifTree, f));
    }
    if (eqRelation.leftOperand().is(NULL_LITERAL)) {
        return isField(eqRelation.rightOperand()).map(f -> new IfFieldEqNull(ifTree, f));
    }
    return Optional.empty();
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 38 with BinaryExpressionTree

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

the class UCFGJavaVisitor method buildCall.

private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
    if (isStringVarDecl(element)) {
        VariableTree variableTree = (VariableTree) element;
        String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
        if (!idGenerator.isConst(lhs)) {
            ExpressionTree initializer = variableTree.initializer();
            String source = idGenerator.lookupIdFor(initializer);
            blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
        }
        return;
    }
    if (element.is(METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
        buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
    } else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
        if (element.is(PLUS)) {
            BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
            Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
            Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
            Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
            blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
        } else if (element.is(PLUS_ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                idGenerator.varForExpression(element, ((Expression.Variable) var).id());
                blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
            }
        } else if (element.is(ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
            }
        }
    }
}
Also used : Expression(org.sonar.ucfg.Expression) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) 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) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 39 with BinaryExpressionTree

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

the class CognitiveComplexityVisitor method visitBinaryExpression.

@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
    if (tree.is(CONDITIONAL_AND, CONDITIONAL_OR) && !ignored.contains(tree)) {
        List<BinaryExpressionTree> flattenedLogicalExpressions = flattenLogicalExpression(tree).collect(Collectors.toList());
        BinaryExpressionTree previous = null;
        for (BinaryExpressionTree current : flattenedLogicalExpressions) {
            if (previous == null || !previous.is(current.kind())) {
                increaseComplexityByOne(current.operatorToken());
            }
            previous = current;
        }
    }
    super.visitBinaryExpression(tree);
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 40 with BinaryExpressionTree

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

the class CognitiveComplexityVisitor method flattenLogicalExpression.

private Stream<BinaryExpressionTree> flattenLogicalExpression(ExpressionTree expression) {
    if (expression.is(CONDITIONAL_AND, CONDITIONAL_OR)) {
        ignored.add(expression);
        BinaryExpressionTree binaryExpr = (BinaryExpressionTree) expression;
        ExpressionTree left = ExpressionUtils.skipParentheses(binaryExpr.leftOperand());
        ExpressionTree right = ExpressionUtils.skipParentheses(binaryExpr.rightOperand());
        return Stream.concat(Stream.concat(flattenLogicalExpression(left), Stream.of(binaryExpr)), flattenLogicalExpression(right));
    }
    return Stream.empty();
}
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) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

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