Search in sources :

Example 26 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge 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

VisitorsBridge (org.sonar.java.model.VisitorsBridge)26 File (java.io.File)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)11 RecognitionException (com.sonar.sslr.api.RecognitionException)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 SonarComponents (org.sonar.java.SonarComponents)4 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 Measurer (org.sonar.java.Measurer)2 SubscriptionVisitor (org.sonar.java.ast.visitors.SubscriptionVisitor)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 InterruptedIOException (java.io.InterruptedIOException)1 HashMap (java.util.HashMap)1 BeforeClass (org.junit.BeforeClass)1 SensorContext (org.sonar.api.batch.SensorContext)1 InputPath (org.sonar.api.batch.fs.InputPath)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1