Search in sources :

Example 1 with SubscriptionVisitor

use of org.sonar.java.ast.visitors.SubscriptionVisitor 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)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 List (java.util.List)1 Test (org.junit.Test)1 SubscriptionVisitor (org.sonar.java.ast.visitors.SubscriptionVisitor)1 JavaTree (org.sonar.java.model.JavaTree)1 VisitorsBridge (org.sonar.java.model.VisitorsBridge)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1