Search in sources :

Example 21 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.

the class LocalScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            nameOccurrences.add(javaOccurrence);
            Node n = javaOccurrence.getLocation();
            if (n instanceof ASTName) {
                ((ASTName) n).setNameDeclaration(decl);
            }
        // TODO what to do with PrimarySuffix case?
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 22 with NameOccurrence

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

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

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

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.

the class UnsynchronizedStaticDateFormatterRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    if (!node.isStatic()) {
        return data;
    }
    ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (cit == null || !targets.contains(cit.getImage())) {
        return data;
    }
    ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    for (NameOccurrence occ : var.getUsages()) {
        Node n = occ.getLocation();
        if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
            continue;
        }
        // ignore usages, that don't call a method.
        if (!n.getImage().contains(".")) {
            continue;
        }
        ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
        if (method != null && !method.isSynchronized()) {
            addViolation(data, n);
        }
    }
    return data;
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)40 Node (net.sourceforge.pmd.lang.ast.Node)20 List (java.util.List)19 Map (java.util.Map)15 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)13 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)10 Test (org.junit.Test)10 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)9 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)8 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 ArrayList (java.util.ArrayList)7 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)7 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)7 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 Scope (net.sourceforge.pmd.lang.symboltable.Scope)4 HashSet (java.util.HashSet)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3