Search in sources :

Example 16 with ASTPrimaryExpression

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

the class InsufficientStringBufferDeclarationRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isEither(node.getNameDeclaration(), StringBuffer.class, StringBuilder.class)) {
        return data;
    }
    Node rootNode = node;
    int anticipatedLength = 0;
    int constructorLength = DEFAULT_BUFFER_SIZE;
    constructorLength = getConstructorLength(node, constructorLength);
    anticipatedLength = getInitialLength(node);
    anticipatedLength += getConstructorAppendsLength(node);
    List<NameOccurrence> usage = node.getUsages();
    Map<Node, Map<Node, Integer>> blocks = new HashMap<>();
    for (NameOccurrence no : usage) {
        JavaNameOccurrence jno = (JavaNameOccurrence) no;
        Node n = jno.getLocation();
        if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
            if (!jno.isOnLeftHandSide() && !InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "setLength")) {
                continue;
            }
            if (constructorLength != -1 && anticipatedLength > constructorLength) {
                anticipatedLength += processBlocks(blocks);
                String[] param = { String.valueOf(constructorLength), String.valueOf(anticipatedLength) };
                addViolation(data, rootNode, param);
            }
            constructorLength = getConstructorLength(n, constructorLength);
            rootNode = n;
            anticipatedLength = getInitialLength(node);
        }
        ASTPrimaryExpression s = n.getFirstParentOfType(ASTPrimaryExpression.class);
        int numChildren = s.jjtGetNumChildren();
        for (int jx = 0; jx < numChildren; jx++) {
            Node sn = s.jjtGetChild(jx);
            if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
                continue;
            }
            int thisSize = 0;
            Node block = getFirstParentBlock(sn);
            if (isAdditive(sn)) {
                thisSize = processAdditive(sn);
            } else {
                thisSize = processNode(sn);
            }
            if (block != null) {
                storeBlockStatistics(blocks, thisSize, block);
            } else {
                anticipatedLength += thisSize;
            }
        }
    }
    anticipatedLength += processBlocks(blocks);
    if (constructorLength != -1 && anticipatedLength > constructorLength) {
        String[] param = { String.valueOf(constructorLength), String.valueOf(anticipatedLength) };
        addViolation(data, rootNode, param);
    }
    return data;
}
Also used : HashMap(java.util.HashMap) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) HashMap(java.util.HashMap) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 17 with ASTPrimaryExpression

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

the class UseStringBufferForStringAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isA(node, String.class) || node.isArray()) {
        return data;
    }
    Node parent = node.jjtGetParent().jjtGetParent();
    if (!(parent instanceof ASTLocalVariableDeclaration)) {
        return data;
    }
    for (NameOccurrence no : node.getUsages()) {
        Node name = no.getLocation();
        ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
        if (statement == null) {
            continue;
        }
        ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
        if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in method call
            continue;
        }
        ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
        if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in condition
            continue;
        }
        ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
        if (conditional != null) {
            Node thirdParent = name.getNthParent(3);
            Node fourthParent = name.getNthParent(4);
            if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
                // string)
                continue;
            }
        }
        if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
            ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
            if (astName != null) {
                if (astName.equals(name)) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && assignmentOperator.isCompound()) {
                        addViolation(data, assignmentOperator);
                    }
                } else if (astName.getImage().equals(name.getImage())) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && !assignmentOperator.isCompound()) {
                        addViolation(data, astName);
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 18 with ASTPrimaryExpression

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

the class NonThreadSafeSingletonRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
    for (ASTIfStatement ifStatement : ifStatements) {
        if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
            if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
                continue;
            }
            ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
            if (n == null || !fieldDecls.containsKey(n.getImage())) {
                continue;
            }
            List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
            boolean violation = false;
            for (int ix = 0; ix < assigmnents.size(); ix++) {
                ASTAssignmentOperator oper = assigmnents.get(ix);
                if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
                    continue;
                }
                ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
                if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
                    ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
                    String name = null;
                    if (pp.usesThisModifier()) {
                        ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
                        name = priSuf.getImage();
                    } else {
                        ASTName astName = (ASTName) pp.jjtGetChild(0);
                        name = astName.getImage();
                    }
                    if (fieldDecls.containsKey(name)) {
                        violation = true;
                    }
                }
            }
            if (violation) {
                addViolation(data, ifStatement);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 19 with ASTPrimaryExpression

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

the class BooleanInstantiationRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (!customBoolean) {
        if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
            return super.visit(node, data);
        }
        if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage()) || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
            ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
            ASTPrimarySuffix suffix = parent.getFirstDescendantOfType(ASTPrimarySuffix.class);
            if (suffix == null) {
                return super.visit(node, data);
            }
            ASTPrimaryPrefix prefix = suffix.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (prefix == null) {
                return super.visit(node, data);
            }
            if (prefix.hasDescendantOfType(ASTBooleanLiteral.class)) {
                super.addViolation(data, node);
                return data;
            }
            ASTLiteral literal = prefix.getFirstDescendantOfType(ASTLiteral.class);
            if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 20 with ASTPrimaryExpression

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

the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.

/**
 * Determine if during the variable initializer calls to ".append" are done.
 *
 * @param node
 * @return
 */
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
    ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
    ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
    int result = 0;
    boolean previousWasAppend = false;
    for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
        Node child = primary.jjtGetChild(i);
        if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
            // skip the constructor call, that has already been checked
            continue;
        }
        if (child instanceof ASTPrimarySuffix) {
            ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
            if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
                previousWasAppend = true;
            } else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
                previousWasAppend = false;
                ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
                if (literal != null && literal.isStringLiteral()) {
                    result++;
                } else {
                    // checking the remainder of the initializer
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Aggregations

ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)31 Node (net.sourceforge.pmd.lang.ast.Node)23 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)16 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)11 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)10 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)8 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)7 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)5 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)5 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)3 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)3