Search in sources :

Example 6 with BinaryExpressionTree

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

the class JavaTreeModelTest method additive_expression.

/**
 * 15.18. Additive Operators
 */
@Test
public void additive_expression() {
    String code = "class T { int m() { return 1 + 2 - 3; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Kind.MINUS)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("-");
    assertThat(tree.rightOperand()).isNotNull();
    assertThat(tree.rightOperand().is(Kind.INT_LITERAL)).isTrue();
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (BinaryExpressionTree) tree.leftOperand();
    assertThat(tree.is(Kind.PLUS)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("+");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Test(org.junit.Test)

Example 7 with BinaryExpressionTree

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

the class JavaTreeModelTest method shift_expression.

/**
 * 15.19. Shift Operators
 */
@Test
public void shift_expression() {
    String code = "class T { int m() { return 1 >> 2 << 3 >>> 4; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.UNSIGNED_RIGHT_SHIFT)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo(">>>");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (BinaryExpressionTree) tree.leftOperand();
    assertThat(tree.is(Tree.Kind.LEFT_SHIFT)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("<<");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (BinaryExpressionTree) tree.leftOperand();
    assertThat(tree.is(Kind.RIGHT_SHIFT)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo(">>");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Test(org.junit.Test)

Example 8 with BinaryExpressionTree

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

the class JavaTreeModelTest method conditional_and_expression.

/**
 * 15.23. Conditional-And Operator &&
 */
@Test
public void conditional_and_expression() {
    String code = "class T { boolean m() { return false && false && true; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("&&");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (BinaryExpressionTree) tree.leftOperand();
    assertThat(tree.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("&&");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Test(org.junit.Test)

Example 9 with BinaryExpressionTree

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

the class JavaTreeModelTest method conditional_or_expression.

/**
 * 15.24. Conditional-Or Operator ||
 */
@Test
public void conditional_or_expression() {
    String code = "class T { boolean m() { return false || false || true; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.CONDITIONAL_OR)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("||");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (BinaryExpressionTree) tree.leftOperand();
    assertThat(tree.is(Tree.Kind.CONDITIONAL_OR)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("||");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Test(org.junit.Test)

Example 10 with BinaryExpressionTree

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

the class NullDereferenceInConditionalCheck method visitBinaryExpression.

@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
    if (isAndWithNullComparison(tree) || isOrWithNullExclusion(tree)) {
        ExpressionTree nonNullOperand = getNonNullOperand(tree.leftOperand());
        IdentifierTree identifierTree = getIdentifier(nonNullOperand);
        if (identifierTree != null) {
            IdentifierVisitor visitor = new IdentifierVisitor(identifierTree);
            tree.rightOperand().accept(visitor);
            if (visitor.raiseIssue) {
                context.reportIssue(this, tree, "Either reverse the equality operator in the \"" + identifierTree.name() + "\" null test, or reverse the logical operator that follows it.");
            }
        }
    }
    super.visitBinaryExpression(tree);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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