use of net.sourceforge.pmd.lang.symboltable.Scope in project pmd by pmd.
the class LocalVariableCouldBeFinalRule method visit.
public Object visit(ASTLocalVariableDeclaration node, Object data) {
if (node.isFinal()) {
return data;
}
Scope s = node.getScope();
Map<VariableNameDeclaration, List<NameOccurrence>> decls = s.getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
VariableNameDeclaration var = entry.getKey();
if (var.getAccessNodeParent() != node) {
continue;
}
if (!assigned(entry.getValue())) {
addViolation(data, var.getAccessNodeParent(), var.getImage());
}
}
return data;
}
use of net.sourceforge.pmd.lang.symboltable.Scope 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);
}
}
use of net.sourceforge.pmd.lang.symboltable.Scope in project pmd by pmd.
the class AcceptanceTest method testCatchBlocks.
@Test
public void testCatchBlocks() {
parseCode(TEST_CATCH_BLOCKS);
ASTCatchStatement c = acu.findDescendantsOfType(ASTCatchStatement.class).get(0);
ASTBlock a = c.findDescendantsOfType(ASTBlock.class).get(0);
Scope s = a.getScope();
Map<NameDeclaration, List<NameOccurrence>> vars = s.getParent().getDeclarations();
assertEquals(1, vars.size());
NameDeclaration v = vars.keySet().iterator().next();
assertEquals("e", v.getImage());
assertEquals(1, (vars.get(v)).size());
}
use of net.sourceforge.pmd.lang.symboltable.Scope 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.Scope in project pmd by pmd.
the class OccurrenceFinder method visit.
@Override
public Object visit(ASTPrimaryExpression node, Object data) {
NameFinder nameFinder = new NameFinder(node);
// Maybe do some sort of State pattern thingy for when NameDeclaration
// is empty/not empty
Set<NameDeclaration> declarations = new HashSet<>();
List<PLSQLNameOccurrence> names = nameFinder.getNames();
for (PLSQLNameOccurrence occ : names) {
Search search = new Search(occ);
if (declarations.isEmpty()) {
// doing the first name lookup
search.execute();
declarations.addAll(search.getResult());
if (declarations.isEmpty()) {
// SymbolNotFoundException
break;
}
} else {
Set<NameDeclaration> additionalDeclarations = new HashSet<>();
for (NameDeclaration decl : declarations) {
// now we've got a scope we're starting with, so work from
// there
Scope scope = decl.getScope();
if (null == scope) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("NameOccurrence has no Scope:" + decl.getClass().getCanonicalName() + "=>" + decl.getImage());
}
break;
}
search.execute(scope);
Set<NameDeclaration> found = search.getResult();
additionalDeclarations.addAll(found);
if (found.isEmpty()) {
// SymbolNotFoundException
break;
}
}
declarations.addAll(additionalDeclarations);
}
}
return super.visit(node, data);
}
Aggregations