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