View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.rules;
    5
    6   import java.util.Iterator;
    7   import java.util.List;
    8   import java.util.Map;
    9
    10  import net.sourceforge.pmd.AbstractRule;
    11  import net.sourceforge.pmd.ast.ASTMethodDeclarator;
    12  import net.sourceforge.pmd.symboltable.NameOccurrence;
    13  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
    14
    15  public class AvoidReassigningParameters extends AbstractRule {
    16
    17      public Object visit(ASTMethodDeclarator node, Object data) {
    18          Map params = node.getScope().getVariableDeclarations();
    19          for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
    20              Map.Entry entry = (Map.Entry) i.next();
    21
    22              VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
    23              List usages = (List) entry.getValue();
    24              for (Iterator j = usages.iterator(); j.hasNext();) {
    25                  NameOccurrence occ = (NameOccurrence) j.next();
    26                  if ((occ.isOnLeftHandSide() || occ.isSelfAssignment()) &&
    27                      occ.getNameForWhichThisIsAQualifier() == null &&
    28                      (!decl.isArray() || occ.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1))
    29                  {
    30                      // not an array or no primary suffix to access the array values
    31                      addViolation(data, decl.getNode(), decl.getImage());
    32                  }
    33              }
    34          }
    35          return super.visit(node, data);
    36      }
    37  }