Search in sources :

Example 1 with ASTBlockStatement

use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.

the class CloseResourceRule method ensureClosed.

private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) {
    // What are the chances of a Connection being instantiated in a
    // for-loop init block? Anyway, I'm lazy!
    String variableToClose = id.getImage();
    Node n = var;
    while (!(n instanceof ASTBlock) && !(n instanceof ASTConstructorDeclaration)) {
        n = n.jjtGetParent();
    }
    Node top = n;
    List<ASTTryStatement> tryblocks = top.findDescendantsOfType(ASTTryStatement.class);
    boolean closed = false;
    ASTBlockStatement parentBlock = id.getFirstParentOfType(ASTBlockStatement.class);
    // block.
    for (ASTTryStatement t : tryblocks) {
        // verifies that there are no critical statements between the
        // variable declaration and
        // the beginning of the try block.
        ASTBlockStatement tryBlock = t.getFirstParentOfType(ASTBlockStatement.class);
        // the variable has been initialized with null
        if (!hasNullInitializer(var) && parentBlock.jjtGetParent() == tryBlock.jjtGetParent()) {
            List<ASTBlockStatement> blocks = parentBlock.jjtGetParent().findChildrenOfType(ASTBlockStatement.class);
            int parentBlockIndex = blocks.indexOf(parentBlock);
            int tryBlockIndex = blocks.indexOf(tryBlock);
            boolean criticalStatements = false;
            for (int i = parentBlockIndex + 1; i < tryBlockIndex; i++) {
                // assume variable declarations are not critical
                ASTLocalVariableDeclaration varDecl = blocks.get(i).getFirstDescendantOfType(ASTLocalVariableDeclaration.class);
                if (varDecl == null) {
                    criticalStatements = true;
                    break;
                }
            }
            if (criticalStatements) {
                break;
            }
        }
        if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) {
            ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0);
            List<ASTName> names = f.findDescendantsOfType(ASTName.class);
            for (ASTName oName : names) {
                String name = oName.getImage();
                if (name != null && name.contains(".")) {
                    String[] parts = name.split("\\.");
                    if (parts.length == 2) {
                        String methodName = parts[1];
                        String varName = parts[0];
                        if (varName.equals(variableToClose) && closeTargets.contains(methodName) && nullCheckIfCondition(f, oName, varName)) {
                            closed = true;
                            break;
                        }
                    }
                }
            }
            if (closed) {
                break;
            }
            List<ASTStatementExpression> exprs = new ArrayList<>();
            f.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
            for (ASTStatementExpression stmt : exprs) {
                ASTPrimaryExpression expr = stmt.getFirstChildOfType(ASTPrimaryExpression.class);
                if (expr != null) {
                    ASTPrimaryPrefix prefix = expr.getFirstChildOfType(ASTPrimaryPrefix.class);
                    ASTPrimarySuffix suffix = expr.getFirstChildOfType(ASTPrimarySuffix.class);
                    if (prefix != null && suffix != null) {
                        if (prefix.getImage() == null) {
                            ASTName prefixName = prefix.getFirstChildOfType(ASTName.class);
                            if (prefixName != null && closeTargets.contains(prefixName.getImage())) {
                                // Found a call to a "close target" that is
                                // a direct
                                // method call without a "ClassName."
                                // prefix.
                                closed = variableIsPassedToMethod(expr, variableToClose);
                                if (closed) {
                                    break;
                                }
                            }
                        } else if (suffix.getImage() != null) {
                            String prefixPlusSuffix = prefix.getImage() + "." + suffix.getImage();
                            if (closeTargets.contains(prefixPlusSuffix)) {
                                // Found a call to a "close target" that is
                                // a method call
                                // in the form "ClassName.methodName".
                                closed = variableIsPassedToMethod(expr, variableToClose);
                                if (closed) {
                                    break;
                                }
                            }
                        }
                        // really check it.
                        if (!closed) {
                            List<ASTPrimarySuffix> suffixes = new ArrayList<>();
                            expr.findDescendantsOfType(ASTPrimarySuffix.class, suffixes, true);
                            for (ASTPrimarySuffix oSuffix : suffixes) {
                                String suff = oSuffix.getImage();
                                if (closeTargets.contains(suff)) {
                                    closed = variableIsPassedToMethod(expr, variableToClose);
                                    if (closed) {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (closed) {
                break;
            }
        }
    }
    if (!closed) {
        // See if the variable is returned by the method, which means the
        // method is a utility for creating the db resource, which means of
        // course it can't be closed by the method, so it isn't an error.
        List<ASTReturnStatement> returns = new ArrayList<>();
        top.findDescendantsOfType(ASTReturnStatement.class, returns, true);
        for (ASTReturnStatement returnStatement : returns) {
            ASTName name = returnStatement.getFirstDescendantOfType(ASTName.class);
            if (name != null && name.getImage().equals(variableToClose)) {
                closed = true;
                break;
            }
        }
    }
    // if all is not well, complain
    if (!closed) {
        ASTType type = var.getFirstChildOfType(ASTType.class);
        ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
        ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
        addViolation(data, id, clazz.getImage());
    }
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 2 with ASTBlockStatement

use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.

the class StdCyclomaticComplexityRule method visit.

@Override
public Object visit(ASTSwitchStatement node, Object data) {
    Entry entry = entryStack.peek();
    int childCount = node.jjtGetNumChildren();
    int lastIndex = childCount - 1;
    for (int n = 0; n < lastIndex; n++) {
        Node childNode = node.jjtGetChild(n);
        if (childNode instanceof ASTSwitchLabel) {
            // default is generally not considered a decision (same as
            // "else")
            ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
            if (!sl.isDefault()) {
                childNode = node.jjtGetChild(n + 1);
                if (childNode instanceof ASTBlockStatement) {
                    entry.bumpDecisionPoints();
                }
            }
        }
    }
    super.visit(node, data);
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)

Example 3 with ASTBlockStatement

use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.

the class SingletonClassReturningNewInstanceRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    boolean violation = false;
    String localVarName = null;
    String returnVariableName = null;
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    if ("getInstance".equals(node.getMethodName())) {
        List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
        if (rsl.isEmpty()) {
            return super.visit(node, data);
        } else {
            for (ASTReturnStatement rs : rsl) {
                List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
                ASTPrimaryExpression ape = pel.get(0);
                if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
                    violation = true;
                    break;
                }
            }
        }
        /*
             * public class Singleton {
             * 
             * private static Singleton m_instance=null;
             * 
             * public static Singleton getInstance() {
             * 
             * Singleton m_instance=null;
             * 
             * if ( m_instance == null ) { synchronized(Singleton.class) {
             * if(m_instance == null) { m_instance = new Singleton(); } } }
             * return m_instance; } }
             */
        List<ASTBlockStatement> astBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
        returnVariableName = getReturnVariableName(node);
        if (!astBlockStatements.isEmpty()) {
            for (ASTBlockStatement blockStatement : astBlockStatements) {
                if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) {
                    List<ASTLocalVariableDeclaration> lVarList = blockStatement.findDescendantsOfType(ASTLocalVariableDeclaration.class);
                    if (!lVarList.isEmpty()) {
                        for (ASTLocalVariableDeclaration localVar : lVarList) {
                            localVarName = localVar.getVariableName();
                            if (returnVariableName != null && returnVariableName.equals(localVarName)) {
                                violation = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    if (violation) {
        addViolation(data, node);
    }
    return super.visit(node, data);
}
Also used : ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 4 with ASTBlockStatement

use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.

the class ScopeAndDeclarationFinder method visit.

@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
    /*
         * Local variables declared inside the constructor need to be in a
         * different scope so special handling is needed
         */
    createMethodScope(node);
    Scope methodScope = node.getScope();
    Node formalParameters = node.jjtGetChild(0);
    int i = 1;
    int n = node.jjtGetNumChildren();
    if (!(formalParameters instanceof ASTFormalParameters)) {
        visit((ASTTypeParameters) formalParameters, data);
        formalParameters = node.jjtGetChild(1);
        i++;
    }
    visit((ASTFormalParameters) formalParameters, data);
    Scope localScope = null;
    for (; i < n; i++) {
        JavaNode b = (JavaNode) node.jjtGetChild(i);
        if (b instanceof ASTBlockStatement) {
            if (localScope == null) {
                createLocalScope(node);
                localScope = node.getScope();
            }
            b.setScope(localScope);
            visit(b, data);
        } else {
            visit(b, data);
        }
    }
    if (localScope != null) {
        // pop the local scope
        scopes.pop();
        // reset the correct scope for the constructor
        node.setScope(methodScope);
    }
    // pop the method scope
    scopes.pop();
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)

Example 5 with ASTBlockStatement

use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.

the class ArrayIsStoredDirectlyRule method checkForDirectAssignment.

/**
 * Checks if the variable designed in parameter is written to a field (not
 * local variable) in the statements.
 */
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
    final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    final String varName = vid.getImage();
    for (ASTBlockStatement b : bs) {
        if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
            final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
            if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
                continue;
            }
            String assignedVar = getExpressionVarName(se);
            if (assignedVar == null) {
                continue;
            }
            ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
            Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
            if (n == null) {
                n = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
                if (n == null) {
                    continue;
                }
            }
            if (!isLocalVariable(assignedVar, n)) {
                // something
                if (se.jjtGetNumChildren() < 3) {
                    continue;
                }
                ASTExpression e = (ASTExpression) se.jjtGetChild(2);
                if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
                    continue;
                }
                String val = getExpressionVarName(e);
                if (val == null) {
                    continue;
                }
                ASTPrimarySuffix foo = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (foo != null && foo.isArrayDereference()) {
                    continue;
                }
                if (val.equals(varName)) {
                    Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class);
                    if (md == null) {
                        md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
                    }
                    if (!isLocalVariable(varName, md)) {
                        addViolation(ctx, parameter, varName);
                    }
                }
            }
        }
    }
    return false;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

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