Search in sources :

Example 1 with ImportTree

use of org.sonar.plugins.java.api.tree.ImportTree 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 2 with ImportTree

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

the class SimpleClassNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
    List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
    boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
    if (!fileContainsStarImport) {
        checkImports(imports);
    }
}
Also used : Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) Predicate(java.util.function.Predicate) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree)

Example 3 with ImportTree

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

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

ImportTree (org.sonar.plugins.java.api.tree.ImportTree)4 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)3 ImportClauseTree (org.sonar.plugins.java.api.tree.ImportClauseTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Rule (org.sonar.check.Rule)2 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 Objects (java.util.Objects)1 Predicate (java.util.function.Predicate)1 Test (org.junit.Test)1 RuleProperty (org.sonar.check.RuleProperty)1 SemanticModel (org.sonar.java.resolve.SemanticModel)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1