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