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.ASTAnnotation;
8   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
10  import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
11  import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
12  import net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation;
13  import net.sourceforge.pmd.lang.java.ast.ASTName;
14  import net.sourceforge.pmd.lang.java.ast.ASTResultType;
15  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
16  import net.sourceforge.pmd.util.CollectionUtil;
17  
18  public class LooseCouplingRule extends AbstractJavaRule {
19  
20  	// TODO - these should be brought in via external properties
21  //    private static final Set implClassNames = CollectionUtil.asSet( new Object[] {
22  //    	"ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet", "TreeSet", "TreeMap", "Vector",
23  //    	"java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
24  //    	"java.util.LinkedHashMap", "java.util.LinkedHashSet", "java.util.TreeSet",
25  //    	"java.util.TreeMap", "java.util.Vector" 
26  //    	});
27  
28      public Object visit(ASTClassOrInterfaceType node, Object data) {
29          if (methodHasOverride(node)) {
30              return data;
31          }
32          Node parent = node.getNthParent(3);
33          String typeName = node.getImage();
34          if (CollectionUtil.isCollectionType(typeName, false) && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
35              addViolation(data, node, typeName);
36          }
37          return data;
38      }
39  
40      private boolean methodHasOverride(Node node) {
41          ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
42          if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) {
43              ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class);
44              if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) {
45                  ASTName name = marker.getFirstChildOfType(ASTName.class);
46                  if ("Override".equals(name.getImage())) {
47                      return true;
48                  }
49              }
50          }
51          return false;
52      }
53  }