Search in sources :

Example 1 with ASTTryStatement

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

the class VariableNameDeclarationTest method testExceptionBlkParam.

@Test
public void testExceptionBlkParam() {
    ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
    id.testingOnlySetBeginLine(10);
    id.setImage("foo");
    ASTFormalParameter param = new ASTFormalParameter(2);
    id.jjtSetParent(param);
    param.jjtSetParent(new ASTTryStatement(1));
    VariableNameDeclaration decl = new VariableNameDeclaration(id);
    assertTrue(decl.isExceptionBlockParameter());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 2 with ASTTryStatement

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

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

the class ExceptionAsFlowControlRule method visit.

@Override
public Object visit(ASTThrowStatement node, Object data) {
    ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class);
    if (parent == null) {
        return data;
    }
    for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent.getFirstParentOfType(ASTTryStatement.class)) {
        List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class);
        for (ASTCatchStatement catchStmt : list) {
            ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0);
            ASTType type = fp.getFirstDescendantOfType(ASTType.class);
            ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
            if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
                addViolation(data, name);
            }
        }
    }
    return data;
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 4 with ASTTryStatement

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

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
    List<Node> found = new ArrayList<>();
    if (catches.isEmpty()) {
        return found;
    }
    for (ASTTryStatement trySt : catches) {
        ASTCatchStatement cStatement = getCatch(trySt);
        if (cStatement != null) {
            ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
            if (block.jjtGetNumChildren() != 0) {
                continue;
            }
            List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
            if (blocks.isEmpty()) {
                continue;
            }
            ASTBlockStatement st = blocks.get(blocks.size() - 1);
            ASTName name = st.getFirstDescendantOfType(ASTName.class);
            if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
                found.add(name);
                continue;
            }
            ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
            if (th != null && st.equals(th.getNthParent(2))) {
                found.add(th);
                continue;
            }
        }
    }
    return found;
}
Also used : ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ArrayList(java.util.ArrayList) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)

Aggregations

ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)4 ArrayList (java.util.ArrayList)2 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTCatchStatement (net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)1 ASTThrowStatement (net.sourceforge.pmd.lang.java.ast.ASTThrowStatement)1 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)1