Search in sources :

Example 1 with ASTReturnStatement

use of net.sourceforge.pmd.lang.java.ast.ASTReturnStatement 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 ASTReturnStatement

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

the class DoubleCheckedLockingRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
    if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
        return super.visit(node, data);
    }
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    if (rsl.size() != 1) {
        return super.visit(node, data);
    }
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    // With Java5 and volatile keyword, DCL is no longer an issue
    if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
        return super.visit(node, data);
    }
    // field, then it's ok, too
    if (checkLocalVariableUsage(node, returnVariableName)) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
    if (isl.size() == 2) {
        ASTIfStatement is = isl.get(0);
        if (ifVerify(is, returnVariableName)) {
            // find synchronized
            List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
            if (ssl.size() == 1) {
                ASTSynchronizedStatement ss = ssl.get(0);
                isl = ss.findDescendantsOfType(ASTIfStatement.class);
                if (isl.size() == 1) {
                    ASTIfStatement is2 = isl.get(0);
                    if (ifVerify(is2, returnVariableName)) {
                        List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
                        if (sel.size() == 1) {
                            ASTStatementExpression se = sel.get(0);
                            if (se.jjtGetNumChildren() == 3) {
                                // primaryExpression, AssignmentOperator, Expression
                                if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                                    ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
                                    if (matchName(pe, returnVariableName)) {
                                        if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                                            addViolation(data, node);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Example 3 with ASTReturnStatement

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

the class SingletonClassReturningNewInstanceRule method getReturnVariableName.

private String getReturnVariableName(ASTMethodDeclaration node) {
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(0);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    /*
         * if(lastChild instanceof ASTPrimarySuffix){ returnVariableName =
         * getNameFromPrimarySuffix((ASTPrimarySuffix) lastChild); }
         */
    return returnVariableName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 4 with ASTReturnStatement

use of net.sourceforge.pmd.lang.java.ast.ASTReturnStatement 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 5 with ASTReturnStatement

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

the class MethodReturnsInternalArrayRule method visit.

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
    if (!method.getResultType().returnsArray() || method.isPrivate()) {
        return data;
    }
    List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class);
    ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class);
    for (ASTReturnStatement ret : returns) {
        final String vn = getReturnedVariableName(ret);
        if (!isField(vn, td)) {
            continue;
        }
        if (ret.findDescendantsOfType(ASTPrimarySuffix.class).size() > 2) {
            continue;
        }
        if (ret.hasDescendantOfType(ASTAllocationExpression.class)) {
            continue;
        }
        if (hasArraysCopyOf(ret)) {
            continue;
        }
        if (hasClone(ret, vn)) {
            continue;
        }
        if (isEmptyArray(vn, td)) {
            continue;
        }
        if (!isLocalVariable(vn, method)) {
            addViolation(data, ret, vn);
        } else {
            // This is to handle field hiding
            final ASTPrimaryPrefix pp = ret.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (pp != null && pp.usesThisModifier()) {
                final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (ps.hasImageEqualTo(vn)) {
                    addViolation(data, ret, vn);
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Aggregations

ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)7 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)4 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)2 ArrayList (java.util.ArrayList)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)1 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)1