Search in sources :

Example 1 with ImportClauseTree

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

the class TreeFactory method newCompilationUnit.

// End of literals
// Compilation unit
public CompilationUnitTreeImpl newCompilationUnit(JavaTree spacing, Optional<PackageDeclarationTree> packageDeclaration, Optional<List<ImportClauseTree>> importDeclarations, Optional<ModuleDeclarationTree> moduleDeclaration, Optional<List<Tree>> typeDeclarations, InternalSyntaxToken eof) {
    ImmutableList.Builder<ImportClauseTree> imports = ImmutableList.builder();
    if (importDeclarations.isPresent()) {
        for (ImportClauseTree child : importDeclarations.get()) {
            imports.add(child);
        }
    }
    ImmutableList.Builder<Tree> types = ImmutableList.builder();
    if (typeDeclarations.isPresent()) {
        for (Tree child : typeDeclarations.get()) {
            types.add(child);
        }
    }
    return new CompilationUnitTreeImpl(packageDeclaration.orNull(), imports.build(), types.build(), moduleDeclaration.orNull(), eof);
}
Also used : CompilationUnitTreeImpl(org.sonar.java.model.JavaTree.CompilationUnitTreeImpl) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ImmutableList(com.google.common.collect.ImmutableList) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ListTree(org.sonar.plugins.java.api.tree.ListTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) ModuleDirectiveTree(org.sonar.plugins.java.api.tree.ModuleDirectiveTree) ModuleDeclarationTree(org.sonar.plugins.java.api.tree.ModuleDeclarationTree)

Example 2 with ImportClauseTree

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

the class StaticImportCountCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    List<ImportClauseTree> staticImports = cut.imports().stream().filter(importClauseTree -> importClauseTree.is(Tree.Kind.IMPORT) && ((ImportTree) importClauseTree).isStatic()).collect(Collectors.toList());
    int staticImportsCount = staticImports.size();
    if (staticImportsCount > threshold) {
        List<JavaFileScannerContext.Location> flow = staticImports.stream().map(importStatement -> new JavaFileScannerContext.Location("+1", importStatement)).collect(Collectors.toList());
        String message = String.format("Reduce the number of \"static\" imports in this class from %d to the maximum allowed %d.", staticImportsCount, threshold);
        reportIssue(staticImports.get(0), message, flow, null);
    }
}
Also used : List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) RuleProperty(org.sonar.check.RuleProperty) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree)

Example 3 with ImportClauseTree

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

the class JavaTreeModelTest method import_declaration.

@Test
public void import_declaration() {
    ImportClauseTree tree = compilationUnit(";").imports().get(0);
    assertThat(tree.is(Kind.EMPTY_STATEMENT)).isTrue();
    assertThat(tree.is(Kind.IMPORT)).isFalse();
    tree = compilationUnit("import foo.Bar;").imports().get(0);
    assertThat(tree.is(Kind.IMPORT)).isTrue();
    ImportTree importTree = (ImportTree) tree;
    assertThat(importTree.isStatic()).isFalse();
    assertThat(importTree.qualifiedIdentifier()).isNotNull();
    assertThatChildrenIteratorHasSize(importTree, 3);
    tree = compilationUnit("import foo.bar.*;").imports().get(0);
    assertThat(tree.is(Kind.IMPORT)).isTrue();
    importTree = (ImportTree) tree;
    assertThat(importTree.isStatic()).isFalse();
    assertThat(importTree.qualifiedIdentifier()).isNotNull();
    assertThatChildrenIteratorHasSize(importTree, 3);
    tree = compilationUnit("import static foo.Bar.method;").imports().get(0);
    assertThat(tree.is(Kind.IMPORT)).isTrue();
    importTree = (ImportTree) tree;
    assertThat(importTree.isStatic()).isTrue();
    assertThat(importTree.qualifiedIdentifier()).isNotNull();
    assertThatChildrenIteratorHasSize(importTree, 4);
    tree = compilationUnit("import static foo.Bar.*;").imports().get(0);
    assertThat(tree.is(Kind.IMPORT)).isTrue();
    importTree = (ImportTree) tree;
    assertThat(importTree.isStatic()).isTrue();
    assertThat(importTree.qualifiedIdentifier()).isNotNull();
    assertThatChildrenIteratorHasSize(importTree, 4);
}
Also used : ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) Test(org.junit.Test)

Example 4 with ImportClauseTree

use of org.sonar.plugins.java.api.tree.ImportClauseTree 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)

Aggregations

ImportClauseTree (org.sonar.plugins.java.api.tree.ImportClauseTree)4 ImportTree (org.sonar.plugins.java.api.tree.ImportTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Rule (org.sonar.check.Rule)1 RuleProperty (org.sonar.check.RuleProperty)1 JavaTree (org.sonar.java.model.JavaTree)1 CompilationUnitTreeImpl (org.sonar.java.model.JavaTree.CompilationUnitTreeImpl)1 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 ListTree (org.sonar.plugins.java.api.tree.ListTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1