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 net.sourceforge.pmd.AbstractRule;
    7   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
    8   import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
    9   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
    10  import net.sourceforge.pmd.ast.Node;
    11  import net.sourceforge.pmd.ast.SimpleNode;
    12  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
    13
    14  import java.util.Iterator;
    15  import java.util.List;
    16  import java.util.Map;
    17
    18  public class UnusedFormalParameterRule extends AbstractRule {
    19
    20      public Object visit(ASTConstructorDeclaration node, Object data) {
    21          check(node, data);
    22          return data;
    23      }
    24
    25      public Object visit(ASTMethodDeclaration node, Object data) {
    26          if (!node.isPrivate() && !hasProperty("checkall")) {
    27              return data;
    28          }
    29          if (!node.isNative()) {
    30              check(node, data);
    31          }
    32          return data;
    33      }
    34
    35      private void check(SimpleNode node, Object data) {
    36          Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    37          if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
    38              Map vars = node.getScope().getVariableDeclarations();
    39              for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
    40                  VariableNameDeclaration nameDecl = (VariableNameDeclaration) i.next();
    41                  if (!((List) vars.get(nameDecl)).isEmpty()) {
    42                      continue;
    43                  }
    44                  addViolation(data, node, new Object[]{node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage()});
    45              }
    46          }
    47      }
    48
    49  }