private boolean throwsOneException(ASTMethodDeclaration node, Class<? extends Throwable> exception) { ASTNameList throwsList = node.getThrows(); if (throwsList != null && throwsList.jjtGetNumChildren() == 1) { ASTName n = (ASTName) throwsList.jjtGetChild(0); if (n.getType() == exception || exception.getSimpleName().equals(n.getImage()) || exception.getName().equals(n.getImage())) { return true; } } return false; }
if (node.getType() != null) { // static field or method
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName) node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } // no need to visit children, the only child, ASTName, will have no type return data; }
if (node.getType() != null) { // static field or method
private boolean methodHasOverride(Node node) { ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) { ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class); if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) { ASTName name = marker.getFirstChildOfType(ASTName.class); if (name.getType() == Override.class) { return true; } } } return false; } }
private boolean isLockTypeAndMethod(ASTStatementExpression statementExpression, String methodName) { ASTName name = statementExpression.getFirstDescendantOfType(ASTName.class); if (name == null || name.getType() == null || !Lock.class.isAssignableFrom(name.getType())) { return false; } Token token = (Token)name.jjtGetLastToken(); return methodName.equals(token.image); } }
private boolean throwsOneException(ASTMethodDeclaration node, Class<? extends Throwable> exception) { ASTNameList throwsList = node.getThrows(); if (throwsList != null && throwsList.jjtGetNumChildren() == 1) { ASTName n = (ASTName) throwsList.jjtGetChild(0); if (n.getType() == exception || exception.getSimpleName().equals(n.getImage()) || exception.getName().equals(n.getImage())) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName)node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } return data; }
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName) node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } // no need to visit children, the only child, ASTName, will have no type return data; }
@Override public Object visit(ASTTryStatement node, Object data) { try { List<Node> nodes = node.findChildNodesWithXPath(XPATH); if (nodes == null || nodes.isEmpty()) { return super.visit(node, data); } for (Node nameNode : nodes) { if (!(nameNode instanceof ASTName)) { continue; } ASTName name = (ASTName)nameNode; if (name.getType() != CountDownLatch.class) { continue; } addViolationWithMessage(data, name, "java.concurrent.CountDownShouldInFinallyRule.violation.msg", new Object[] {name.getImage()}); } } catch (JaxenException ignore) { } return super.visit(node, data); } }
private boolean isStaticSimpleDateFormatCall(ASTPrimaryExpression primaryExpression, Set<String> localSimpleDateFormatNames) { if (primaryExpression.jjtGetNumChildren() == 0) { return false; } ASTName name = primaryExpression.getFirstDescendantOfType(ASTName.class); if (name == null || name.getType() != SimpleDateFormat.class) { return false; } if (localSimpleDateFormatNames.contains(name.getNameDeclaration().getName())) { return false; } ASTPrimaryPrefix primaryPrefix = (ASTPrimaryPrefix)primaryExpression.jjtGetChild(0); if (primaryPrefix.getType() != SimpleDateFormat.class) { return false; } Token token = (Token)primaryPrefix.jjtGetLastToken(); return FORMAT_METHOD_NAME.equals(token.image); }
private boolean checkThreadFactoryArgument(ASTExpression expression) { if (expression.getType() != null && ThreadFactory.class.isAssignableFrom(expression.getType())) { return true; } ASTName name = expression.getFirstDescendantOfType(ASTName.class); if (name != null && name.getType() == Executors.class) { return false; } ASTLambdaExpression lambdaExpression = expression.getFirstDescendantOfType(ASTLambdaExpression.class); if (lambdaExpression != null) { List<ASTVariableDeclaratorId> variableDeclaratorIds = lambdaExpression.findChildrenOfType(ASTVariableDeclaratorId.class); if (variableDeclaratorIds == null || variableDeclaratorIds.size() != SINGLE_LENGTH) { return false; } } else if (expression.getType() != null && RejectedExecutionHandler.class.isAssignableFrom(expression.getType())) { return false; } return true; } }
@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; }
private boolean methodHasOverride(Node node) { ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) { ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class); if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) { ASTName name = marker.getFirstChildOfType(ASTName.class); if (name.getType() == Override.class) { return true; } } } return false; } }