From 77be42ec0059015aa678104b8907ff237d3bd53d Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Mon, 24 Jun 2002 21:55:30 +0000 Subject: [PATCH] fixed instance variable checker for outer classes git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@37 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/FunctionalTest.java | 14 +++++++------- pmd/src/net/sourceforge/pmd/RuleFactory.java | 2 +- .../pmd/UnusedPrivateInstanceVariableRule.java | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/FunctionalTest.java b/pmd/regress/test/net/sourceforge/pmd/FunctionalTest.java index 970f9ffd0f..8708a77a6f 100644 --- a/pmd/regress/test/net/sourceforge/pmd/FunctionalTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/FunctionalTest.java @@ -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(); diff --git a/pmd/src/net/sourceforge/pmd/RuleFactory.java b/pmd/src/net/sourceforge/pmd/RuleFactory.java index 736b86e0f4..d919925846 100644 --- a/pmd/src/net/sourceforge/pmd/RuleFactory.java +++ b/pmd/src/net/sourceforge/pmd/RuleFactory.java @@ -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; } } diff --git a/pmd/src/net/sourceforge/pmd/UnusedPrivateInstanceVariableRule.java b/pmd/src/net/sourceforge/pmd/UnusedPrivateInstanceVariableRule.java index 018aea4eca..04e2d1a688 100644 --- a/pmd/src/net/sourceforge/pmd/UnusedPrivateInstanceVariableRule.java +++ b/pmd/src/net/sourceforge/pmd/UnusedPrivateInstanceVariableRule.java @@ -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) {