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);
}
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);
}
}
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);
}
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();
}
Aggregations