1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import java.util.Iterator; |
7 |
| import java.util.List; |
8 |
| import java.util.Map; |
9 |
| |
10 |
| import net.sourceforge.pmd.AbstractRule; |
11 |
| import net.sourceforge.pmd.ast.ASTMethodDeclarator; |
12 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
13 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
14 |
| |
15 |
| public class AvoidReassigningParameters extends AbstractRule { |
16 |
| |
17 |
11
| public Object visit(ASTMethodDeclarator node, Object data) {
|
18 |
11
| Map params = node.getScope().getVariableDeclarations();
|
19 |
11
| for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
|
20 |
11
| Map.Entry entry = (Map.Entry) i.next();
|
21 |
| |
22 |
11
| VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
|
23 |
11
| List usages = (List) entry.getValue();
|
24 |
11
| for (Iterator j = usages.iterator(); j.hasNext();) {
|
25 |
9
| NameOccurrence occ = (NameOccurrence) j.next();
|
26 |
9
| if ((occ.isOnLeftHandSide() || occ.isSelfAssignment()) &&
|
27 |
| occ.getNameForWhichThisIsAQualifier() == null && |
28 |
| (!decl.isArray() || occ.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1)) |
29 |
| { |
30 |
| |
31 |
5
| addViolation(data, decl.getNode(), decl.getImage());
|
32 |
| } |
33 |
| } |
34 |
| } |
35 |
11
| return super.visit(node, data);
|
36 |
| } |
37 |
| } |