diff --git a/pmd/etc/Java1.4-c.jjt b/pmd/etc/Java1.4-c.jjt index cbf1ee662d..5ce58832ed 100644 --- a/pmd/etc/Java1.4-c.jjt +++ b/pmd/etc/Java1.4-c.jjt @@ -846,9 +846,9 @@ void PrimaryPrefix() : { Literal() | - "this" + "this" {jjtThis.setUsesThisModifier();} | - "super" "." + "super" {jjtThis.setUsesSuperModifier();} "." | "(" Expression() ")" | diff --git a/pmd/etc/build.xml b/pmd/etc/build.xml index a03e9c2467..3a980399a3 100644 --- a/pmd/etc/build.xml +++ b/pmd/etc/build.xml @@ -57,9 +57,9 @@ - + - + diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 315af28b76..6c47b1608a 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,7 @@ ???? 2002 - ???: Modified PMD to be built and used with JUnit 3.8.1 and Ant 1.5. +Fixed bug 610018 - StringInstantiationRule now allows for String(byte[], int, int) usage. +Fixed bug 610693 - UnusedPrivateInstanceVariable handles parameter shadowing better. September 12 2002 - 1.0rc2: Added new rules: JUnitSpellingRule, JUnitStaticSuiteRule, StringInstantiationRule diff --git a/pmd/etc/go.bat b/pmd/etc/go.bat index da6634faff..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\newrules.xml +java %MAIN% %TEST_FILE% xml rulesets\unusedcode.xml diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java index f560f2ab5c..93fca04c41 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java @@ -97,4 +97,14 @@ public class UnusedPrivateInstanceVariableRuleTest extends RuleTst { Report report = process("UnusedPrivateInstanceVar15.java", rule); assertTrue(report.isEmpty()); } + + public void test16() throws Throwable { + Report report = process("UnusedPrivateInstanceVar16.java", rule); + assertEquals(1, report.size()); + } + + public void test17() throws Throwable { + Report report = process("UnusedPrivateInstanceVar17.java", rule); + assertTrue(report.isEmpty()); + } } diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTPrimaryPrefix.java b/pmd/src/net/sourceforge/pmd/ast/ASTPrimaryPrefix.java index 323e570791..699117c17e 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTPrimaryPrefix.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTPrimaryPrefix.java @@ -11,6 +11,24 @@ public class ASTPrimaryPrefix extends SimpleNode { super(p, id); } + private boolean usesThisModifier; + private boolean usesSuperModifier; + + public void setUsesThisModifier() { + usesThisModifier = true; + } + + public boolean usesThisModifier() { + return this.usesThisModifier; + } + + public void setUsesSuperModifier() { + usesSuperModifier = true; + } + + public boolean usesSuperModifier() { + return this.usesSuperModifier; + } /** Accept the visitor. **/ public Object jjtAccept(JavaParserVisitor visitor, Object data) { diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java index e70a7044d0..12ae203abb 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java @@ -3026,9 +3026,13 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java break; case THIS: jj_consume_token(THIS); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setUsesThisModifier(); break; case SUPER: jj_consume_token(SUPER); + jjtn000.setUsesSuperModifier(); jj_consume_token(DOT); jj_consume_token(IDENTIFIER); break; diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java index d1bb579c98..064a42fa56 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java @@ -1,4 +1,4 @@ -/* Generated By:JJTree: Do not edit this line. /home/dpeugh/projects/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java */ +/* Generated By:JJTree: Do not edit this line. C:/data/pmd/pmd/src/net/sourceforge/pmd/ast\JavaParserTreeConstants.java */ package net.sourceforge.pmd.ast; diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java index 2cc61cef42..4bad4c20f0 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java @@ -1,4 +1,4 @@ -/* Generated By:JJTree: Do not edit this line. /home/dpeugh/projects/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java */ +/* Generated By:JJTree: Do not edit this line. C:/data/pmd/pmd/src/net/sourceforge/pmd/ast\JavaParserVisitor.java */ package net.sourceforge.pmd.ast; diff --git a/pmd/src/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRule.java b/pmd/src/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRule.java index 8e70966186..babaf9d4ef 100644 --- a/pmd/src/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRule.java @@ -7,6 +7,8 @@ package net.sourceforge.pmd.rules; import java.util.Iterator; import java.util.Stack; +import java.util.HashSet; +import java.util.Set; import java.text.MessageFormat; import net.sourceforge.pmd.ast.*; @@ -34,7 +36,6 @@ public class UnusedPrivateInstanceVariableRule extends UnusedCodeRule { return data; } - public Object visit(ASTClassBody node, Object data) { if (!foundDeclarationsAlready) { foundDeclarationsAlready = true; @@ -43,20 +44,18 @@ public class UnusedPrivateInstanceVariableRule extends UnusedCodeRule { nameSpaces.push(nameSpace); for (int i=0;i 0 && child.jjtGetChild(0) instanceof ASTFieldDeclaration) { - ASTFieldDeclaration field = (ASTFieldDeclaration)child.jjtGetChild(0); - AccessNode access = (AccessNode)field; - if (!access.isPrivate()) { - continue; - } - SimpleNode target = (SimpleNode)field.jjtGetChild(1).jjtGetChild(0); - if (target.getImage() != null && target.getImage().equals("serialVersionUID")) { - continue; - } - Namespace group = (Namespace)nameSpaces.peek(); - group.peek().add(new Symbol(target.getImage(), target.getBeginLine())); + if (child instanceof ASTClassBodyDeclaration && child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTFieldDeclaration) { + ASTFieldDeclaration field = (ASTFieldDeclaration)child.jjtGetChild(0); + AccessNode access = (AccessNode)field; + if (!access.isPrivate()) { + continue; } + SimpleNode target = (SimpleNode)field.jjtGetChild(1).jjtGetChild(0); + if (target.getImage() != null && target.getImage().equals("serialVersionUID")) { + continue; + } + Namespace group = (Namespace)nameSpaces.peek(); + group.peek().add(new Symbol(target.getImage(), target.getBeginLine())); } } } @@ -64,24 +63,53 @@ public class UnusedPrivateInstanceVariableRule extends UnusedCodeRule { return data; } + private Set params = new HashSet(); + + public Object visit(ASTMethodDeclaration node, Object data) { + super.visit(node, data); + params.clear(); + return data; + } + + public Object visit(ASTConstructorDeclaration node, Object data) { + super.visit(node, data); + params.clear(); + return data; + } + + public Object visit(ASTFormalParameter node, Object data) { + ASTVariableDeclaratorId paramName = (ASTVariableDeclaratorId)node.jjtGetChild(1); + params.add(paramName.getImage()); + return data; + } + public Object visit(ASTPrimarySuffix node, Object data) { if ((node.jjtGetParent() instanceof ASTPrimaryExpression) && (node.getImage() != null)) { - recordPossibleUsage(node); + ASTPrimaryExpression parent = (ASTPrimaryExpression)node.jjtGetParent(); + + boolean force = false; + if (parent.jjtGetChild(0) instanceof ASTPrimaryPrefix) { + ASTPrimaryPrefix prefix = (ASTPrimaryPrefix)parent.jjtGetChild(0); + force = prefix.usesThisModifier(); + } + recordPossibleUsage(node, force); } return super.visit(node, data); } public Object visit(ASTName node, Object data) { if ((node.jjtGetParent() instanceof ASTPrimaryPrefix)) { - recordPossibleUsage(node); + recordPossibleUsage(node, false); } return super.visit(node, data); } - private void recordPossibleUsage(SimpleNode node) { + private void recordPossibleUsage(SimpleNode node, boolean force) { String otherImg = (node.getImage().indexOf('.') == -1) ? node.getImage() : node.getImage().substring(node.getImage().indexOf('.')+1); Namespace group = (Namespace)nameSpaces.peek(); - group.peek().recordPossibleUsageOf(new Symbol(getEndName(node.getImage()), node.getBeginLine())); - group.peek().recordPossibleUsageOf(new Symbol(otherImg, node.getBeginLine())); + if ((!params.contains(getEndName(node.getImage())) && !params.contains(otherImg)) || force) { + group.peek().recordPossibleUsageOf(new Symbol(getEndName(node.getImage()), node.getBeginLine())); + group.peek().recordPossibleUsageOf(new Symbol(otherImg, node.getBeginLine())); + } } } diff --git a/pmd/test-data/UnusedPrivateInstanceVar16.java b/pmd/test-data/UnusedPrivateInstanceVar16.java new file mode 100644 index 0000000000..d04d2e0dc9 --- /dev/null +++ b/pmd/test-data/UnusedPrivateInstanceVar16.java @@ -0,0 +1,10 @@ +public class UnusedPrivateInstanceVar16 { + + // this field is NOT used. + private int value = 0; + + // but the param with the same name is. + public int doSomething(int value) { + return value + 1; + } +} \ No newline at end of file diff --git a/pmd/test-data/UnusedPrivateInstanceVar17.java b/pmd/test-data/UnusedPrivateInstanceVar17.java new file mode 100644 index 0000000000..eabf5b6d01 --- /dev/null +++ b/pmd/test-data/UnusedPrivateInstanceVar17.java @@ -0,0 +1,8 @@ +public class UnusedPrivateInstanceVar17 { + + private int value; + + public UnusedPrivateInstanceVar17(int value) { + this.value=value; + } +} \ No newline at end of file