Search in sources :

Example 31 with BinaryExpressionTree

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

the class CFG method buildBinaryExpression.

private void buildBinaryExpression(Tree tree) {
    BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
    currentBlock.elements.add(tree);
    build(binaryExpressionTree.rightOperand());
    build(binaryExpressionTree.leftOperand());
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 32 with BinaryExpressionTree

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

the class ExpressionUtilsTest method test_skip_parenthesis.

@Test
public void test_skip_parenthesis() throws Exception {
    File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
    ExpressionTree parenthesis = ((ReturnStatementTree) methodTree.block().body().get(0)).expression();
    assertThat(parenthesis.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
    ExpressionTree skipped = ExpressionUtils.skipParentheses(parenthesis);
    assertThat(skipped.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
    assertThat(ExpressionUtils.skipParentheses(((BinaryExpressionTree) skipped).leftOperand()).is(Tree.Kind.IDENTIFIER)).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) File(java.io.File) Test(org.junit.Test)

Example 33 with BinaryExpressionTree

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

the class IdenticalOperandOnBinaryExpressionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        checkEqualsMethods(mit);
        return;
    }
    BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
    ExpressionTree rightOperand = binaryExpressionTree.rightOperand();
    ExpressionTree equivalentOperand = equivalentOperand(binaryExpressionTree, rightOperand);
    if (equivalentOperand != null) {
        reportIssue(rightOperand, "Correct one of the identical sub-expressions on both sides of operator \"" + binaryExpressionTree.operatorToken().text() + "\"", ImmutableList.of(new JavaFileScannerContext.Location("", equivalentOperand)), null);
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) 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 34 with BinaryExpressionTree

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

the class IndexOfStartPositionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree leftOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).leftOperand());
    ExpressionTree rightOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).rightOperand());
    if (leftOperand.is(Tree.Kind.METHOD_INVOCATION)) {
        checkIndexOfInvocation((MethodInvocationTree) leftOperand, rightOperand);
    } else if (rightOperand.is(Tree.Kind.METHOD_INVOCATION)) {
        checkIndexOfInvocation((MethodInvocationTree) rightOperand, leftOperand);
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 35 with BinaryExpressionTree

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

the class ForLoopFalseConditionCheck method isConditionFalseAtInitialization.

private static boolean isConditionFalseAtInitialization(ForStatementTree forStatement) {
    Iterable<ForLoopInitializer> initializers = ForLoopInitializer.list(forStatement);
    ExpressionTree condition = forStatement.condition();
    if (!condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO, Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) {
        return false;
    }
    BinaryExpressionTree binaryCondition = (BinaryExpressionTree) condition;
    Integer leftOperand = eval(binaryCondition.leftOperand(), initializers);
    Integer rightOperand = eval(binaryCondition.rightOperand(), initializers);
    if (leftOperand != null && rightOperand != null) {
        return !evaluateCondition(condition, leftOperand, rightOperand);
    }
    return false;
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

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