fixed shadowing bug. this involved a tweak to the grammer to pick up uses of 'this' and 'super'

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@973 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-09-23 16:38:15 +00:00
parent 4abae06d44
commit 79f889f87f
12 changed files with 106 additions and 26 deletions

View File

@ -846,9 +846,9 @@ void PrimaryPrefix() :
{
Literal()
|
"this"
"this" {jjtThis.setUsesThisModifier();}
|
"super" "." <IDENTIFIER>
"super" {jjtThis.setUsesSuperModifier();} "." <IDENTIFIER>
|
"(" Expression() ")"
|

View File

@ -57,9 +57,9 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<pmd reportFile="c:\jdk-1.4.html" rulesetfiles="rulesets/imports.xml" format="html">
<pmd reportFile="c:\jdk-1.4.html" rulesetfiles="rulesets/unusedcode.xml" format="html">
<!--<fileset dir="c:\data\pmd\pmd\src\net\sourceforge\pmd\">-->
<fileset dir="c:\j2sdk1.4.0_01\src\java\net">
<fileset dir="c:\j2sdk1.4.0_01\src\java\util\">
<include name="**/*.java"/>
<!--<exclude name="**/ast**/"/>-->
</fileset>

View File

@ -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

View File

@ -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

View File

@ -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());
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<node.jjtGetNumChildren(); i++) {
SimpleNode child = (SimpleNode)node.jjtGetChild(i);
if (child instanceof ASTClassBodyDeclaration) {
if (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()));
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()));
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
public class UnusedPrivateInstanceVar17 {
private int value;
public UnusedPrivateInstanceVar17(int value) {
this.value=value;
}
}