1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules.optimization; |
5 |
| |
6 |
| import java.util.Iterator; |
7 |
| import java.util.List; |
8 |
| import java.util.Map; |
9 |
| |
10 |
| import net.sourceforge.pmd.ast.ASTFormalParameter; |
11 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
12 |
| import net.sourceforge.pmd.ast.AccessNode; |
13 |
| import net.sourceforge.pmd.symboltable.Scope; |
14 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
15 |
| |
16 |
| public class MethodArgumentCouldBeFinal extends AbstractOptimizationRule { |
17 |
| |
18 |
12
| public Object visit(ASTMethodDeclaration meth, Object data) {
|
19 |
12
| if (meth.isNative() || meth.isAbstract()) {
|
20 |
2
| return data;
|
21 |
| } |
22 |
10
| Scope s = meth.getScope();
|
23 |
10
| Map decls = s.getVariableDeclarations();
|
24 |
10
| for (Iterator i = decls.entrySet().iterator(); i.hasNext();) {
|
25 |
15
| Map.Entry entry = (Map.Entry) i.next();
|
26 |
15
| VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
|
27 |
15
| AccessNode node = var.getAccessNodeParent();
|
28 |
15
| if (!node.isFinal() && (node instanceof ASTFormalParameter) && !assigned((List) entry.getValue())) {
|
29 |
7
| addViolation(data, node, var.getImage());
|
30 |
| } |
31 |
| } |
32 |
10
| return data;
|
33 |
| } |
34 |
| |
35 |
| } |