Search in sources :

Example 11 with BinaryExpressionTree

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

the class ModulusEqualityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equality = (BinaryExpressionTree) tree;
        checkModulusAndIntLiteral(equality.leftOperand(), equality.rightOperand());
        checkModulusAndIntLiteral(equality.rightOperand(), equality.leftOperand());
    } else {
        MethodTree methodTree = (MethodTree) tree;
        for (VariableTree variableTree : methodTree.parameters()) {
            methodParams.add(variableTree.symbol());
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 12 with BinaryExpressionTree

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

the class ModulusEqualityCheck method checkModulusAndIntLiteral.

private void checkModulusAndIntLiteral(ExpressionTree operand1, ExpressionTree operand2) {
    if (operand1.is(Tree.Kind.REMAINDER)) {
        BinaryExpressionTree modulusExp = (BinaryExpressionTree) operand1;
        Integer intValue = LiteralUtils.intLiteralValue(operand2);
        ExpressionTree leftOperand = modulusExp.leftOperand();
        ExpressionTree rightOperand = modulusExp.rightOperand();
        boolean usesMethodParam = isMethodParameter(leftOperand) || isMethodParameter(rightOperand);
        boolean usesSize = isSizeAccessor(leftOperand) || isSizeAccessor(rightOperand);
        if (intValue != null && intValue != 0 && usesMethodParam && !usesSize) {
            String sign = intValue > 0 ? "positive" : "negative";
            reportIssue(modulusExp.operatorToken(), "The results of this modulus operation may not be " + sign + ".");
        }
    }
}
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 13 with BinaryExpressionTree

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

the class RawByteBitwiseOperationsCheck method visitBinaryExpression.

@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
    super.visitBinaryExpression(tree);
    if (isShifting(tree)) {
        shifts.add(tree);
        return;
    }
    if (isSecuringByte(tree)) {
        byteContainments.add(tree);
        return;
    }
    if (isIntegerOrLongExpected(tree.symbolType())) {
        ExpressionTree leftOperand = ExpressionUtils.skipParentheses(tree.leftOperand());
        ExpressionTree rightOperand = ExpressionUtils.skipParentheses(tree.rightOperand());
        checkShiftWithoutByteSecuring(leftOperand, rightOperand);
        checkShiftWithoutByteSecuring(rightOperand, leftOperand);
    }
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 14 with BinaryExpressionTree

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

the class NonShortCircuitLogicCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
    if (isBoolean(binaryExpressionTree.leftOperand().symbolType())) {
        String operator = binaryExpressionTree.operatorToken().text();
        String replacement = REPLACEMENTS.get(operator);
        reportIssue(binaryExpressionTree.operatorToken(), "Correct this \"" + operator + "\" to \"" + replacement + "\".");
    }
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 15 with BinaryExpressionTree

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

the class CollectionSizeAndArrayLengthCheck method checkArrayLength.

private void checkArrayLength(MemberSelectExpressionTree testedValue, BinaryExpressionTree bet, boolean leftIsZero) {
    if (!"length".equals(testedValue.identifier().name())) {
        return;
    }
    ExpressionTree expression = testedValue.expression();
    if (!expression.symbolType().isArray()) {
        return;
    }
    reportIssue(bet, leftIsZero, ARRAY_ISSUE_MSG, arrayName(expression));
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) 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