Search in sources :

Example 16 with NameOccurrence

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

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

the class JUnitTestsShouldIncludeAssertRule method isExpectStatement.

private boolean isExpectStatement(ASTStatementExpression expression, Map<String, List<NameOccurrence>> expectables) {
    if (expression != null) {
        ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
        if (pe != null) {
            Node name = pe.getFirstDescendantOfType(ASTName.class);
            // case of an AllocationExpression
            if (name == null) {
                return false;
            }
            String img = name.getImage();
            if (img.indexOf(".") == -1) {
                return false;
            }
            String varname = img.split("\\.")[0];
            if (!expectables.containsKey(varname)) {
                return false;
            }
            for (NameOccurrence occ : expectables.get(varname)) {
                if (occ.getLocation() == name && img.startsWith(varname + ".expect")) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 18 with NameOccurrence

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

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

the class SymbolTableTestRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) {
        for (NameOccurrence no : declaration.getUsages()) {
            Node location = no.getLocation();
            System.out.println(declaration.getImage() + " is used here: " + location.getImage());
        }
    }
    return data;
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 20 with NameOccurrence

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

the class ClassScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && (javaOccurrence.isMethodOrConstructorInvocation() || javaOccurrence.isMethodReference())) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getMethodDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                nameOccurrences.add(javaOccurrence);
                Node n = javaOccurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    } else if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                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 : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ArrayList(java.util.ArrayList) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) 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