Search in sources :

Example 1 with ASTPrimarySuffix

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

the class InsufficientStringBufferDeclarationRule method getConstructorAppendsLength.

private int getConstructorAppendsLength(final Node node) {
    final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class);
    int size = 0;
    if (parent != null) {
        final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class);
        if (initializer != null) {
            final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
            if (primExp != null) {
                for (int i = 0; i < primExp.jjtGetNumChildren(); i++) {
                    final Node sn = primExp.jjtGetChild(i);
                    if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
                        continue;
                    }
                    size += processNode(sn);
                }
            }
        }
    }
    return size;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 2 with ASTPrimarySuffix

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

the class UnnecessaryWrapperObjectCreationRule method visit.

public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
        return super.visit(node, data);
    }
    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if (image.startsWith("java.lang.")) {
        image = image.substring(10);
    }
    boolean checkBoolean = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
    if (PREFIX_SET.contains(image) || checkBoolean && "Boolean.valueOf".equals(image)) {
        ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
        if (parent.jjtGetNumChildren() >= 3) {
            Node n = parent.jjtGetChild(2);
            if (n instanceof ASTPrimarySuffix) {
                ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
                image = suffix.getImage();
                if (SUFFIX_SET.contains(image) || checkBoolean && "booleanValue".equals(image)) {
                    super.addViolation(data, node);
                    return data;
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 3 with ASTPrimarySuffix

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

the class IdempotentOperationsRule method visit.

@Override
public Object visit(ASTStatementExpression node, Object data) {
    if (node.jjtGetNumChildren() != 3 || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression) || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator) || ((ASTAssignmentOperator) node.jjtGetChild(1)).isCompound() || !(node.jjtGetChild(2) instanceof ASTExpression) || node.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0) {
        return super.visit(node, data);
    }
    Node lhs = node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
    if (!(lhs instanceof ASTName)) {
        return super.visit(node, data);
    }
    Node rhs = node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
    if (!(rhs instanceof ASTName)) {
        return super.visit(node, data);
    }
    if (!lhs.hasImageEqualTo(rhs.getImage())) {
        return super.visit(node, data);
    }
    if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
        Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
        if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) {
            return super.visit(node, data);
        }
    }
    if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
        Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
        if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) {
            return super.visit(node, data);
        }
    }
    if (lhs.findDescendantsOfType(ASTPrimarySuffix.class).size() != rhs.findDescendantsOfType(ASTPrimarySuffix.class).size()) {
        return super.visit(node, data);
    }
    List<ASTPrimarySuffix> lhsSuffixes = lhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
    List<ASTPrimarySuffix> rhsSuffixes = rhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
    if (lhsSuffixes.size() != rhsSuffixes.size()) {
        return super.visit(node, data);
    }
    for (int i = 0; i < lhsSuffixes.size(); i++) {
        ASTPrimarySuffix l = lhsSuffixes.get(i);
        ASTPrimarySuffix r = rhsSuffixes.get(i);
        if (!l.hasImageEqualTo(r.getImage())) {
            return super.visit(node, data);
        }
    }
    addViolation(data, node);
    return super.visit(node, data);
}
Also used : ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 4 with ASTPrimarySuffix

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

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

the class AvoidCallingFinalizeRule method visit.

public Object visit(ASTPrimaryPrefix pp, Object ctx) {
    List<ASTPrimarySuffix> primarySuffixes = pp.jjtGetParent().findChildrenOfType(ASTPrimarySuffix.class);
    ASTPrimarySuffix firstSuffix = null;
    if (!primarySuffixes.isEmpty()) {
        firstSuffix = primarySuffixes.get(0);
    }
    if (firstSuffix == null || firstSuffix.getImage() == null || !firstSuffix.getImage().endsWith("finalize")) {
        return super.visit(pp, ctx);
    }
    if (!checkForViolation(pp)) {
        return super.visit(pp, ctx);
    }
    addViolation(ctx, pp);
    return super.visit(pp, ctx);
}
Also used : ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Aggregations

ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)24 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)14 Node (net.sourceforge.pmd.lang.ast.Node)12 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)11 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ArrayList (java.util.ArrayList)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 List (java.util.List)2 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2