use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class UnusedReturnedDataCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
} else {
BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
ExpressionTree leftOperand = expressionTree.leftOperand();
ExpressionTree rightOperand = expressionTree.rightOperand();
for (MethodMatcher matcher : CHECKED_METHODS) {
MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
if (leftMit != null && isTreeLiteralNull(rightOperand)) {
raiseIssue(ExpressionUtils.methodName(leftMit));
}
MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
if (rightMit != null && isTreeLiteralNull(leftOperand)) {
raiseIssue(ExpressionUtils.methodName(rightMit));
}
}
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class DoubleCheckedLockingCheck method isIfFieldEqNull.
private static Optional<IfFieldEqNull> isIfFieldEqNull(Tree tree) {
if (!tree.is(IF_STATEMENT)) {
return Optional.empty();
}
IfStatementTree ifTree = (IfStatementTree) tree;
if (!ifTree.condition().is(EQUAL_TO)) {
return Optional.empty();
}
BinaryExpressionTree eqRelation = (BinaryExpressionTree) ifTree.condition();
if (eqRelation.rightOperand().is(NULL_LITERAL)) {
return isField(eqRelation.leftOperand()).map(f -> new IfFieldEqNull(ifTree, f));
}
if (eqRelation.leftOperand().is(NULL_LITERAL)) {
return isField(eqRelation.rightOperand()).map(f -> new IfFieldEqNull(ifTree, f));
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildCall.
private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
if (isStringVarDecl(element)) {
VariableTree variableTree = (VariableTree) element;
String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
if (!idGenerator.isConst(lhs)) {
ExpressionTree initializer = variableTree.initializer();
String source = idGenerator.lookupIdFor(initializer);
blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
}
return;
}
if (element.is(METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
} else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
if (element.is(PLUS)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
} else if (element.is(PLUS_ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
idGenerator.varForExpression(element, ((Expression.Variable) var).id());
blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
}
} else if (element.is(ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
}
}
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class CognitiveComplexityVisitor method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
if (tree.is(CONDITIONAL_AND, CONDITIONAL_OR) && !ignored.contains(tree)) {
List<BinaryExpressionTree> flattenedLogicalExpressions = flattenLogicalExpression(tree).collect(Collectors.toList());
BinaryExpressionTree previous = null;
for (BinaryExpressionTree current : flattenedLogicalExpressions) {
if (previous == null || !previous.is(current.kind())) {
increaseComplexityByOne(current.operatorToken());
}
previous = current;
}
}
super.visitBinaryExpression(tree);
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class CognitiveComplexityVisitor method flattenLogicalExpression.
private Stream<BinaryExpressionTree> flattenLogicalExpression(ExpressionTree expression) {
if (expression.is(CONDITIONAL_AND, CONDITIONAL_OR)) {
ignored.add(expression);
BinaryExpressionTree binaryExpr = (BinaryExpressionTree) expression;
ExpressionTree left = ExpressionUtils.skipParentheses(binaryExpr.leftOperand());
ExpressionTree right = ExpressionUtils.skipParentheses(binaryExpr.rightOperand());
return Stream.concat(Stream.concat(flattenLogicalExpression(left), Stream.of(binaryExpr)), flattenLogicalExpression(right));
}
return Stream.empty();
}
Aggregations