Search in sources :

Example 86 with CompilationUnitTree

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

the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.

@Test
public void type_hierarchy_visit_should_be_limited() {
    ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
    Symbols symbols = new Symbols(bytecodeCompleter);
    ActionParser<Tree> parser = JavaParser.createParser();
    SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
    Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
    TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
    CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
    new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
    typeAndReferenceSolver.visitCompilationUnit(tree);
    ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
    JavaType site = (JavaType) classTree.symbol().type();
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
    TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
    // call with empty formals should return.
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
    verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
    JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
    verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) File(java.io.File) Test(org.junit.Test)

Example 87 with CompilationUnitTree

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

the class UselessImportCheck method scanFile.

@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    CompilationUnitTree cut = context.getTree();
    if (cut.moduleDeclaration() != null) {
        // imports can be used for types used in module directive or annotations
        return;
    }
    ExpressionTree packageName = getPackageName(cut);
    pendingReferences.clear();
    lineByImportReference.clear();
    pendingImports.clear();
    currentPackage = ExpressionsHelper.concatenate(packageName);
    for (ImportClauseTree importClauseTree : cut.imports()) {
        ImportTree importTree = null;
        if (importClauseTree.is(Tree.Kind.IMPORT)) {
            importTree = (ImportTree) importClauseTree;
        }
        if (importTree == null || importTree.isStatic()) {
            // discard empty statements, which can be part of imports
            continue;
        }
        reportIssue(importTree);
    }
    // check references
    scan(cut);
    // check references from comments.
    new CommentVisitor().checkImportsFromComments(cut, pendingImports);
    leaveFile();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree)

Example 88 with CompilationUnitTree

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

the class GenericsTest method declaredTypesOfVariablesFromLastClass.

private static List<Type> declaredTypesOfVariablesFromLastClass(String... lines) {
    CompilationUnitTree tree = treeOf(lines);
    List<Tree> declaredClasses = tree.types();
    Tree last = declaredClasses.get(declaredClasses.size() - 1);
    if (!(last instanceof ClassTree)) {
        return Collections.emptyList();
    }
    ClassTree testClass = (ClassTree) last;
    List<Type> types = new ArrayList<>();
    for (Tree member : testClass.members()) {
        if (member instanceof VariableTree) {
            types.add(((VariableTree) member).type().symbolType());
        }
    }
    return types;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Type(org.sonar.plugins.java.api.semantic.Type) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 89 with CompilationUnitTree

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

the class GenericsTest method declaredTypes.

private static List<Type> declaredTypes(String... lines) {
    CompilationUnitTree tree = treeOf(lines);
    List<Type> results = Lists.newLinkedList();
    for (Tree classTree : tree.types()) {
        Type type = ((ClassTree) classTree).symbol().type();
        results.add(type);
    }
    return results;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Type(org.sonar.plugins.java.api.semantic.Type) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 90 with CompilationUnitTree

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

the class RequiresDirectiveTreeImplTest method test_BaseTreeVisitor.

@Test
public void test_BaseTreeVisitor() {
    CompilationUnitTree cut = createTree("@org.foo.Bar", "open module com.greetings {", "  requires static transitive bos.gul;", "  requires gla.qix;", "}");
    RequiresDirectiveVisitor moduleDeclarationVisitor = new RequiresDirectiveVisitor();
    cut.accept(moduleDeclarationVisitor);
    assertThat(moduleDeclarationVisitor.visited).isTrue();
    assertThat(moduleDeclarationVisitor.directives).hasSize(2);
    assertThat(moduleDeclarationVisitor.identifiers).containsExactly("com", "greetings", "bos", "gul", "gla", "qix");
}
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