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