use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodJavaSymbolTest method test_throws.
@Test
public void test_throws() {
File bytecodeDir = new File("target/test-classes");
JavaAstScanner.scanSingleFileForTests(new File("src/test/java/org/sonar/java/resolve/targets/MethodThrowingExceptionsUsage.java"), new VisitorsBridge(Collections.singleton(new SubscriptionVisitor() {
@Override
public List<Tree.Kind> nodesToVisit() {
return Lists.newArrayList(Tree.Kind.METHOD_INVOCATION);
}
@Override
public void visitNode(Tree tree) {
Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) ((MethodInvocationTree) tree).symbol();
List<Type> thrownTypes = methodSymbol.thrownTypes();
assertThat(thrownTypes).hasSize(2);
if ("test".equals(methodSymbol.name())) {
// FIXME substitution should be done : see SONARJAVA-1778
assertThat(((JavaType) thrownTypes.get(0)).isTagged(JavaType.TYPEVAR)).isTrue();
} else {
assertThat(thrownTypes.get(0).is("java.sql.SQLException")).isTrue();
}
assertThat(thrownTypes.get(1).is("java.io.IOException")).isTrue();
}
}), Lists.newArrayList(bytecodeDir), null));
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodReferenceResolutionTest method method_references_type_defered_should_not_raise_npe.
@Test
public void method_references_type_defered_should_not_raise_npe() throws Exception {
Result result = Result.createFor("MethodReferencesDeferedType");
LambdaExpressionTree lambda = (LambdaExpressionTree) result.symbol("qualifier").declaration().parent();
Type symbolType = ((MethodInvocationTree) lambda.body()).symbolType();
assertThat(symbolType.is("java.util.stream.Stream")).isTrue();
assertThat(symbolType).isInstanceOf(ParametrizedTypeJavaType.class);
List<JavaType> substitutedTypes = ((ParametrizedTypeJavaType) symbolType).typeSubstitution.substitutedTypes();
assertThat(substitutedTypes).hasSize(1);
assertThat(substitutedTypes.get(0).is("Qualifier")).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodReferenceResolutionTest method MethodReferencesNoArguments.
@Test
public void MethodReferencesNoArguments() throws Exception {
Result result = Result.createFor("MethodReferencesNoArguments");
JavaSymbol isTrue = result.symbol("isTrue");
assertThat(isTrue.usages()).hasSize(1);
JavaSymbol isFalse = result.symbol("isFalse");
assertThat(isFalse.usages()).hasSize(1);
JavaSymbol up = result.symbol("up");
assertThat(up.usages()).hasSize(1);
Tree upMethodRef = up.usages().get(0).parent();
MethodInvocationTree map = (MethodInvocationTree) upMethodRef.parent().parent();
JavaType mapType = (JavaType) map.symbolType();
assertThat(mapType.is("java.util.stream.Stream")).isTrue();
assertThat(mapType.isParameterized()).isTrue();
List<JavaType> substitutedTypes = ((ParametrizedTypeJavaType) mapType).typeSubstitution.substitutedTypes();
assertThat(substitutedTypes).hasSize(1);
assertThat(substitutedTypes.get(0).is("A$B")).isTrue();
JavaSymbol bool = result.symbol("bool", 28);
assertThat(bool.usages().stream().map(id -> id.identifierToken().line()).collect(Collectors.toList())).containsExactly(11, 12, 13);
bool = result.symbol("bool", 29);
assertThat(bool.usages().stream().map(id -> id.identifierToken().line()).collect(Collectors.toList())).containsExactly(14, 15);
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodReferenceResolutionTest method MethodReferenceWithStream.
@Test
public void MethodReferenceWithStream() throws Exception {
Result result = Result.createFor("MethodReferencesStream");
JavaSymbol flatipus1 = result.symbol("flatipus1");
assertThat(flatipus1.usages()).hasSize(1);
MethodInvocationTree flatMap = (MethodInvocationTree) flatipus1.usages().get(0).parent().parent().parent();
Type symbolType = flatMap.symbolType();
assertThat(symbolType.is("java.util.Optional")).isTrue();
JavaSymbol flatipus2 = result.symbol("flatipus2");
assertThat(flatipus2.usages()).hasSize(1);
JavaSymbol bool = result.symbol("bool");
assertThat(bool.usages()).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodReferenceResolutionTest method MethodReferenceWithArrayNew.
@Test
public void MethodReferenceWithArrayNew() throws Exception {
Result result = Result.createFor("MethodReferencesArrayNew");
JavaSymbol bar = result.symbol("bar");
assertThat(bar.usages()).hasSize(1);
MethodInvocationTree callingBar = (MethodInvocationTree) bar.usages().get(0).parent();
MethodInvocationTree toArray = (MethodInvocationTree) callingBar.arguments().get(0);
assertThat(toArray.symbolType().is("B[][]")).isTrue();
JavaSymbol bool = result.symbol("bool");
assertThat(bool.usages()).hasSize(1);
}
Aggregations