1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
8 |
| import net.sourceforge.pmd.ast.ASTConstructorDeclaration; |
9 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
10 |
| import net.sourceforge.pmd.ast.Node; |
11 |
| import net.sourceforge.pmd.ast.SimpleNode; |
12 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
13 |
| |
14 |
| import java.util.Iterator; |
15 |
| import java.util.List; |
16 |
| import java.util.Map; |
17 |
| |
18 |
| public class UnusedFormalParameterRule extends AbstractRule { |
19 |
| |
20 |
1
| public Object visit(ASTConstructorDeclaration node, Object data) {
|
21 |
1
| check(node, data);
|
22 |
1
| return data;
|
23 |
| } |
24 |
| |
25 |
9
| public Object visit(ASTMethodDeclaration node, Object data) {
|
26 |
9
| if (!node.isPrivate() && !hasProperty("checkall")) {
|
27 |
5
| return data;
|
28 |
| } |
29 |
4
| if (!node.isNative()) {
|
30 |
4
| check(node, data);
|
31 |
| } |
32 |
4
| return data;
|
33 |
| } |
34 |
| |
35 |
5
| private void check(SimpleNode node, Object data) {
|
36 |
5
| Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
|
37 |
5
| if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
|
38 |
5
| Map vars = node.getScope().getVariableDeclarations();
|
39 |
5
| for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
40 |
5
| VariableNameDeclaration nameDecl = (VariableNameDeclaration) i.next();
|
41 |
5
| if (!((List) vars.get(nameDecl)).isEmpty()) {
|
42 |
2
| continue;
|
43 |
| } |
44 |
3
| addViolation(data, node, new Object[]{node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage()});
|
45 |
| } |
46 |
| } |
47 |
| } |
48 |
| |
49 |
| } |