Search in sources :

Example 1 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class VisitorsBridge method visitFile.

public void visitFile(@Nullable Tree parsedTree) {
    semanticModel = null;
    CompilationUnitTree tree = new JavaTree.CompilationUnitTreeImpl(null, new ArrayList<>(), new ArrayList<>(), null, null);
    boolean fileParsed = parsedTree != null;
    if (fileParsed && parsedTree.is(Tree.Kind.COMPILATION_UNIT)) {
        tree = (CompilationUnitTree) parsedTree;
        if (isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
            try {
                semanticModel = SemanticModel.createFor(tree, classLoader);
            } catch (Exception e) {
                LOG.error("Unable to create symbol table for : " + currentFile.getAbsolutePath(), e);
                addAnalysisError(e, currentFile.getPath(), AnalysisError.Kind.SEMANTIC_ERROR);
                return;
            }
            createSonarSymbolTable(tree);
        } else {
            SemanticModel.handleMissingTypes(tree);
        }
    }
    JavaFileScannerContext javaFileScannerContext = createScannerContext(tree, semanticModel, sonarComponents, fileParsed);
    // Symbolic execution checks
    if (symbolicExecutionEnabled && isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
        runScanner(javaFileScannerContext, new SymbolicExecutionVisitor(executableScanners, behaviorCache), AnalysisError.Kind.SE_ERROR);
        behaviorCache.cleanup();
    }
    executableScanners.forEach(scanner -> runScanner(javaFileScannerContext, scanner, AnalysisError.Kind.CHECK_ERROR));
    if (semanticModel != null) {
        classesNotFound.addAll(semanticModel.classesNotFound());
    }
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) IllegalRuleParameterException(org.sonar.java.IllegalRuleParameterException) InterruptedIOException(java.io.InterruptedIOException) RecognitionException(com.sonar.sslr.api.RecognitionException)

Example 2 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class ExportsDirectiveTreeImplTest method exportsDirective.

private ExportsDirectiveTree exportsDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ExportsDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExportsDirectiveTree(org.sonar.plugins.java.api.tree.ExportsDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 3 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class ExportsDirectiveTreeImplTest method createTree.

private CompilationUnitTree createTree(String... lines) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(Arrays.stream(lines).collect(Collectors.joining("\n")));
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return compilationUnitTree;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 4 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class ProvidesDirectiveTreeImplTest method providesDirective.

private ProvidesDirectiveTree providesDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ProvidesDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ProvidesDirectiveTree(org.sonar.plugins.java.api.tree.ProvidesDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 5 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class UsesDirectiveTreeImplTest method test_BaseTreeVisitor.

@Test
public void test_BaseTreeVisitor() {
    CompilationUnitTree cut = createTree("open module com.greetings {", "  uses org.bar.MyInterface;", "  uses org.gul.MyOtherInterface;", "}");
    UsesDirectiveVisitor moduleDeclarationVisitor = new UsesDirectiveVisitor();
    cut.accept(moduleDeclarationVisitor);
    assertThat(moduleDeclarationVisitor.visited).isTrue();
    assertThat(moduleDeclarationVisitor.directives).hasSize(2);
    assertThat(moduleDeclarationVisitor.identifiers).containsExactly("com", "greetings", "MyInterface", "MyOtherInterface");
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Test(org.junit.Test)

Aggregations

CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)94 Test (org.junit.Test)46 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)42 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)35 File (java.io.File)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)25 Tree (org.sonar.plugins.java.api.tree.Tree)20 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)9 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)9 List (java.util.List)8 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)8 Type (org.sonar.plugins.java.api.semantic.Type)8 SemanticModel (org.sonar.java.resolve.SemanticModel)7 Collectors (java.util.stream.Collectors)6 Symbol (org.sonar.plugins.java.api.semantic.Symbol)6 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)6 ArrayList (java.util.ArrayList)5 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)5