protected SortedMap<Integer, Node> orderedCommentsAndDeclarations(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.getComments()); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTFieldDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTMethodDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTConstructorDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTEnumDeclaration.class, true)); return itemsByLineNumber; }
private boolean isTestNgClass(ASTCompilationUnit node) { List<ASTImportDeclaration> imports = node.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration i : imports) { if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) { return true; } } return false; }
private boolean hasImports(ASTCompilationUnit cu, String className) { List<ASTImportDeclaration> imports = cu.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : imports) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(className)) { return true; } } return false; } }
/** * Determines all the class/interface declarations inside this compilation * unit, which implement Cloneable * * @param currentClass * the node of the class, that is currently analyzed (inside this * compilation unit) * @return a Set of class/interface names */ private Set<String> determineTopLevelCloneableClasses(final ASTClassOrInterfaceDeclaration currentClass) { final List<ASTClassOrInterfaceDeclaration> classes = currentClass.getFirstParentOfType(ASTCompilationUnit.class) .findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); final Set<String> classesNames = new HashSet<>(); for (final ASTClassOrInterfaceDeclaration c : classes) { if (!Objects.equals(c, currentClass) && extendsOrImplementsCloneable(c)) { classesNames.add(c.getImage()); } } return classesNames; }
/** * Checks whether the type this node is referring to is declared within the * same compilation unit - either a class/interface or a enum type. You want * to check this, if {@link #getType()} is null. * * @return <code>true</code> if this node referencing a type in the same * compilation unit, <code>false</code> otherwise. */ public boolean isReferenceToClassSameCompilationUnit() { ASTCompilationUnit root = getFirstParentOfType(ASTCompilationUnit.class); for (ASTClassOrInterfaceDeclaration c : root.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true)) { if (c.hasImageEqualTo(getImage())) { return true; } } for (ASTEnumDeclaration e : root.findDescendantsOfType(ASTEnumDeclaration.class, true)) { if (e.hasImageEqualTo(getImage())) { return true; } } return false; }
/** * check contains POJO * @param node compilation unit * @return */ private boolean hasPojoInJavaFile(ASTCompilationUnit node) { List<ASTClassOrInterfaceDeclaration> klasses = node.findDescendantsOfType( ASTClassOrInterfaceDeclaration.class); for (ASTClassOrInterfaceDeclaration klass : klasses) { if (isPojo(klass)) { return true; } } return false; }
protected SortedMap<Integer, Node> orderedCommentsAndDeclarations(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.getComments()); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTFieldDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTMethodDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTConstructorDeclaration.class, true)); addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTEnumDeclaration.class, true)); return itemsByLineNumber; }
private boolean isTestNgClass(ASTCompilationUnit node) { List<ASTImportDeclaration> imports = node.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration i : imports) { if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) { return true; } } return false; }
protected SortedMap<Integer, Node> orderedComments(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments()); List<ASTAnnotation> annotations = cUnit.findDescendantsOfType(ASTAnnotation.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, annotations); List<ASTClassOrInterfaceDeclaration> classDecl = cUnit.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, classDecl); List<ASTFieldDeclaration> fields = cUnit.findDescendantsOfType(ASTFieldDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fields); List<ASTMethodDeclaration> methods = cUnit.findDescendantsOfType(ASTMethodDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, methods); List<ASTConstructorDeclaration> constructors = cUnit.findDescendantsOfType(ASTConstructorDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, constructors); List<ASTEnumDeclaration> enumDecl = cUnit.findDescendantsOfType(ASTEnumDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl); return itemsByLineNumber; }
private SortedMap<Integer, Node> orderedCommentsAndEnumDeclarations(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); List<ASTEnumDeclaration> enumDecl = cUnit.findDescendantsOfType(ASTEnumDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl); List<ASTEnumConstant> contantDecl = cUnit.findDescendantsOfType(ASTEnumConstant.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, contantDecl); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments()); return itemsByLineNumber; }
@Override protected SortedMap<Integer, Node> orderedCommentsAndDeclarations(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); List<ASTImportDeclaration> importDecl = cUnit .findDescendantsOfType(ASTImportDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, importDecl); List<ASTClassOrInterfaceDeclaration> classDecl = cUnit .findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, classDecl); List<ASTFieldDeclaration> fields = cUnit.findDescendantsOfType(ASTFieldDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fields); List<ASTMethodDeclaration> methods = cUnit.findDescendantsOfType(ASTMethodDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, methods); List<ASTConstructorDeclaration> constructors = cUnit.findDescendantsOfType(ASTConstructorDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, constructors); List<ASTBlockStatement> blockStatements = cUnit.findDescendantsOfType(ASTBlockStatement.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, blockStatements); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments()); return itemsByLineNumber; }
private boolean hasImports(ASTCompilationUnit cu, String className) { List<ASTImportDeclaration> imports = cu.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : imports) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(className)) { return true; } } return false; } }
/** * Check comments behind nodes. * * @param cUnit compilation unit * @return sorted comments and expressions */ protected SortedMap<Integer, Node> orderedCommentsAndExpressions(ASTCompilationUnit cUnit) { SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>(); // expression nodes List<ASTExpression> expressionNodes = cUnit.findDescendantsOfType(ASTExpression.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, expressionNodes); // filed declaration nodes List<ASTFieldDeclaration> fieldNodes = cUnit.findDescendantsOfType(ASTFieldDeclaration.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fieldNodes); // enum constant nodes List<ASTEnumConstant> enumConstantNodes = cUnit.findDescendantsOfType(ASTEnumConstant.class); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumConstantNodes); NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments()); return itemsByLineNumber; }
/** * Determines all the class/interface declarations inside this compilation * unit, which implement Cloneable * * @param currentClass * the node of the class, that is currently analyzed (inside this * compilation unit) * @return a Set of class/interface names */ private Set<String> determineTopLevelCloneableClasses(final ASTClassOrInterfaceDeclaration currentClass) { final List<ASTClassOrInterfaceDeclaration> classes = currentClass.getFirstParentOfType(ASTCompilationUnit.class) .findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); final Set<String> classesNames = new HashSet<>(); for (final ASTClassOrInterfaceDeclaration c : classes) { if (!Objects.equals(c, currentClass) && extendsOrImplementsCloneable(c)) { classesNames.add(c.getImage()); } } return classesNames; }
/** * Checks whether the type this node is referring to is declared within the * same compilation unit - either a class/interface or a enum type. You want * to check this, if {@link #getType()} is null. * * @return <code>true</code> if this node referencing a type in the same * compilation unit, <code>false</code> otherwise. */ public boolean isReferenceToClassSameCompilationUnit() { ASTCompilationUnit root = getFirstParentOfType(ASTCompilationUnit.class); for (ASTClassOrInterfaceDeclaration c : root.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true)) { if (c.hasImageEqualTo(getImage())) { return true; } } for (ASTEnumDeclaration e : root.findDescendantsOfType(ASTEnumDeclaration.class, true)) { if (e.hasImageEqualTo(getImage())) { return true; } } return false; }
@Override public Object visit(ASTCompilationUnit node, Object data) { List<ASTFieldDeclaration> fieldDeclarations = node.findDescendantsOfType(ASTFieldDeclaration.class); if (fieldDeclarations == null || fieldDeclarations.isEmpty()) { return super.visit(node, data); } for (ASTFieldDeclaration fieldDeclaration : fieldDeclarations) { if (fieldDeclaration.getType() == ThreadLocal.class) { if (checkThreadLocalWithInitalValue(fieldDeclaration)) { continue; } checkThreadLocal(fieldDeclaration, node, data); } } return super.visit(node, data); }
@Override public Object visit(ASTCompilationUnit node, Object data) { boolean hasImportSqlMapClient = hasSqlMapClientImport(node.findChildrenOfType(ASTImportDeclaration.class)); if (!hasImportSqlMapClient) { return super.visit(node, data); } List<ASTClassOrInterfaceDeclaration> classOrInterfaceDeclarations = node.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); if (classOrInterfaceDeclarations == null || classOrInterfaceDeclarations.isEmpty()) { return super.visit(node, data); } for (ASTClassOrInterfaceDeclaration classOrInterfaceDeclaration : classOrInterfaceDeclarations) { visitASTClassOrInterfaceDeclaration(classOrInterfaceDeclaration, data); } return super.visit(node, data); }
@Override public Object visit(ASTCompilationUnit node, Object data) { Object superResult = super.visit(node, data); Info info = new Info(); List<ASTImportDeclaration> importDeclarations = node.findChildrenOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : importDeclarations) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); info.executorsUsed = info.executorsUsed || (name.getType() == Executors.class || Executors.class.getName().equals(name.getImage())); if (name.getImage().startsWith(Executors.class.getName() + DOT)) { info.importedExecutorsMethods.add(name.getImage()); } } List<ASTPrimaryExpression> primaryExpressions = node.findDescendantsOfType(ASTPrimaryExpression.class); for(ASTPrimaryExpression primaryExpression : primaryExpressions){ if (!info.executorsUsed && info.importedExecutorsMethods.isEmpty()) { continue; } Token initToken = (Token)primaryExpression.jjtGetFirstToken(); if (!checkInitStatement(initToken, info)) { addViolationWithMessage(data, primaryExpression,"java.concurrent.ThreadPoolCreationRule.violation.msg"); } } return superResult; }