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.Rule;
    8   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
    9   import net.sourceforge.pmd.symboltable.MethodNameDeclaration;
    10
    11  import java.util.HashSet;
    12  import java.util.Iterator;
    13  import java.util.Map;
    14  import java.util.Set;
    15
    16  public class SymbolTableTestRule extends AbstractRule implements Rule {
    17
    18      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    19          Map methods = node.getScope().getEnclosingClassScope().getMethodDeclarations();
    20          Set suffixes = new HashSet();
    21          for (Iterator i = methods.keySet().iterator(); i.hasNext();) {
    22              MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
    23              String suffix = findSuffix(mnd);
    24              if (suffix != null) {
    25                  if (suffixes.contains(suffix)) {
    26                      addViolation(data, mnd.getNode(), suffix);
    27                  }
    28                  suffixes.add(suffix);
    29              }
    30          }
    31          return data;
    32      }
    33
    34      private String findSuffix(MethodNameDeclaration mnd) {
    35          String end = null;
    36          if (mnd.getImage().startsWith("is")) {
    37              end = mnd.getImage().substring(2);
    38          } else if (mnd.getImage().startsWith("get")) {
    39              end = mnd.getImage().substring(3);
    40          }
    41          return end;
    42      }
    43
    44  }