Search in sources :

Example 16 with BinaryExpressionTree

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

the class CollectionSizeAndArrayLengthCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    BinaryExpressionTree bet = (BinaryExpressionTree) tree;
    ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
    ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
    boolean leftIsZero = isZero(leftOperand);
    boolean rightIsZero = isZero(rightOperand);
    if (!leftIsZero && !rightIsZero) {
        return;
    }
    ExpressionTree testedValue = leftIsZero ? rightOperand : leftOperand;
    if (testedValue.is(Tree.Kind.METHOD_INVOCATION)) {
        checkCollectionSize((MethodInvocationTree) testedValue, bet, leftIsZero);
    } else if (testedValue.is(Tree.Kind.MEMBER_SELECT)) {
        checkArrayLength((MemberSelectExpressionTree) testedValue, bet, leftIsZero);
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 17 with BinaryExpressionTree

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

the class ConcatenationWithStringValueOfCheck method visitBinaryExpression.

@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
    if (!tree.is(Kind.PLUS)) {
        super.visitBinaryExpression(tree);
        return;
    }
    Set<ExpressionTree> valueOfTrees = Sets.newHashSet();
    boolean flagIssue = false;
    ExpressionTree current = tree;
    while (current.is(Kind.PLUS)) {
        BinaryExpressionTree binOp = (BinaryExpressionTree) current;
        scan(binOp.rightOperand());
        if (isStringValueOf(binOp.rightOperand())) {
            valueOfTrees.add(binOp.rightOperand());
        }
        flagIssue |= binOp.leftOperand().is(Kind.STRING_LITERAL);
        if (!valueOfTrees.isEmpty()) {
            flagIssue |= binOp.rightOperand().is(Kind.STRING_LITERAL);
        }
        current = ((BinaryExpressionTree) current).leftOperand();
    }
    if (flagIssue) {
        for (ExpressionTree valueOfTree : valueOfTrees) {
            context.reportIssue(this, valueOfTree, "Directly append the argument of String.valueOf().");
        }
    }
    scan(current);
}
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) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 18 with BinaryExpressionTree

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

the class SillyBitOperationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expression;
    SyntaxToken operatorToken;
    if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
        BinaryExpressionTree binary = (BinaryExpressionTree) tree;
        expression = binary.rightOperand();
        operatorToken = binary.operatorToken();
    } else {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
        expression = assignment.expression();
        operatorToken = assignment.operatorToken();
    }
    Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
    if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
        reportIssue(operatorToken, "Remove this silly bit operation.");
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 19 with BinaryExpressionTree

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

the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.

private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
    ExpressionTree arg = null;
    ExpressionTree expression = null;
    if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
        Symbol symbol = mit.symbol();
        ExpressionTree methodSelect = mit.methodSelect();
        if (mit.arguments().size() == 1) {
            arg = mit.arguments().get(0);
            if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
                expression = ((MemberSelectExpressionTree) methodSelect).expression();
            }
        }
    } else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
        arg = equalTo.leftOperand();
        expression = equalTo.rightOperand();
    }
    if (arg != null && expression != null) {
        if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
            return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
        } else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
            return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
        }
    }
    return Optional.empty();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 20 with BinaryExpressionTree

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

the class ShiftOnIntOrLongCheck method checkShiftTree.

private void checkShiftTree(Tree tree, int treeIndex) {
    String identifier;
    ExpressionTree shift;
    SyntaxToken operatorToken;
    if (tree.is(Kind.LEFT_SHIFT, Kind.RIGHT_SHIFT)) {
        BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
        if (isZeroMaskShift(binaryExpressionTree)) {
            // No issue should be reported for "1 << 0" or "1 >> 0"
            return;
        }
        identifier = getIdentifierName(binaryExpressionTree.leftOperand());
        shift = binaryExpressionTree.rightOperand();
        operatorToken = binaryExpressionTree.operatorToken();
    } else {
        AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
        identifier = getIdentifierName(assignmentExpressionTree.variable());
        shift = assignmentExpressionTree.expression();
        operatorToken = assignmentExpressionTree.operatorToken();
    }
    checkShift((ExpressionTree) tree, shift, identifier, operatorToken, treeIndex);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

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