refactoring
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1037 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -24,9 +24,9 @@ public class SymbolTableTest extends TestCase {
|
||||
|
||||
public void testAdd() {
|
||||
SymbolTable s = new SymbolTable();
|
||||
s.add(FOO);
|
||||
s.addDeclaration(FOO);
|
||||
try {
|
||||
s.add(FOO);
|
||||
s.addDeclaration(FOO);
|
||||
} catch (RuntimeException e) {
|
||||
return; // cool
|
||||
}
|
||||
@ -36,8 +36,8 @@ public class SymbolTableTest extends TestCase {
|
||||
public void testParentContains() {
|
||||
SymbolTable table = new SymbolTable();
|
||||
table.openScope();
|
||||
table.add(new NameDeclaration(NameDeclarationTest.createNode("bar", 12), Kind.UNKNOWN));
|
||||
table.add(new NameDeclaration(NameDeclarationTest.createNode("baz", 12), Kind.UNKNOWN));
|
||||
table.addDeclaration(new NameDeclaration(NameDeclarationTest.createNode("bar", 12), Kind.UNKNOWN));
|
||||
table.addDeclaration(new NameDeclaration(NameDeclarationTest.createNode("baz", 12), Kind.UNKNOWN));
|
||||
assertTrue(table.getUnusedNameDeclarations().hasNext());
|
||||
table.leaveScope();
|
||||
assertTrue(!table.getUnusedNameDeclarations().hasNext());
|
||||
@ -45,28 +45,28 @@ public class SymbolTableTest extends TestCase {
|
||||
|
||||
public void testRecordUsage() {
|
||||
SymbolTable s = new SymbolTable();
|
||||
s.add(FOO);
|
||||
s.addDeclaration(FOO);
|
||||
assertTrue(s.getUnusedNameDeclarations().hasNext());
|
||||
s.recordOccurrence(new NameOccurrence(FOO.getImage(), FOO.getLine()));
|
||||
s.lookup(new NameOccurrence(FOO.getImage(), FOO.getLine()));
|
||||
assertTrue(!s.getUnusedNameDeclarations().hasNext());
|
||||
}
|
||||
|
||||
public void testRecordOccurrence() {
|
||||
SymbolTable table = new SymbolTable();
|
||||
table.openScope();
|
||||
table.recordOccurrence(new NameOccurrence("bar", 10));
|
||||
table.lookup(new NameOccurrence("bar", 10));
|
||||
assertTrue(!table.getUnusedNameDeclarations().hasNext());
|
||||
}
|
||||
|
||||
public void testRecordOccurrence2() {
|
||||
SymbolTable s = new SymbolTable();
|
||||
s.recordOccurrence(new NameOccurrence("bar", 10));
|
||||
s.lookup(new NameOccurrence("bar", 10));
|
||||
assertTrue(!s.getUnusedNameDeclarations().hasNext());
|
||||
}
|
||||
|
||||
public void testRecordUsageParent() {
|
||||
SymbolTable parent = new SymbolTable();
|
||||
parent.add(FOO);
|
||||
parent.addDeclaration(FOO);
|
||||
parent.openScope();
|
||||
parent.leaveScope();
|
||||
assertEquals(FOO, parent.getUnusedNameDeclarations().next());
|
||||
@ -74,9 +74,9 @@ public class SymbolTableTest extends TestCase {
|
||||
|
||||
public void testRecordUsageParent2() {
|
||||
SymbolTable parent = new SymbolTable();
|
||||
parent.add(FOO);
|
||||
parent.addDeclaration(FOO);
|
||||
parent.openScope();
|
||||
parent.recordOccurrence(new NameOccurrence(FOO.getImage(), FOO.getLine()));
|
||||
parent.lookup(new NameOccurrence(FOO.getImage(), FOO.getLine()));
|
||||
assertTrue(!parent.getUnusedNameDeclarations().hasNext());
|
||||
}
|
||||
}
|
||||
|
@ -16,14 +16,8 @@ public class ContextManager {
|
||||
return (Scope)scopes.get(scopes.size()-1);
|
||||
}
|
||||
|
||||
public void recordOccurrence(NameOccurrence nameOccurrence) {
|
||||
if (occursInHigherScope(nameOccurrence, scopes.size()-1)) {
|
||||
return;
|
||||
}
|
||||
if (!getCurrentScope().contains(nameOccurrence) ) {
|
||||
return;
|
||||
}
|
||||
getCurrentScope().addOccurrence(nameOccurrence);
|
||||
public void lookup(NameOccurrence nameOccurrence) {
|
||||
lookup(nameOccurrence, scopes.size()-1);
|
||||
}
|
||||
|
||||
public void openScope() {
|
||||
@ -34,14 +28,13 @@ public class ContextManager {
|
||||
scopes.remove(scopes.size()-1);
|
||||
}
|
||||
|
||||
private boolean occursInHigherScope(NameOccurrence nameOccurrence, int startingDepth) {
|
||||
if (!((Scope)scopes.get(startingDepth)).contains(nameOccurrence) && startingDepth>1) {
|
||||
return occursInHigherScope(nameOccurrence, startingDepth-1);
|
||||
private void lookup(NameOccurrence nameOccurrence, int startingDepth) {
|
||||
Scope scope = (Scope)scopes.get(startingDepth);
|
||||
if (!scope.contains(nameOccurrence) && startingDepth>1) {
|
||||
lookup(nameOccurrence, startingDepth-1);
|
||||
}
|
||||
if (((Scope)scopes.get(startingDepth)).contains(nameOccurrence)) {
|
||||
((Scope)scopes.get(startingDepth)).addOccurrence(nameOccurrence);
|
||||
return true;
|
||||
if (scope.contains(nameOccurrence)) {
|
||||
scope.addOccurrence(nameOccurrence);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -26,12 +26,12 @@ public class SymbolTable {
|
||||
cm.leaveScope();
|
||||
}
|
||||
|
||||
public void add(NameDeclaration nameDecl) {
|
||||
public void addDeclaration(NameDeclaration nameDecl) {
|
||||
cm.getCurrentScope().addDeclaration(nameDecl);
|
||||
}
|
||||
|
||||
public void recordOccurrence(NameOccurrence nameOccurrence) {
|
||||
cm.recordOccurrence(nameOccurrence);
|
||||
public void lookup(NameOccurrence nameOccurrence) {
|
||||
cm.lookup(nameOccurrence);
|
||||
}
|
||||
|
||||
public Iterator getUnusedNameDeclarations() {
|
||||
|
@ -37,7 +37,7 @@ public class SymbolTableBuilder extends JavaParserVisitorAdapter {
|
||||
public Object visit(ASTVariableDeclaratorId node, Object data) {
|
||||
if (node.jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration) {
|
||||
node.setScope(table.getCurrentScope());
|
||||
table.add(new NameDeclaration(node, Kind.LOCAL_VARIABLE));
|
||||
table.addDeclaration(new NameDeclaration(node, Kind.LOCAL_VARIABLE));
|
||||
}
|
||||
return super.visit(node, data);
|
||||
}
|
||||
@ -47,7 +47,7 @@ public class SymbolTableBuilder extends JavaParserVisitorAdapter {
|
||||
*/
|
||||
public Object visit(ASTName node, Object data) {
|
||||
if (node.jjtGetParent() instanceof ASTPrimaryPrefix) {
|
||||
table.recordOccurrence(new NameOccurrence(getEndName(node), node.getBeginLine()));
|
||||
table.lookup(new NameOccurrence(getEndName(node), node.getBeginLine()));
|
||||
}
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
5
pmd/test-data/SymbolTableBuilderTest.java
Normal file
5
pmd/test-data/SymbolTableBuilderTest.java
Normal file
@ -0,0 +1,5 @@
|
||||
public class SymbolTableBuilderTest {
|
||||
private void foo() {
|
||||
int x = 2;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user