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