use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class CloneMethodMustImplementCloneableRule method visit.
@Override
public Object visit(final ASTMethodDeclaration node, final Object data) {
// Is this a clone method?
final ASTMethodDeclarator methodDeclarator = node.getFirstChildOfType(ASTMethodDeclarator.class);
if (!isCloneMethod(methodDeclarator)) {
return data;
}
// Is the clone method just throwing CloneNotSupportedException?
final ASTClassOrInterfaceDeclaration classOrInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (// Don't analyze enums, which cannot subclass clone()
classOrInterface != null && (node.isFinal() || classOrInterface.isFinal())) {
if (node.findDescendantsOfType(ASTBlock.class).size() == 1) {
final List<ASTBlockStatement> blocks = node.findDescendantsOfType(ASTBlockStatement.class);
if (blocks.size() == 1) {
final ASTBlockStatement block = blocks.get(0);
final ASTClassOrInterfaceType type = block.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (type != null && type.getType() != null && type.getNthParent(9).equals(node) && type.getType().equals(CloneNotSupportedException.class)) {
return data;
} else if (type != null && type.getType() == null && "CloneNotSupportedException".equals(type.getImage())) {
return data;
}
}
}
}
// TODO : Should we really care about this? It can only happen with an incomplete auxclasspath
if (classOrInterface != null && classOrInterface.getType() == null) {
// Now check other whether implemented or extended classes are defined inside the same file
final Set<String> classesNames = determineTopLevelCloneableClasses(classOrInterface);
final ASTImplementsList implementsList = classOrInterface.getFirstChildOfType(ASTImplementsList.class);
if (implementsList != null) {
final List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class);
for (final ASTClassOrInterfaceType t : types) {
if (classesNames.contains(t.getImage())) {
return data;
}
}
}
final ASTExtendsList extendsList = classOrInterface.getFirstChildOfType(ASTExtendsList.class);
if (extendsList != null) {
final ASTClassOrInterfaceType type = extendsList.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (classesNames.contains(type.getImage())) {
return data;
}
}
}
// Nothing can save us now
addViolation(data, node);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class UnnecessaryLocalBeforeReturnRule method statementsBeforeReturn.
private boolean statementsBeforeReturn(VariableNameDeclaration variableDeclaration, ASTReturnStatement returnStatement) {
if (!getProperty(STATEMENT_ORDER_MATTERS)) {
return false;
}
ASTBlockStatement declarationStatement = variableDeclaration.getAccessNodeParent().getFirstParentOfType(ASTBlockStatement.class);
ASTBlockStatement returnBlockStatement = returnStatement.getFirstParentOfType(ASTBlockStatement.class);
// double check: we should now be at the same level in the AST - both block statements are children of the same parent
if (declarationStatement.jjtGetParent() == returnBlockStatement.jjtGetParent()) {
return returnBlockStatement.jjtGetChildIndex() - declarationStatement.jjtGetChildIndex() > 1;
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class PrematureDeclarationRule method visit.
/**
* @param node
* ASTLocalVariableDeclaration
* @param data
* Object
* @return Object
* @see net.sourceforge.pmd.lang.java.ast.JavaParserVisitor#visit(ASTLocalVariableDeclaration,
* Object)
*/
public Object visit(ASTLocalVariableDeclaration node, Object data) {
// is it part of a for-loop declaration?
if (node.jjtGetParent() instanceof ASTForInit) {
// yes, those don't count
return visit((AbstractJavaNode) node, data);
}
String varName = varNameIn(node);
AbstractJavaNode grandparent = (AbstractJavaNode) node.jjtGetParent().jjtGetParent();
List<ASTBlockStatement> nextBlocks = blocksAfter(grandparent, node);
for (ASTBlockStatement block : nextBlocks) {
if (hasReferencesIn(block, varName) || isLambda(block)) {
break;
}
if (hasExit(block)) {
addViolation(data, node, varName);
break;
}
}
return visit((AbstractJavaNode) node, data);
}
Aggregations