Search in sources :

Example 11 with ASTBlockStatement

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;
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Example 12 with ASTBlockStatement

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;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)

Example 13 with ASTBlockStatement

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);
}
Also used : AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit)

Aggregations

ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)13 Node (net.sourceforge.pmd.lang.ast.Node)8 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)4 ArrayList (java.util.ArrayList)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTSwitchLabel (net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTCatchStatement (net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1