private void checkIfLombokClass(ImportTree tree) { String importStr = fullQualifiedName(tree.qualifiedIdentifier()); if (importStr.contains("lombok")) { lombokClass = true; } }
@Override public void visitNode(Tree tree) { ImportTree importTree = (ImportTree) tree; // See RSPEC-2208 : exception with static imports. if (fullQualifiedName(importTree.qualifiedIdentifier()).endsWith(".*") && !importTree.isStatic()) { reportIssue(importTree.qualifiedIdentifier(), "Explicitly import the specific classes needed."); } }
private void resolveImports(List<ImportTree> imports) { ImportResolverVisitor importResolverVisitor = new ImportResolverVisitor(); for (ImportTree importTree : imports) { importTree.accept(importResolverVisitor); } }
@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); } }
public void ignoreLines(CompilationUnitTree tree) { if (!tree.imports().isEmpty()) { int start = ((InternalSyntaxToken) tree.imports().get(0).importKeyword()).getLine(); int end = ((InternalSyntaxToken) tree.imports().get(tree.imports().size() - 1).semicolonToken()).getLine(); for (int i = start; i <= end; i++) { ignoredLines.add(i); } } }
@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); } }
private static int getLine(ImportClauseTree importClauseTree, boolean fromStart) { if (importClauseTree.is(Tree.Kind.IMPORT)) { if (fromStart) { return ((ImportTree) importClauseTree).importKeyword().line(); } else { return ((ImportTree) importClauseTree).semicolonToken().line(); } } return ((EmptyStatementTree) importClauseTree).semicolonToken().line(); }
@Override public void visitImport(ImportTree tree) { scan(tree.qualifiedIdentifier()); }
@Override public void visitNode(Tree tree) { ImportTree importTree = (ImportTree) tree; // See RSPEC-2208 : exception with static imports. if (fullQualifiedName(importTree.qualifiedIdentifier()).endsWith(".*") && !importTree.isStatic()) { reportIssue(importTree.qualifiedIdentifier(), "Explicitly import the specific classes needed."); } }
@Override public void visitImport(ImportTree tree) { //reset currentSymbol to default package currentSymbol = symbols.defaultPackage; isStatic = tree.isStatic(); super.visitImport(tree); //Associate symbol only if found. if (currentSymbol.kind < JavaSymbol.ERRONEOUS) { enterSymbol(currentSymbol, tree); } else { if (isStatic) { for (JavaSymbol symbol : resolved) { //add only static fields //TODO accessibility should be checked : package/public if ((symbol.flags & Flags.STATIC) != 0) { //TODO only the first symbol found will be associated with the tree. enterSymbol(symbol, tree); } } } } }
private static int getLine(ImportClauseTree importClauseTree, boolean fromStart) { if (importClauseTree.is(Tree.Kind.IMPORT)) { if (fromStart) { return ((ImportTree) importClauseTree).importKeyword().line(); } else { return ((ImportTree) importClauseTree).semicolonToken().line(); } } return ((EmptyStatementTree) importClauseTree).semicolonToken().line(); }
@Override public void visitImport(ImportTree tree) { scan(tree.qualifiedIdentifier()); }
@Override public void visitImport(ImportTree tree) { //reset currentSymbol to default package currentSymbol = symbols.defaultPackage; isStatic = tree.isStatic(); tree.qualifiedIdentifier().accept(this); //Associate symbol only if found. if (currentSymbol.kind < JavaSymbol.ERRONEOUS) { enterSymbol(currentSymbol, tree); } else if (isStatic) { resolved.stream() //add only static fields //TODO accessibility should be checked : package/public .filter(symbol -> Flags.isFlagged(symbol.flags, Flags.STATIC)) //TODO only the first symbol found will be associated with the tree. .forEach(symbol -> enterSymbol(symbol, tree)); } }
@Override public void visitImport(ImportTree tree) { //reset currentSymbol to default package currentSymbol = symbols.defaultPackage; isStatic = tree.isStatic(); super.visitImport(tree); //Associate symbol only if found. if (currentSymbol.kind < JavaSymbol.ERRONEOUS) { enterSymbol(currentSymbol, tree); } else { if (isStatic) { for (JavaSymbol symbol : resolved) { //add only static fields //TODO accessibility should be checked : package/public if ((symbol.flags & Flags.STATIC) != 0) { //TODO only the first symbol found will be associated with the tree. enterSymbol(symbol, tree); } } } } }
@Override public void visitImport(ImportTree tree) { scan(tree.qualifiedIdentifier()); }
@Override public void visitImport(ImportTree tree) { //reset currentSymbol to default package currentSymbol = symbols.defaultPackage; isStatic = tree.isStatic(); tree.qualifiedIdentifier().accept(this); //Associate symbol only if found. if (currentSymbol.kind < JavaSymbol.ERRONEOUS) { enterSymbol(currentSymbol, tree); } else if (isStatic) { resolved.stream() //add only static fields //TODO accessibility should be checked : package/public .filter(symbol -> Flags.isFlagged(symbol.flags, Flags.STATIC)) //TODO only the first symbol found will be associated with the tree. .forEach(symbol -> enterSymbol(symbol, tree)); } }
if (importTree == null || importTree.isStatic()) {
@Override public void visitImport(ImportTree tree) { scan(tree.qualifiedIdentifier()); }
currentPackage = concatenate(cut.packageName()); for (ImportTree importTree : cut.imports()) { if (!importTree.isStatic()) { String importName = concatenate((ExpressionTree) importTree.qualifiedIdentifier()); if ("java.lang.*".equals(importName)) { context.addIssue(importTree, this, "Remove this unnecessary import: java.lang classes are always implicitly imported.");
if (importTree == null || importTree.isStatic()) {