View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.coupling;
5   
6   import net.sourceforge.pmd.lang.ast.Node;
7   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
8   import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
10  import net.sourceforge.pmd.lang.java.ast.ASTResultType;
11  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
12  import net.sourceforge.pmd.util.CollectionUtil;
13  
14  public class LooseCouplingRule extends AbstractJavaRule {
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 Object visit(ASTClassOrInterfaceType node, Object data) {
25          Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
26          String typeName = node.getImage();
27          if (CollectionUtil.isCollectionType(typeName, false) && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
28              addViolation(data, node, typeName);
29          }
30          return data;
31      }
32  }