01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules;
04:
05: import net.sourceforge.pmd.AbstractRule;
06: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
07: import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
08: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
09: import net.sourceforge.pmd.ast.Node;
10: import net.sourceforge.pmd.ast.SimpleNode;
11: import net.sourceforge.pmd.symboltable.NameOccurrence;
12: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
13:
14: import java.util.List;
15: import java.util.Map;
16:
17: public class UnusedFormalParameterRule extends AbstractRule {
18:
19: public Object visit(ASTConstructorDeclaration node, Object data) {
20: check(node, data);
21: return data;
22: }
23:
24: public Object visit(ASTMethodDeclaration node, Object data) {
25: if (!node.isPrivate() && !hasProperty("checkall")) {
26: return data;
27: }
28: if (!node.isNative()) {
29: check(node, data);
30: }
31: return data;
32: }
33:
34: private void check(SimpleNode node, Object data) {
35: Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
36: if (parent instanceof ASTClassOrInterfaceDeclaration
37: && !((ASTClassOrInterfaceDeclaration) parent)
38: .isInterface()) {
39: Map<VariableNameDeclaration, List<NameOccurrence>> vars = node
40: .getScope().getVariableDeclarations();
41: for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars
42: .entrySet()) {
43: VariableNameDeclaration nameDecl = entry.getKey();
44: if (actuallyUsed(nameDecl, entry.getValue())) {
45: continue;
46: }
47: addViolation(data, node, new Object[] {
48: node instanceof ASTMethodDeclaration ? "method"
49: : "constructor", nameDecl.getImage() });
50: }
51: }
52: }
53:
54: private boolean actuallyUsed(VariableNameDeclaration nameDecl,
55: List<NameOccurrence> usages) {
56: for (NameOccurrence occ : usages) {
57: if (occ.isOnLeftHandSide()) {
58: if (nameDecl.isArray()
59: && occ.getLocation().jjtGetParent()
60: .jjtGetParent().jjtGetNumChildren() > 1) {
61: // array element access
62: return true;
63: }
64: continue;
65: } else {
66: return true;
67: }
68: }
69: return false;
70: }
71:
72: }
|