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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations