KickJava   Java API By Example, From Geeks To Geeks.

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


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.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     public Object JavaDoc visit(ASTMethodDeclaration meth, Object JavaDoc data) {
19         if (meth.isNative() || meth.isAbstract()) {
20             return data;
21         }
22         Scope s = meth.getScope();
23         Map JavaDoc decls = s.getVariableDeclarations();
24         for (Iterator JavaDoc i = decls.entrySet().iterator(); i.hasNext();) {
25             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
26             VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
27             AccessNode node = var.getAccessNodeParent();
28             if (!node.isFinal() && (node instanceof ASTFormalParameter) && !assigned((List JavaDoc) entry.getValue())) {
29                 addViolation(data, node, var.getImage());
30             }
31         }
32         return data;
33     }
34
35 }
36
Popular Tags