use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodInvocationTreeImplTest method symbol_should_be_set.
@Test
public void symbol_should_be_set() {
CompilationUnitTree cut = createTree("class A { void foo(){} void bar(){foo();} }");
ClassTree classTree = (ClassTree) cut.types().get(0);
Symbol.MethodSymbol declaration = ((MethodTree) classTree.members().get(0)).symbol();
StatementTree statementTree = ((MethodTree) classTree.members().get(1)).block().body().get(0);
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
assertThat(mit.symbol()).isSameAs(declaration);
assertThat(mit.arguments()).isNotNull();
assertThat(mit.arguments().openParenToken()).isNotNull();
assertThat(mit.arguments().closeParenToken()).isNotNull();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Symbol sym;
Arguments args;
Tree methodName;
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
sym = nct.constructorSymbol();
args = nct.arguments();
methodName = nct.identifier();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
sym = mit.symbol();
args = mit.arguments();
methodName = mit.methodSelect();
}
if (sym.isMethodSymbol() && !args.isEmpty()) {
ExpressionTree lastArg = args.get(args.size() - 1);
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
MethodJavaType methodType = getMethodType(methodSymbol, methodName);
checkInvokedMethod(methodSymbol, methodType, lastArg);
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method targetType.
@CheckForNull
private static Type targetType(TypeCastTree tree) {
Tree parent = skipParentheses(tree.parent());
Type target = null;
if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
Tree method = parent;
while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
method = method.parent();
}
target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
} else if (parent.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) parent;
target = variableTree.symbol().type();
} else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) parent;
if (mit.symbol().isMethodSymbol()) {
JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
int castArgIndex = mit.arguments().indexOf(tree);
target = sym.parameterTypes().get(castArgIndex);
}
} else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
target = tree.type().symbolType();
} else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
} else if (parent instanceof ExpressionTree) {
target = ((ExpressionTree) parent).symbolType();
}
return target;
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isMapPut.
@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
MethodInvocationTree mapPut = (MethodInvocationTree) expression;
Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
ExpressionTree keyTree = mapPut.arguments().get(0);
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, false, null);
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class OSCommandInjectionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
Arguments arguments = mit.arguments();
if (RUNTIME_EXEC_MATCHER.matches(mit)) {
checkForIssue(tree, arguments.get(0));
} else if (PROCESS_BUILDER_COMMAND_MATCHER.matches(mit) && !arguments.isEmpty()) {
checkForIssue(tree, arguments);
}
} else if (((NewClassTree) tree).symbolType().is("java.lang.ProcessBuilder")) {
checkForIssue(tree, ((NewClassTree) tree).arguments());
}
}
}
Aggregations