View Javadoc

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