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);
}
}
}
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());
}
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());
}
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;
}
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;
}
Aggregations