View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.rules.design;
    5
    6   import net.sourceforge.pmd.AbstractRule;
    7   import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
    8   import net.sourceforge.pmd.ast.ASTFieldDeclaration;
    9   import net.sourceforge.pmd.ast.ASTFormalParameter;
    10  import net.sourceforge.pmd.ast.ASTResultType;
    11  import net.sourceforge.pmd.ast.Node;
    12
    13  import java.util.HashSet;
    14  import java.util.Set;
    15
    16  public class LooseCoupling extends AbstractRule {
    17
    18      private Set implClassNames = new HashSet();
    19
    20      public LooseCoupling() {
    21          super();
    22          implClassNames.add("HashSet");
    23          implClassNames.add("HashMap");
    24          implClassNames.add("LinkedHashMap");
    25          implClassNames.add("LinkedHashSet");
    26          implClassNames.add("TreeSet");
    27          implClassNames.add("TreeMap");
    28          implClassNames.add("Vector");
    29          implClassNames.add("java.util.HashSet");
    30          implClassNames.add("java.util.HashMap");
    31          implClassNames.add("java.util.LinkedHashMap");
    32          implClassNames.add("java.util.LinkedHashSet");
    33          implClassNames.add("java.util.TreeSet");
    34          implClassNames.add("java.util.TreeMap");
    35          implClassNames.add("java.util.Vector");
    36      }
    37
    38      public Object visit(ASTClassOrInterfaceType node, Object data) {
    39          Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    40          if (implClassNames.contains(node.getImage()) && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
    41              addViolation(data, node, node.getImage());
    42          }
    43          return data;
    44      }
    45  }