Search in sources :

Example 6 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence 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 7 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class StringToStringRule method visit.

public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isA(node.getNameDeclaration(), String.class) && !TypeHelper.isA(node.getNameDeclaration(), String[].class)) {
        return data;
    }
    boolean isArray = node.isArray();
    for (NameOccurrence occ : node.getUsages()) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
        if (qualifier != null) {
            if (!isArray && isNotAMethodReference(qualifier) && qualifier.getImage().indexOf("toString") != -1) {
                addViolation(data, jocc.getLocation());
            } else if (isArray && isNotAName(qualifier) && qualifier.getImage().equals("toString")) {
                addViolation(data, jocc.getLocation());
            }
        }
    }
    return data;
}
Also used : JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 8 with JavaNameOccurrence

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

Example 9 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class AssignmentToNonFinalStaticRule method initializedInConstructor.

private boolean initializedInConstructor(List<NameOccurrence> usages) {
    boolean initInConstructor = false;
    for (NameOccurrence occ : usages) {
        // legitimate usages of these with static fields, e.g. typesafe enum pattern.
        if (((JavaNameOccurrence) occ).isOnLeftHandSide()) {
            Node node = occ.getLocation();
            Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
            if (constructor != null) {
                initInConstructor = true;
            }
        }
    }
    return initInConstructor;
}
Also used : JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Node(net.sourceforge.pmd.lang.ast.Node) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)9 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)9 Node (net.sourceforge.pmd.lang.ast.Node)6 List (java.util.List)3 Map (java.util.Map)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1