use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class AbstractInjectionChecker method isDynamicString.
protected boolean isDynamicString(Tree methodTree, ExpressionTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) {
if (arg.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) arg;
IdentifierTree identifier = memberSelectExpressionTree.identifier();
if (ExpressionUtils.isSelectOnThisOrSuper(memberSelectExpressionTree)) {
return isIdentifierDynamicString(methodTree, identifier, currentlyChecking, firstLevel);
}
return !isConstant(identifier.symbol());
} else if (arg.is(Tree.Kind.IDENTIFIER)) {
return isIdentifierDynamicString(methodTree, (IdentifierTree) arg, currentlyChecking, firstLevel);
} else if (arg.is(Tree.Kind.PLUS)) {
BinaryExpressionTree binaryArg = (BinaryExpressionTree) arg;
return isDynamicString(methodTree, binaryArg.rightOperand(), currentlyChecking) || isDynamicString(methodTree, binaryArg.leftOperand(), currentlyChecking);
} else if (arg.is(Tree.Kind.METHOD_INVOCATION)) {
return false;
}
return !arg.is(Tree.Kind.STRING_LITERAL, Tree.Kind.NULL_LITERAL);
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class ForLoopTerminationConditionCheck method visitForStatement.
@Override
public void visitForStatement(ForStatementTree forStatement) {
ExpressionTree condition = forStatement.condition();
if (condition == null || !condition.is(Tree.Kind.NOT_EQUAL_TO)) {
return;
}
BinaryExpressionTree inequalityCondition = (BinaryExpressionTree) condition;
IntInequality loopVarAndTerminalValue = IntInequality.of(inequalityCondition);
if (loopVarAndTerminalValue != null) {
IdentifierTree loopIdentifier = loopVarAndTerminalValue.identifier;
int terminationValue = loopVarAndTerminalValue.literalValue;
Integer initialValue = initialValue(loopIdentifier, forStatement);
if (initialValue != null && initialValue != terminationValue) {
checkIncrement(forStatement, loopIdentifier, initialValue < terminationValue);
}
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class CastArithmeticOperandCheck method checkIntegerDivisionInsideFloatingPointExpression.
private boolean checkIntegerDivisionInsideFloatingPointExpression(BinaryExpressionTree integerDivision) {
Tree parent = integerDivision.parent();
while (parent instanceof ExpressionTree) {
ExpressionTree expressionTree = (ExpressionTree) parent;
if (isFloatingPoint(expressionTree.symbolType())) {
context.reportIssue(this, integerDivision, "Cast one of the operands of this integer division to a \"double\".");
return false;
}
parent = expressionTree.parent();
}
return true;
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class CastArithmeticOperandCheck method checkExpression.
private void checkExpression(Type varType, @Nullable ExpressionTree expr) {
if (isVarTypeErrorProne(varType) && expr != null && expressionIsOperationToIntOrLong(expr)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) expr;
if (binaryExpressionTree.is(Tree.Kind.DIVIDE) && varType.isPrimitive(Type.Primitives.LONG)) {
// widening the result of an int division is harmless
return;
}
if (varType.isPrimitive(Type.Primitives.LONG) && expr.symbolType().isPrimitive(Type.Primitives.LONG)) {
return;
}
context.reportIssue(this, binaryExpressionTree.operatorToken(), "Cast one of the operands of this " + OPERATION_BY_KIND.get(expr.kind()) + " operation to a \"" + varType.name() + "\".");
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class ExplodedGraphWalkerTest method binary_expression_creates_not_null_value.
@Test
public void binary_expression_creates_not_null_value() throws Exception {
int[] counter = new int[1];
SECheck check = new SECheck() {
@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
ProgramState state = context.getState();
if (syntaxNode instanceof BinaryExpressionTree) {
SymbolicValue sv = state.peekValue();
assertThat(state.getConstraint(sv, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
counter[0]++;
}
return state;
}
};
JavaCheckVerifier.verifyNoIssue("src/test/files/se/BinaryTreeExecution.java", check);
assertThat(counter[0]).isEqualTo(17);
}
Aggregations