use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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.MethodInvocationTree in project sonar-java by SonarSource.
the class DuplicateArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
Arguments arguments = mit.arguments();
int arity = arguments.size();
if (arity <= 1) {
return;
}
Set<ExpressionTree> reported = new HashSet<>();
for (int i = 0; i < arity; i++) {
ExpressionTree arg = ExpressionUtils.skipParentheses(arguments.get(i));
if (isLiteral(arg) || arg.is(Tree.Kind.IDENTIFIER) || arg.is(Tree.Kind.NEW_CLASS)) {
continue;
}
for (int j = i + 1; j < arity; j++) {
ExpressionTree otherArg = ExpressionUtils.skipParentheses(arguments.get(j));
if (!reported.contains(otherArg) && SyntacticEquivalence.areEquivalent(arg, otherArg)) {
reportIssue(otherArg, String.format("Verify that this is the intended value; it is the same as the %s argument.", argumentNumber(i + 1)), Collections.singletonList(new JavaFileScannerContext.Location("", arg)), null);
reported.add(otherArg);
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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.MethodInvocationTree in project sonar-java by SonarSource.
the class WeakSSLContextCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
Arguments arguments = mit.arguments();
if (SSLCONTEXT_GETINSTANCE_MATCHER.matches(mit)) {
ExpressionTree firstArgument = arguments.get(0);
if (firstArgument.is(Tree.Kind.STRING_LITERAL) && !STRONG_PROTOCOLS.contains(trimQuotes(((LiteralTree) firstArgument).value()))) {
reportIssue(firstArgument, "Change this code to use a stronger protocol.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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));
}
}
}
}
Aggregations