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.ASTLocalVariableDeclaration; |
11 |
| import net.sourceforge.pmd.symboltable.Scope; |
12 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
13 |
| |
14 |
| public class LocalVariableCouldBeFinal extends AbstractOptimizationRule { |
15 |
| |
16 |
12
| public Object visit(ASTLocalVariableDeclaration node, Object data) {
|
17 |
12
| if (node.isFinal()) {
|
18 |
1
| return data;
|
19 |
| } |
20 |
11
| Scope s = node.getScope();
|
21 |
11
| Map decls = s.getVariableDeclarations();
|
22 |
11
| for (Iterator i = decls.entrySet().iterator(); i.hasNext();) {
|
23 |
19
| Map.Entry entry = (Map.Entry) i.next();
|
24 |
19
| VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
|
25 |
19
| if (var.getAccessNodeParent() != node) {
|
26 |
8
| continue;
|
27 |
| } |
28 |
11
| if (!assigned((List) entry.getValue())) {
|
29 |
4
| addViolation(data, var.getAccessNodeParent(), var.getImage());
|
30 |
| } |
31 |
| } |
32 |
11
| return data;
|
33 |
| } |
34 |
| |
35 |
| } |