fixed instance variable checker for outer classes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@37 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland 2002-06-24 21:55:30 +00:00
parent 61a6f5bc1a
commit 77be42ec00
3 changed files with 21 additions and 13 deletions

View File

@ -125,7 +125,6 @@ public class FunctionalTest extends TestCase{
assertEquals(new EmptyIfStmtRule(), ((RuleViolation)report.iterator().next()).getRule());
}
/*
public void testUnusedPrivateInstanceVar1() {
Report report = process(new File(root() + "UnusedPrivateInstanceVar1.java"));
assertEquals(1, report.getSize());
@ -162,13 +161,14 @@ public class FunctionalTest extends TestCase{
Report report = process(new File(root() + "UnusedPrivateInstanceVar8.java"));
assertTrue(report.empty());
}
/*
TODO - this tests unused variables in nested classes
public void testUnusedPrivateInstanceVar9() {
Report report = process(new File(root() + "UnusedPrivateInstanceVar9.java"));
assertEquals(1, report.getSize());
}
TODO - this tests unused variables in nested classes
*/
// TODO
// there's another test here that needs to work, see UnusedPrivateInstanceVar5.java
// 'foo' should be marked as an unused instance variable, but it isn't because the
// anonymous inner classes are broken for that rule because the "doingIDTraversal" is an
// instance variable
// TODO
private Report process(File file) {
PMD p = new PMD();

View File

@ -49,7 +49,7 @@ public class RuleFactory {
list.add(new EmptyIfStmtRule());
list.add(new UnnecessaryConversionTemporaryRule());
list.add(new UnusedLocalVariableRule());
//list.add(new UnusedPrivateInstanceVariableRule());
list.add(new UnusedPrivateInstanceVariableRule());
return list;
}
}

View File

@ -18,6 +18,10 @@ public class UnusedPrivateInstanceVariableRule extends AbstractRule implements R
// need to attach it to the report or the stack or something
// TODO
private boolean doingIDTraversal;
// TODO
// this means we don't process nested or inner classes, which is sloppy
// TODO
private boolean alreadyWorking;
public String getDescription() {
return "Avoid unused private instance variables";
@ -31,20 +35,24 @@ public class UnusedPrivateInstanceVariableRule extends AbstractRule implements R
}
public Object visit(ASTClassBody node, Object data) {
if (alreadyWorking) {
return data;
}
alreadyWorking = true;
doingIDTraversal = true;
Namespace nameSpace = new Namespace();
nameSpaces.push(nameSpace);
nameSpace.addTable();
Object report = super.visit(node, data);
super.visit(node, null);
doingIDTraversal = false;
//System.out.println("data = " + data);
report = super.visit(node, data);
reportUnusedInstanceVars((Report)report, nameSpace.peek());
super.visit(node, null);
reportUnusedInstanceVars((Report)data, nameSpace.peek());
nameSpace.removeTable();
nameSpaces.pop();
return report;
alreadyWorking = false;
return data;
}
public Object visit(ASTVariableDeclaratorId node, Object data) {