use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class DefaultEncodingUsageCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!excluded.contains(tree)) {
super.visitNode(tree);
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
boolean foundIssue = checkForbiddenTypes(variableTree.simpleName(), variableTree.type().symbolType());
if (foundIssue) {
excluded.add(variableTree.initializer());
}
} else if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
checkForbiddenTypes(ExpressionUtils.methodName(mit), mit.symbolType());
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class AbsOnNegativeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) tree;
if (MATH_ABS_METHODS.anyMatch(methodTree)) {
ExpressionTree firstArgument = methodTree.arguments().get(0);
checkForIssue(firstArgument);
}
} else {
ExpressionTree operand = ((UnaryExpressionTree) tree).expression();
checkForIssue(operand);
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method Lambdas.
@Test
public void Lambdas() throws Exception {
Result result = Result.createFor("Lambdas");
JavaSymbol barMethod = result.symbol("bar");
assertThat(barMethod.usages()).hasSize(1);
MethodTree methodTree = (MethodTree) barMethod.declaration();
Type Ftype = result.symbol("F").type();
assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
JavaSymbol qixMethod = result.symbol("qix");
LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
JavaSymbol fieldSymbol = result.symbol("field");
assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
JavaSymbol bSymbol = result.symbol("b");
assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
JavaSymbol condMethod = result.symbol("cond");
ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
JavaSymbol parenthMethod = result.symbol("parenth");
ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
JavaSymbol sym = result.symbol("o");
assertThat(sym.type.is("java.lang.Object")).isTrue();
assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
JavaSymbol operations = result.symbol("operations");
MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
JavaSymbol myStringParam = result.symbol("myStringParam");
Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
assertThat(stringParamMethod.usages()).hasSize(1);
assertThat(myStringParam.type.is("java.lang.String")).isTrue();
assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
assertThat(result.symbol("foo", 95).usages()).hasSize(1);
assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method symbolNotFound.
@Test
public void symbolNotFound() throws Exception {
Result result = Result.createFor("SymbolsNotFound");
MethodTree methodDeclaration = (MethodTree) result.symbol("method").declaration();
ExpressionStatementTree expression = (ExpressionStatementTree) methodDeclaration.block().body().get(0);
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression.expression();
Symbol symbolNotFound = methodInvocation.symbol();
assertThat(symbolNotFound).isNotNull();
assertThat(symbolNotFound.name()).isNull();
assertThat(symbolNotFound.owner()).isSameAs(Symbols.unknownSymbol);
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class BehaviorCacheTest method test_peek.
@Test
public void test_peek() throws Exception {
Set<String> testedPre = new HashSet<>();
Set<String> testedPost = new HashSet<>();
SECheck check = new SECheck() {
@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
assertThat(peekMethodBehavior).isNull();
testedPre.add(symbol.name());
}
return context.getState();
}
@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
String methodName = symbol.name();
MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
assertThat(peekMethodBehavior).isNotNull();
if ("foo".equals(methodName) || "isBlank".equals(methodName)) {
// foo should have been computed
assertThat(peekMethodBehavior.isComplete()).isTrue();
} else if ("bar".equals(methodName)) {
assertThat(peekMethodBehavior.isComplete()).isFalse();
}
testedPost.add(methodName);
}
return super.checkPostStatement(context, syntaxNode);
}
};
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/BehaviorCachePeek.java", check);
assertThat(sev.behaviorCache.peek("org.apache.commons.lang.StringUtils#isBlank(Ljava/lang/String;)Z").isComplete()).isTrue();
assertThat(sev.behaviorCache.peek("org.foo.A#foo()Z").isComplete()).isTrue();
assertThat(sev.behaviorCache.peek("org.foo.A#bar()Z").isComplete()).isFalse();
assertThat(sev.behaviorCache.peek("org.foo.A#unknownMethod()Z")).isNull();
assertThat(sev.behaviorCache.behaviors.keySet()).containsOnly("org.foo.A#foo()Z");
assertThat(testedPre).containsOnly("foo", "bar", "isBlank");
assertThat(testedPost).containsOnly("foo", "bar", "isBlank");
}
Aggregations