Search in sources :

Example 16 with ASTPrimarySuffix

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

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

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

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

Example 20 with ASTPrimarySuffix

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

the class ConsecutiveLiteralAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!isStringBuffer(node)) {
        return data;
    }
    threshold = getProperty(THRESHOLD_DESCRIPTOR);
    int concurrentCount = checkConstructor(node, data);
    if (hasInitializer(node)) {
        concurrentCount += checkInitializerExpressions(node);
    }
    Node lastBlock = getFirstParentBlock(node);
    Node currentBlock = lastBlock;
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getDeclarations(VariableNameDeclaration.class);
    Node rootNode = null;
    // only want the constructor flagged if it's really containing strings
    if (concurrentCount >= 1) {
        rootNode = node;
    }
    for (List<NameOccurrence> decl : decls.values()) {
        for (NameOccurrence no : decl) {
            JavaNameOccurrence jno = (JavaNameOccurrence) no;
            Node n = jno.getLocation();
            // variable
            if (!node.getImage().equals(jno.getImage())) {
                continue;
            }
            currentBlock = getFirstParentBlock(n);
            if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
                if (!jno.isPartOfQualifiedName()) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                }
                continue;
            }
            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;
                }
                // see if it changed blocks
                if (currentBlock != null && lastBlock != null && !currentBlock.equals(lastBlock) || currentBlock == null ^ lastBlock == null) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                }
                // here
                if (concurrentCount == 0) {
                    rootNode = sn;
                }
                if (isAdditive(sn)) {
                    concurrentCount = processAdditive(data, concurrentCount, sn, rootNode);
                    if (concurrentCount != 0) {
                        rootNode = sn;
                    }
                } else if (!isAppendingStringLiteral(sn)) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                } else {
                    concurrentCount++;
                }
                lastBlock = currentBlock;
            }
        }
    }
    checkForViolation(rootNode, data, concurrentCount);
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

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