<No Comment Entered>

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@543 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
David Craine
2002-07-29 13:24:18 +00:00
parent 7f17f681f0
commit 1d34b9b803

View File

@ -7,6 +7,9 @@ import net.sourceforge.pmd.ast.ASTInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTName;
import java.util.*;
import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.RuleContext;
import java.text.MessageFormat;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
public class UnusedFormalParameterRule extends AbstractRule {
@ -23,7 +26,20 @@ public class UnusedFormalParameterRule extends AbstractRule {
* @param paramNames list of param names to check
*/
private void checkParamNames(HashSet paramNames, SimpleNode startNode) {
if (paramNames.isEmpty()) return; //if there are no more paramNames then there's no reason to keep checking
int index = 0;
if (startNode instanceof ASTName) {
String nodeImage = ((ASTName)startNode).getImage();
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++) {
SimpleNode node = (SimpleNode)startNode.jjtGetChild(i);
checkParamNames(paramNames, node);
}
}
}
public Object visit(ASTMethodDeclaration node, Object data) {
@ -34,10 +50,15 @@ public class UnusedFormalParameterRule extends AbstractRule {
if (paramCount == 0) return data; //bail out if now paramters
HashSet paramNames = new HashSet();
for (int i=0; i<paramCount; i++) {
ASTName paramName = (ASTName)formalParams.jjtGetChild(i).jjtGetChild(0).jjtGetChild(0);
paramNames.add(paramName);
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())));
}
checkParamNames(paramNames, md);
}
return data;
}