01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.optimization;
04:
05: import java.util.List;
06: import java.util.Map;
07:
08: import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
09: import net.sourceforge.pmd.symboltable.NameOccurrence;
10: import net.sourceforge.pmd.symboltable.Scope;
11: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
12:
13: public class LocalVariableCouldBeFinal extends AbstractOptimizationRule {
14:
15: public Object visit(ASTLocalVariableDeclaration node, Object data) {
16: if (node.isFinal()) {
17: return data;
18: }
19: Scope s = node.getScope();
20: Map<VariableNameDeclaration, List<NameOccurrence>> decls = s
21: .getVariableDeclarations();
22: for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls
23: .entrySet()) {
24: VariableNameDeclaration var = entry.getKey();
25: if (var.getAccessNodeParent() != node) {
26: continue;
27: }
28: if (!assigned(entry.getValue())) {
29: addViolation(data, var.getAccessNodeParent(), var
30: .getImage());
31: }
32: }
33: return data;
34: }
35:
36: }
|