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  import net.sourceforge.pmd.util.CollectionUtil;
13  
14  public class LooseCoupling extends AbstractRule {
15  
16  	// TODO - these should be brought in via external properties
17  //    private static final Set implClassNames = CollectionUtil.asSet( new Object[] {
18  //    	"ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet", "TreeSet", "TreeMap", "Vector",
19  //    	"java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
20  //    	"java.util.LinkedHashMap", "java.util.LinkedHashSet", "java.util.TreeSet",
21  //    	"java.util.TreeMap", "java.util.Vector" 
22  //    	});
23  
24      public LooseCoupling() {
25          super();
26      }
27  
28      public Object visit(ASTClassOrInterfaceType node, Object data) {
29          Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
30          String typeName = node.getImage();
31          if (CollectionUtil.isCollectionType(typeName, false) && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
32              addViolation(data, node, typeName);
33          }
34          return data;
35      }
36  }