use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class CollectionSizeAndArrayLengthCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
BinaryExpressionTree bet = (BinaryExpressionTree) tree;
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
boolean leftIsZero = isZero(leftOperand);
boolean rightIsZero = isZero(rightOperand);
if (!leftIsZero && !rightIsZero) {
return;
}
ExpressionTree testedValue = leftIsZero ? rightOperand : leftOperand;
if (testedValue.is(Tree.Kind.METHOD_INVOCATION)) {
checkCollectionSize((MethodInvocationTree) testedValue, bet, leftIsZero);
} else if (testedValue.is(Tree.Kind.MEMBER_SELECT)) {
checkArrayLength((MemberSelectExpressionTree) testedValue, bet, leftIsZero);
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class ConcatenationWithStringValueOfCheck method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
if (!tree.is(Kind.PLUS)) {
super.visitBinaryExpression(tree);
return;
}
Set<ExpressionTree> valueOfTrees = Sets.newHashSet();
boolean flagIssue = false;
ExpressionTree current = tree;
while (current.is(Kind.PLUS)) {
BinaryExpressionTree binOp = (BinaryExpressionTree) current;
scan(binOp.rightOperand());
if (isStringValueOf(binOp.rightOperand())) {
valueOfTrees.add(binOp.rightOperand());
}
flagIssue |= binOp.leftOperand().is(Kind.STRING_LITERAL);
if (!valueOfTrees.isEmpty()) {
flagIssue |= binOp.rightOperand().is(Kind.STRING_LITERAL);
}
current = ((BinaryExpressionTree) current).leftOperand();
}
if (flagIssue) {
for (ExpressionTree valueOfTree : valueOfTrees) {
context.reportIssue(this, valueOfTree, "Directly append the argument of String.valueOf().");
}
}
scan(current);
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class SillyBitOperationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ExpressionTree expression;
SyntaxToken operatorToken;
if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
BinaryExpressionTree binary = (BinaryExpressionTree) tree;
expression = binary.rightOperand();
operatorToken = binary.operatorToken();
} else {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
expression = assignment.expression();
operatorToken = assignment.operatorToken();
}
Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
reportIssue(operatorToken, "Remove this silly bit operation.");
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.
private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
ExpressionTree arg = null;
ExpressionTree expression = null;
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
Symbol symbol = mit.symbol();
ExpressionTree methodSelect = mit.methodSelect();
if (mit.arguments().size() == 1) {
arg = mit.arguments().get(0);
if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
expression = ((MemberSelectExpressionTree) methodSelect).expression();
}
}
} else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
arg = equalTo.leftOperand();
expression = equalTo.rightOperand();
}
if (arg != null && expression != null) {
if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
} else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class ShiftOnIntOrLongCheck method checkShiftTree.
private void checkShiftTree(Tree tree, int treeIndex) {
String identifier;
ExpressionTree shift;
SyntaxToken operatorToken;
if (tree.is(Kind.LEFT_SHIFT, Kind.RIGHT_SHIFT)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
if (isZeroMaskShift(binaryExpressionTree)) {
// No issue should be reported for "1 << 0" or "1 >> 0"
return;
}
identifier = getIdentifierName(binaryExpressionTree.leftOperand());
shift = binaryExpressionTree.rightOperand();
operatorToken = binaryExpressionTree.operatorToken();
} else {
AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
identifier = getIdentifierName(assignmentExpressionTree.variable());
shift = assignmentExpressionTree.expression();
operatorToken = assignmentExpressionTree.operatorToken();
}
checkShift((ExpressionTree) tree, shift, identifier, operatorToken, treeIndex);
}
Aggregations