Fixed problem with detecting object useage of param as well as problem with native methods.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@551 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
David Craine
2002-07-29 18:40:47 +00:00
parent 0affb68053
commit 2e94f97267

View File

@ -10,6 +10,7 @@ import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.RuleContext;
import java.text.MessageFormat;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
public class UnusedFormalParameterRule extends AbstractRule {
@ -30,12 +31,18 @@ public class UnusedFormalParameterRule extends AbstractRule {
int index = 0;
if (startNode instanceof ASTName) {
String nodeImage = ((ASTName)startNode).getImage();
//check to see if there is a "." in the image which indicates a method call on this variable name
int index2 = nodeImage.indexOf('.');
if (index2 != -1) {
nodeImage = nodeImage.substring(0, index2); //set the node Image to the prefix
}
if (paramNames.contains(nodeImage)) {
paramNames.remove(nodeImage); //the name is used so let's remove it from the list
}
}
else if (startNode.jjtGetNumChildren() > 0) {
for (int i=0; i<startNode.jjtGetNumChildren(); i++) {
int limit = startNode.jjtGetNumChildren();
for (int i=0; i<limit; i++) {
SimpleNode node = (SimpleNode)startNode.jjtGetChild(i);
checkParamNames(paramNames, node);
}
@ -53,14 +60,28 @@ public class UnusedFormalParameterRule extends AbstractRule {
ASTVariableDeclaratorId paramName = (ASTVariableDeclaratorId)formalParams.jjtGetChild(i).jjtGetChild(1);
paramNames.add(paramName.getImage());
}
checkParamNames(paramNames, (SimpleNode)node.jjtGetChild(2)); //check the block node for the occurence of the parameter names
if (!paramNames.isEmpty()) { //there are still names left in the set so the must not have been used
RuleContext ctx = (RuleContext)data;
//System.out.println(paramNames);
ctx.getReport().addRuleViolation(createRuleViolation(ctx, md.getBeginLine(), MessageFormat.format(getMessage(), paramNames.toArray())));
//find the block node
int j = 2;
SimpleNode blockNode =(SimpleNode)node.jjtGetChild(j);
int childCount = node.jjtGetNumChildren();
while (j <= childCount) {
if (blockNode instanceof ASTBlock)
break;
blockNode =(SimpleNode)node.jjtGetChild(++j);
}
if (blockNode instanceof ASTBlock) {
checkParamNames(paramNames, blockNode); //check the block node for the occurence of the parameter names
if (!paramNames.isEmpty()) { //there are still names left in the set so the must not have been used
RuleContext ctx = (RuleContext)data;
System.out.println(paramNames);
ctx.getReport().addRuleViolation(createRuleViolation(ctx, md.getBeginLine(), MessageFormat.format(getMessage(), paramNames.toArray())));
}
}
}
return data;
}
}