Search in sources :

Example 11 with VariableNameDeclaration

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

the class ForLoopCanBeForeachRule method visit.

@Override
public Object visit(ASTForStatement node, Object data) {
    final ASTForInit init = node.getFirstChildOfType(ASTForInit.class);
    final ASTForUpdate update = node.getFirstChildOfType(ASTForUpdate.class);
    final ASTExpression guardCondition = node.getFirstChildOfType(ASTExpression.class);
    if (init == null && update == null || guardCondition == null) {
        return data;
    }
    Entry<VariableNameDeclaration, List<NameOccurrence>> indexDecl = getIndexVarDeclaration(init, update);
    if (indexDecl == null) {
        return data;
    }
    List<NameOccurrence> occurrences = indexDecl.getValue();
    VariableNameDeclaration index = indexDecl.getKey();
    if (TypeHelper.isA(index, Iterator.class)) {
        Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = getIterableDeclOfIteratorLoop(index, node.getScope());
        if (iterableInfo != null && isReplaceableIteratorLoop(indexDecl, guardCondition, iterableInfo, node)) {
            addViolation(data, node);
        }
        return data;
    }
    if (occurrences == null || !"int".equals(index.getTypeImage()) || !indexStartsAtZero(index)) {
        return data;
    }
    String itName = index.getName();
    String iterableName = getIterableNameOrNullToAbort(guardCondition, itName);
    if (!isForUpdateSimpleEnough(update, itName) || iterableName == null) {
        return data;
    }
    Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = findDeclaration(iterableName, node.getScope());
    VariableNameDeclaration iterableDeclaration = iterableInfo == null ? null : iterableInfo.getKey();
    if (iterableDeclaration == null) {
        return data;
    }
    if (iterableDeclaration.isArray() && isReplaceableArrayLoop(node, occurrences, iterableDeclaration)) {
        addViolation(data, node);
    } else if (iterableDeclaration.getTypeImage() != null && iterableDeclaration.getTypeImage().matches("List|ArrayList|LinkedList") && isReplaceableListLoop(node, occurrences, iterableDeclaration)) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) List(java.util.List) ASTForUpdate(net.sourceforge.pmd.lang.java.ast.ASTForUpdate) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 12 with VariableNameDeclaration

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

the class ForLoopCanBeForeachRule method getIndexVarDeclaration.

/* Finds the declaration of the index variable and its occurrences, null to abort */
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIndexVarDeclaration(ASTForInit init, ASTForUpdate update) {
    if (init == null) {
        return guessIndexVarFromUpdate(update);
    }
    ASTLocalVariableDeclaration decl = init.getFirstChildOfType(ASTLocalVariableDeclaration.class);
    if (decl == null) {
        return null;
    }
    int numDeclaredVars = decl.findChildrenOfType(ASTVariableDeclarator.class).size();
    if (numDeclaredVars > 1) {
        // will abort in the calling function
        return null;
    }
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = init.getScope().getDeclarations(VariableNameDeclaration.class);
    Entry<VariableNameDeclaration, List<NameOccurrence>> indexVarAndOccurrences = null;
    for (Entry<VariableNameDeclaration, List<NameOccurrence>> e : decls.entrySet()) {
        ASTForInit declInit = e.getKey().getNode().getFirstParentOfType(ASTForInit.class);
        if (Objects.equals(declInit, init)) {
            indexVarAndOccurrences = e;
            break;
        }
    }
    return indexVarAndOccurrences;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) List(java.util.List)

Example 13 with VariableNameDeclaration

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

the class VariableAccessVisitor method collectDeclarations.

private Set<Map<VariableNameDeclaration, List<NameOccurrence>>> collectDeclarations(DataFlowNode inode) {
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> decls = new HashSet<>();
    Map<VariableNameDeclaration, List<NameOccurrence>> varDecls;
    for (int i = 0; i < inode.getFlow().size(); i++) {
        DataFlowNode n = inode.getFlow().get(i);
        if (n instanceof StartOrEndDataFlowNode) {
            continue;
        }
        varDecls = ((JavaNode) n.getNode()).getScope().getDeclarations(VariableNameDeclaration.class);
        if (!decls.contains(varDecls)) {
            decls.add(varDecls);
        }
    }
    return decls;
}
Also used : StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) HashSet(java.util.HashSet) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 14 with VariableNameDeclaration

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

the class UselessStringValueOfRule method isPrimitive.

private static boolean isPrimitive(Node parent) {
    boolean result = false;
    if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
        Node child = parent.jjtGetChild(0);
        if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
            Node gc = child.jjtGetChild(0);
            if (gc instanceof ASTName) {
                ASTName name = (ASTName) gc;
                NameDeclaration nd = name.getNameDeclaration();
                if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
                    result = true;
                }
            } else if (gc instanceof ASTLiteral) {
                result = !((ASTLiteral) gc).isStringLiteral();
            }
        }
    }
    return result;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 15 with VariableNameDeclaration

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Aggregations

VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)23 List (java.util.List)18 Map (java.util.Map)15 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ArrayList (java.util.ArrayList)6 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)2