Search in sources :

Example 21 with BinaryExpressionTree

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

the class ReplaceLambdaByMethodRefCheck method getNullCheck.

private static Optional<String> getNullCheck(@Nullable Tree statement, LambdaExpressionTree tree) {
    if (statement == null) {
        return Optional.empty();
    }
    Tree expr = statement;
    if (expr.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
        expr = ExpressionUtils.skipParentheses((ParenthesizedTree) statement);
    }
    if (expr.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
        BinaryExpressionTree bet = (BinaryExpressionTree) expr;
        ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
        ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
        if (nullAgainstParam(leftOperand, rightOperand, tree) || nullAgainstParam(rightOperand, leftOperand, tree)) {
            return Optional.of(expr.is(Tree.Kind.EQUAL_TO) ? "isNull" : "nonNull");
        }
    }
    return Optional.empty();
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

Example 22 with BinaryExpressionTree

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

the class JavaTreeModelTest method equality_expression.

/**
 * 15.21. Equality Operators
 */
@Test
public void equality_expression() {
    String code = "class T { boolean m() { return false == false != true; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.NOT_EQUAL_TO)).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.EQUAL_TO)).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 23 with BinaryExpressionTree

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

the class JavaTreeModelTest method bitwise_and_logical_operators.

/**
 * 15.22. Bitwise and Logical Operators
 */
@Test
public void bitwise_and_logical_operators() {
    String code = "class T { int m() { return 1 & 2 & 3; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.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.AND)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("&");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    code = "class T { int m() { return 1 ^ 2 ^ 3; } }";
    tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.XOR)).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.XOR)).isTrue();
    assertThat(tree.leftOperand()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("^");
    assertThat(tree.rightOperand()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    code = "class T { int m() { return 1 | 2 | 3; } }";
    tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.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.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 24 with BinaryExpressionTree

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

the class JavaTreeModelTest method relational_expression.

/**
 * 15.20. Relational Operators
 */
@Test
public void relational_expression() {
    String code = "class T { boolean m() { return 1 < 2 > 3; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.GREATER_THAN)).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.LESS_THAN)).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 25 with BinaryExpressionTree

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

the class JavaTreeModelTest method multiplicative_expression.

/**
 * 15.17. Multiplicative Operators
 */
@Test
public void multiplicative_expression() {
    String code = "class T { int m() { return 1 * 2 / 3 % 4; } }";
    BinaryExpressionTree tree = (BinaryExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Kind.REMAINDER)).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.DIVIDE)).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.MULTIPLY)).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)

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