Search in sources :

Example 81 with MethodInvocationTree

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));
}
Also used : SubscriptionVisitor(org.sonar.java.ast.visitors.SubscriptionVisitor) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VisitorsBridge(org.sonar.java.model.VisitorsBridge) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File) Test(org.junit.Test)

Example 82 with MethodInvocationTree

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();
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Example 83 with MethodInvocationTree

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);
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) List(java.util.List) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 84 with MethodInvocationTree

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);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Example 85 with MethodInvocationTree

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);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7