Search in sources :

Example 1 with NameOccurrence

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

the class LocalScopeTest method testPostfixUsageIsRecorded.

@Test
public void testPostfixUsageIsRecorded() {
    parseCode(TEST3);
    List<ASTVariableDeclaratorId> nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
    ASTVariableDeclaratorId node = nodes.get(0);
    Map<NameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations();
    NameDeclaration decl = vars.keySet().iterator().next();
    List<NameOccurrence> usages = vars.get(decl);
    JavaNameOccurrence occ = (JavaNameOccurrence) usages.get(0);
    assertEquals(4, occ.getLocation().getBeginLine());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 2 with NameOccurrence

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

the class MethodScopeTest method testMethodParameterOccurrenceRecorded.

@Test
public void testMethodParameterOccurrenceRecorded() {
    parseCode(TEST1);
    Map<NameDeclaration, List<NameOccurrence>> m = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0).getScope().getDeclarations();
    NameDeclaration vnd = m.keySet().iterator().next();
    assertEquals("bar", vnd.getImage());
    List<NameOccurrence> occs = m.get(vnd);
    NameOccurrence occ = occs.get(0);
    assertEquals(3, occ.getLocation().getBeginLine());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 3 with NameOccurrence

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

the class AcceptanceTest method testEnum.

@Test
public void testEnum() {
    parseCode(NameOccurrencesTest.TEST_ENUM);
    ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
    List<NameOccurrence> usages = vdi.getUsages();
    assertEquals(2, usages.size());
    assertEquals(5, usages.get(0).getLocation().getBeginLine());
    assertEquals(9, usages.get(1).getLocation().getBeginLine());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 4 with NameOccurrence

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

the class VariableAccessVisitor method markUsages.

private List<VariableAccess> markUsages(DataFlowNode inode) {
    // undefinitions was once a field... seems like it works fine as a local
    List<VariableAccess> undefinitions = new ArrayList<>();
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            VariableNameDeclaration vnd = entry.getKey();
            if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
                // add definition for initialized variables
                addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
            }
            undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
            for (NameOccurrence occurrence : entry.getValue()) {
                addAccess((JavaNameOccurrence) occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 5 with NameOccurrence

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

the class CheckSkipResultRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    ASTType typeNode = node.getTypeNode();
    if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
        return data;
    }
    for (NameOccurrence occ : node.getUsages()) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
        if (qualifier != null && "skip".equals(qualifier.getImage())) {
            Node loc = jocc.getLocation();
            if (loc != null) {
                ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
                while (exp != null) {
                    if (exp.jjtGetParent() instanceof ASTStatementExpression) {
                        // if exp is in a bare statement,
                        // the returned value is not used
                        addViolation(data, occ.getLocation());
                        break;
                    } else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
                        // if exp is enclosed in a pair of parenthesis
                        // let's have a look at the enclosing expression
                        // we'll see if it's in a bare statement
                        exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
                    } else {
                        // or assignement so the returned value is used
                        break;
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

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