diff --git a/pmd/etc/Java1.4-c.jjt b/pmd/etc/Java1.4-c.jjt index c6b2e3525d..62ee607a04 100644 --- a/pmd/etc/Java1.4-c.jjt +++ b/pmd/etc/Java1.4-c.jjt @@ -391,7 +391,14 @@ void ClassBody() : void NestedClassDeclaration() : {} { - ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp")* + ("static" { ((AccessNode) jjtThis).setStatic( true ); } + | "abstract" { ((AccessNode) jjtThis).setAbstract( true ); } + | "final" { ((AccessNode) jjtThis).setFinal( true ); } + | "public" { ((AccessNode) jjtThis).setPublic( true ); } + | "protected" { ((AccessNode) jjtThis).setProtected( true ); } + | "private" { ((AccessNode) jjtThis).setPrivate( true ); } + | "strictfp" { ((AccessNode) jjtThis).setStrict( true ); } + )* UnmodifiedClassDeclaration() } diff --git a/pmd/etc/build.xml b/pmd/etc/build.xml index 7881e9e2f2..a0a9f49b04 100644 --- a/pmd/etc/build.xml +++ b/pmd/etc/build.xml @@ -38,7 +38,8 @@ - + + diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 0023ccb022..2b21e9a4ec 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -4,6 +4,7 @@ Changed sorting of RuleViolations to group Files together. Changed XML Renderer to improved format. Created DVSL Stylesheet for the new format. Fixed bug 580093 - OverrideBothEqualsAndHashcodeRule reports a more correct line number. +Fixed bug 581853 - UnusedLocalVariableRule now handles anonymous inner classes correctly. July 10 2002 - 0.4: Added new rules: OverrideBothEqualsAndHashcodeRule, EmptyTryBlock, EmptyFinallyBlock diff --git a/pmd/etc/go.bat b/pmd/etc/go.bat index 7ef93f9c6a..705dafad3c 100755 --- a/pmd/etc/go.bat +++ b/pmd/etc/go.bat @@ -2,4 +2,4 @@ set MAIN=net.sourceforge.pmd.PMD set TEST_FILE=c:\\data\\pmd\\pmd\\test-data\\%1%.java -java %MAIN% %TEST_FILE% xml rulesets\new_for_0_5.xml +java %MAIN% %TEST_FILE% xml rulesets\unusedcode.xml diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java index 90adda6459..ff96ef3757 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java @@ -75,10 +75,7 @@ public class UnusedLocalVariableTest extends RuleTst { public void testUnusedLocal9() throws Throwable { Report report = process("Unused9.java", rule); - assertEquals(2, report.size()); - Iterator i = report.iterator(); - assertEquals(rule, ((RuleViolation)i.next()).getRule()); - assertEquals(rule, ((RuleViolation)i.next()).getRule()); + assertTrue(report.isEmpty()); } /* diff --git a/pmd/rulesets/new_for_0_5.xml b/pmd/rulesets/new_for_0_5.xml index f49df4f4f3..35c831aa14 100644 --- a/pmd/rulesets/new_for_0_5.xml +++ b/pmd/rulesets/new_for_0_5.xml @@ -5,7 +5,7 @@ These are new ones for 0.5 - diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTClassBodyDeclaration.java b/pmd/src/net/sourceforge/pmd/ast/ASTClassBodyDeclaration.java index 91a2bf3447..13c207b1aa 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTClassBodyDeclaration.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTClassBodyDeclaration.java @@ -2,7 +2,7 @@ package net.sourceforge.pmd.ast; -public class ASTClassBodyDeclaration extends SimpleNode { +public class ASTClassBodyDeclaration extends AccessNode { public ASTClassBodyDeclaration(int id) { super(id); } diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTNestedClassDeclaration.java b/pmd/src/net/sourceforge/pmd/ast/ASTNestedClassDeclaration.java index 0a8483dc07..7a63f13721 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTNestedClassDeclaration.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTNestedClassDeclaration.java @@ -2,7 +2,7 @@ package net.sourceforge.pmd.ast; -public class ASTNestedClassDeclaration extends SimpleNode { +public class ASTNestedClassDeclaration extends AccessNode { public ASTNestedClassDeclaration(int id) { super(id); } diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java index 77d07ed8ff..83d791c0de 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java @@ -397,24 +397,31 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STATIC: jj_consume_token(STATIC); + ((AccessNode) jjtn000).setStatic( true ); break; case ABSTRACT: jj_consume_token(ABSTRACT); + ((AccessNode) jjtn000).setAbstract( true ); break; case FINAL: jj_consume_token(FINAL); + ((AccessNode) jjtn000).setFinal( true ); break; case PUBLIC: jj_consume_token(PUBLIC); + ((AccessNode) jjtn000).setPublic( true ); break; case PROTECTED: jj_consume_token(PROTECTED); + ((AccessNode) jjtn000).setProtected( true ); break; case PRIVATE: jj_consume_token(PRIVATE); + ((AccessNode) jjtn000).setPrivate( true ); break; case STRICTFP: jj_consume_token(STRICTFP); + ((AccessNode) jjtn000).setStrict( true ); break; default: jj_la1[11] = jj_gen; @@ -4932,64 +4939,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return retval; } - final private boolean jj_3R_296() { - if (jj_scan_token(LSHIFT)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_279() { - if (jj_3R_301()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_271() { - Token xsp; - xsp = jj_scanpos; - if (jj_3R_296()) { - jj_scanpos = xsp; - if (jj_3R_297()) { - jj_scanpos = xsp; - if (jj_3R_298()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_3R_252()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_273() { - if (jj_scan_token(GT)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_278() { - if (jj_3R_239()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_299() { - if (jj_scan_token(PLUS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_212() { - if (jj_scan_token(ORASSIGN)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_277() { - if (jj_3R_238()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_265() { Token xsp; xsp = jj_scanpos; @@ -6321,12 +6270,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_308() { - if (jj_scan_token(STRICTFP)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3_8() { Token xsp; while (true) { @@ -6431,12 +6374,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_307() { - if (jj_scan_token(PRIVATE)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_81() { if (jj_scan_token(PUBLIC)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6532,12 +6469,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_306() { - if (jj_scan_token(PROTECTED)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_73() { if (jj_scan_token(FINAL)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6600,12 +6531,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_305() { - if (jj_scan_token(PUBLIC)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_72() { if (jj_scan_token(ABSTRACT)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6650,14 +6575,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_310() { - if (jj_scan_token(IMPLEMENTS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_3R_324()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_48() { Token xsp; while (true) { @@ -6720,12 +6637,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_304() { - if (jj_scan_token(FINAL)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_45() { Token xsp; xsp = jj_scanpos; @@ -6758,6 +6669,14 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_310() { + if (jj_scan_token(IMPLEMENTS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_3R_324()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3_4() { Token xsp; while (true) { @@ -6838,32 +6757,12 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_303() { - if (jj_scan_token(ABSTRACT)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_259() { if (jj_3R_267()) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; return false; } - final private boolean jj_3R_68() { - if (jj_scan_token(STRICTFP)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_309() { - if (jj_scan_token(EXTENDS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_3R_47()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_258() { if (jj_3R_266()) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6916,6 +6815,56 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_68() { + if (jj_scan_token(STRICTFP)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_309() { + if (jj_scan_token(EXTENDS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_3R_47()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_308() { + if (jj_scan_token(STRICTFP)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_307() { + if (jj_scan_token(PRIVATE)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_306() { + if (jj_scan_token(PROTECTED)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_305() { + if (jj_scan_token(PUBLIC)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_304() { + if (jj_scan_token(FINAL)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_303() { + if (jj_scan_token(ABSTRACT)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_302() { if (jj_scan_token(STATIC)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6972,20 +6921,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_245() { - if (jj_scan_token(LBRACE)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - Token xsp; - while (true) { - xsp = jj_scanpos; - if (jj_3R_250()) { jj_scanpos = xsp; break; } - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } - if (jj_scan_token(RBRACE)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_380() { if (jj_scan_token(FINALLY)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7008,12 +6943,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_66() { - if (jj_scan_token(FINAL)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_226() { if (jj_scan_token(TRY)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7031,6 +6960,26 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_245() { + if (jj_scan_token(LBRACE)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + Token xsp; + while (true) { + xsp = jj_scanpos; + if (jj_3R_250()) { jj_scanpos = xsp; break; } + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } + if (jj_scan_token(RBRACE)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_66() { + if (jj_scan_token(FINAL)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_175() { if (jj_scan_token(CLASS)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7048,47 +6997,12 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_42() { - Token xsp; - xsp = jj_scanpos; - if (jj_3R_65()) { - jj_scanpos = xsp; - if (jj_3R_66()) { - jj_scanpos = xsp; - if (jj_3R_67()) { - jj_scanpos = xsp; - if (jj_3R_68()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - - final private boolean jj_3R_65() { - if (jj_scan_token(ABSTRACT)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_375() { if (jj_3R_387()) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; return false; } - final private boolean jj_3_1() { - Token xsp; - while (true) { - xsp = jj_scanpos; - if (jj_3R_42()) { jj_scanpos = xsp; break; } - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - } - if (jj_scan_token(CLASS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_225() { if (jj_scan_token(SYNCHRONIZED)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7115,6 +7029,29 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_42() { + Token xsp; + xsp = jj_scanpos; + if (jj_3R_65()) { + jj_scanpos = xsp; + if (jj_3R_66()) { + jj_scanpos = xsp; + if (jj_3R_67()) { + jj_scanpos = xsp; + if (jj_3R_68()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_65() { + if (jj_scan_token(ABSTRACT)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_224() { if (jj_scan_token(THROW)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7125,6 +7062,18 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3_1() { + Token xsp; + while (true) { + xsp = jj_scanpos; + if (jj_3R_42()) { jj_scanpos = xsp; break; } + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } + if (jj_scan_token(CLASS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_398() { if (jj_scan_token(COMMA)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -8544,6 +8493,64 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_296() { + if (jj_scan_token(LSHIFT)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_279() { + if (jj_3R_301()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_271() { + Token xsp; + xsp = jj_scanpos; + if (jj_3R_296()) { + jj_scanpos = xsp; + if (jj_3R_297()) { + jj_scanpos = xsp; + if (jj_3R_298()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_3R_252()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_273() { + if (jj_scan_token(GT)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_278() { + if (jj_3R_239()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_299() { + if (jj_scan_token(PLUS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_212() { + if (jj_scan_token(ORASSIGN)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + + final private boolean jj_3R_277() { + if (jj_3R_238()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + public JavaParserTokenManager token_source; JavaCharStream jj_input_stream; public Token token, jj_nt; diff --git a/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java b/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java index dd9063e4c7..b2c6739181 100644 --- a/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java @@ -32,7 +32,7 @@ public class UnnecessaryCastRule extends AbstractRule { try { if (inCastCtx) { TypeSet t = new TypeSet(); - //System.out.println(t.findClass(node.getImage())); + System.out.println(t.findClass(node.getImage())); } } catch (Exception e) {} return super.visit(node, data); @@ -42,7 +42,7 @@ public class UnnecessaryCastRule extends AbstractRule { try { if (inCastCtx) { TypeSet t = new TypeSet(); - //System.out.println(t.findClass(node.getImage())); + System.out.println(t.findClass(node.getImage())); } } catch (Exception e) {} return super.visit(node, data); diff --git a/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java b/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java index 553efee31f..bc3dae40fb 100644 --- a/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java @@ -14,14 +14,7 @@ import net.sourceforge.pmd.*; public class UnusedLocalVariableRule extends AbstractRule implements Rule{ - private Stack tableGroups = new Stack(); - - public Object createGroup(SimpleNode node, Object data) { - tableGroups.push(new Namespace()); - Object report = super.visit(node, data); - tableGroups.pop(); - return report; - } + private Stack nameSpaces; /** * Skip interfaces because they don't have local variables. @@ -31,10 +24,13 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{ } /** - * A new ClassBody triggers a new namespace + * For the purpose of local variables, only an ASTCompilation unit creates a new namespace */ - public Object visit(ASTClassBody node, Object data) { - return createGroup(node, data); + public Object visit(ASTCompilationUnit node, Object data) { + nameSpaces = new Stack(); + createNamespace(node, data); + nameSpaces.clear(); + return data; } // these AST types trigger creation of a new symbol table scope @@ -52,7 +48,7 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{ if (!(node.jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration)) { return super.visit(node, data); } - Namespace group = (Namespace)tableGroups.peek(); + Namespace group = (Namespace)nameSpaces.peek(); group.peek().add(new Symbol(node.getImage(), node.getBeginLine())); return super.visit(node, data); } @@ -61,7 +57,7 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{ //System.out.println("ASTName.getImage() = " + node.getImage() + "; " + node.getBeginLine()); if (node.jjtGetParent() instanceof ASTPrimaryPrefix) { String img = (node.getImage().indexOf('.') == -1) ? node.getImage() : node.getImage().substring(0, node.getImage().indexOf('.')); - Namespace group = (Namespace)tableGroups.peek(); + Namespace group = (Namespace)nameSpaces.peek(); group.peek().recordPossibleUsageOf(new Symbol(img, node.getBeginLine())); } return super.visit(node, data); @@ -77,7 +73,7 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{ private Object addTable(SimpleNode node, Object data) { RuleContext ctx = (RuleContext)data; - Namespace group = (Namespace)tableGroups.peek(); + Namespace group = (Namespace)nameSpaces.peek(); group.addTable(); super.visit(node, ctx); reportUnusedLocals(ctx, group.peek()); @@ -85,4 +81,11 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{ return ctx; } + private Object createNamespace(SimpleNode node, Object data) { + nameSpaces.push(new Namespace()); + Object report = super.visit(node, data); + nameSpaces.pop(); + return report; + } + } diff --git a/pmd/test-data/Unused1.java b/pmd/test-data/Unused1.java index 849fbcec12..6a0094877d 100644 --- a/pmd/test-data/Unused1.java +++ b/pmd/test-data/Unused1.java @@ -4,6 +4,6 @@ public class Unused1 { try { FileReader fr = new FileReader("/dev/null"); // howdy - } catch (Exception e) {e=null;} + } catch (Exception e) {} } } diff --git a/pmd/test-data/Unused9.java b/pmd/test-data/Unused9.java index 6e229631a5..3b0b496f19 100644 --- a/pmd/test-data/Unused9.java +++ b/pmd/test-data/Unused9.java @@ -1,10 +1,11 @@ public class Unused9 { -public void foo() { - String x = "baf"; - bar(new Runnable() { - public void run() {String x = "buz";} -}); -} -public void bar(Runnable r) { -} + public void foo() { + final String x = "baf"; + new Runnable() { + public void run() { + System.out.println(x); + } + }; + } + }