KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > optimization > LocalVariableCouldBeFinal


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd.rules.optimization;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
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     public Object JavaDoc visit(ASTLocalVariableDeclaration node, Object JavaDoc data) {
17         if (node.isFinal()) {
18             return data;
19         }
20         Scope s = node.getScope();
21         Map JavaDoc decls = s.getVariableDeclarations();
22         for (Iterator JavaDoc i = decls.entrySet().iterator(); i.hasNext();) {
23             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
24             VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
25             if (var.getAccessNodeParent() != node) {
26                 continue;
27             }
28             if (!assigned((List JavaDoc) entry.getValue())) {
29                 addViolation(data, var.getAccessNodeParent(), var.getImage());
30             }
31         }
32         return data;
33     }
34
35 }
36
Popular Tags