use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class AssignmentToNonFinalStaticRule method initializedInConstructor.
private boolean initializedInConstructor(List<NameOccurrence> usages) {
boolean initInConstructor = false;
for (NameOccurrence occ : usages) {
// legitimate usages of these with static fields, e.g. typesafe enum pattern.
if (((JavaNameOccurrence) occ).isOnLeftHandSide()) {
Node node = occ.getLocation();
Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
if (constructor != null) {
initInConstructor = true;
}
}
}
return initInConstructor;
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class ClassScopeTest method testMethodUsageSeenWithThis.
@Test
public void testMethodUsageSeenWithThis() {
parseCode(METHOD_USAGE_SEEN_WITH_THIS);
ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
Iterator<Map.Entry<NameDeclaration, List<NameOccurrence>>> i = m.entrySet().iterator();
MethodNameDeclaration mnd;
Map.Entry<NameDeclaration, List<NameOccurrence>> entry;
do {
entry = i.next();
mnd = (MethodNameDeclaration) entry.getKey();
} while (!mnd.getImage().equals("bar"));
List<NameOccurrence> usages = entry.getValue();
assertEquals(1, usages.size());
assertEquals("bar", ((JavaNameOccurrence) usages.get(0)).getImage());
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class ClassScopeTest method testNestedClassFieldAndParameter.
/**
* Test case for bug report #2410201
*/
@Test
public void testNestedClassFieldAndParameter() {
parseCode(NESTED_CLASS_FIELD_AND_PARAM);
ASTMethodDeclaration node = acu.getFirstDescendantOfType(ASTMethodDeclaration.class);
Map<NameDeclaration, List<NameOccurrence>> vd = node.getScope().getDeclarations();
assertEquals(1, vd.size());
for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : vd.entrySet()) {
assertEquals("field", entry.getKey().getImage());
List<NameOccurrence> occurrences = entry.getValue();
assertEquals(2, occurrences.size());
NameOccurrence no1 = occurrences.get(0);
assertEquals(8, no1.getLocation().getBeginLine());
NameOccurrence no2 = occurrences.get(1);
assertEquals(9, no2.getLocation().getBeginLine());
}
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class ClassScopeTest method testMethodUsageSeen.
@Test
public void testMethodUsageSeen() {
parseCode(METHOD_USAGE_SEEN);
ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
Iterator<Map.Entry<NameDeclaration, List<NameOccurrence>>> i = m.entrySet().iterator();
MethodNameDeclaration mnd;
Map.Entry<NameDeclaration, List<NameOccurrence>> entry;
do {
entry = i.next();
mnd = (MethodNameDeclaration) entry.getKey();
} while (!mnd.getImage().equals("bar"));
List<NameOccurrence> usages = entry.getValue();
assertEquals(1, usages.size());
assertEquals("bar", ((JavaNameOccurrence) usages.get(0)).getImage());
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class AcceptanceTest method testDemo.
@Test
public void testDemo() {
parseCode(TEST_DEMO);
// System.out.println(TEST_DEMO);
ASTMethodDeclaration node = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
Scope s = node.getScope();
Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
assertEquals("buz", entry.getKey().getImage());
assertEquals("ArrayList", ((TypedNameDeclaration) entry.getKey()).getTypeImage());
List<NameOccurrence> u = entry.getValue();
assertEquals(1, u.size());
NameOccurrence o = u.get(0);
int beginLine = o.getLocation().getBeginLine();
assertEquals(3, beginLine);
// System.out.println("Variable: " + d.getImage());
// System.out.println("Type: " + d.getTypeImage());
// System.out.println("Usages: " + u.size());
// System.out.println("Used in line " + beginLine);
}
}
Aggregations