Search in sources :

Example 36 with NameOccurrence

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

the class AcceptanceTest method testEq.

@Test
public void testEq() {
    parseCode(TEST_EQ);
    ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
    ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
    Scope s = method.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(2, m.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        NameDeclaration vnd = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
            assertEquals(1, usages.size());
            assertEquals(3, usages.get(0).getLocation().getBeginLine());
        } else {
            fail("Unkown variable " + vnd);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 37 with NameOccurrence

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

the class AcceptanceTest method testFieldFinder.

@Test
public void testFieldFinder() {
    parseCode(TEST_FIELD);
    // System.out.println(TEST_FIELD);
    ASTVariableDeclaratorId declaration = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
    assertEquals(3, declaration.getBeginLine());
    assertEquals("bbbbbbbbbb", declaration.getImage());
    assertEquals(1, declaration.getUsages().size());
    NameOccurrence no = declaration.getUsages().get(0);
    Node location = no.getLocation();
    assertEquals(6, location.getBeginLine());
// System.out.println("variable " + declaration.getImage() + " is used
// here: " + location.getImage());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 38 with NameOccurrence

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

the class AcceptanceTest method testInnerOuterClass.

@Test
public void testInnerOuterClass() {
    parseCode(TEST_INNER_CLASS);
    ASTVariableDeclaratorId vdi = // get inner class
    acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(1).getFirstDescendantOfType(// get first declaration
    ASTVariableDeclaratorId.class);
    List<NameOccurrence> usages = vdi.getUsages();
    assertEquals(2, usages.size());
    assertEquals(5, usages.get(0).getLocation().getBeginLine());
    assertEquals(10, 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 39 with NameOccurrence

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

the class VariableAccessVisitor method collectDeclarations.

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

Example 40 with NameOccurrence

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

the class ClassScope method addNameOccurrence.

@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
    PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
    Set<NameDeclaration> declarations = findVariableHere(occurrence);
    Map<MethodNameDeclaration, List<NameOccurrence>> methodNames = getMethodDeclarations();
    if (!declarations.isEmpty() && occurrence.isMethodOrConstructorInvocation()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = methodNames.get(decl);
            if (nameOccurrences == null) {
            // TODO may be a class name: Foo.this.super();
            } else {
                nameOccurrences.add(occurrence);
                Node n = occurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    } else if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
        Map<VariableNameDeclaration, List<NameOccurrence>> variableNames = getVariableDeclarations();
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = variableNames.get(decl);
            if (nameOccurrences == null) {
            // TODO may be a class name
            } else {
                nameOccurrences.add(occurrence);
                Node n = occurrence.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.plsql.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) AbstractPLSQLNode(net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode) ArrayList(java.util.ArrayList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) 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