01: package org.acm.seguin.pmd.rules.design;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTFieldDeclaration;
06: import net.sourceforge.jrefactory.ast.ASTFormalParameter;
07: import net.sourceforge.jrefactory.ast.ASTName;
08: import net.sourceforge.jrefactory.ast.ASTResultType;
09: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
10: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
11: import net.sourceforge.jrefactory.ast.Node;
12:
13: import java.text.MessageFormat;
14: import java.util.HashSet;
15: import java.util.Set;
16:
17: public class LooseCouplingRule extends AbstractRule {
18:
19: private Set implClassNames = new HashSet();
20:
21: public LooseCouplingRule() {
22: super ();
23: implClassNames.add("HashSet");
24: implClassNames.add("HashMap");
25: implClassNames.add("ArrayList");
26: implClassNames.add("LinkedHashMap");
27: implClassNames.add("LinkedHashSet");
28: implClassNames.add("TreeSet");
29: implClassNames.add("TreeMap");
30: implClassNames.add("Vector");
31: implClassNames.add("java.util.HashSet");
32: implClassNames.add("java.util.HashMap");
33: implClassNames.add("java.util.ArrayList");
34: implClassNames.add("java.util.LinkedHashMap");
35: implClassNames.add("java.util.LinkedHashSet");
36: implClassNames.add("java.util.TreeSet");
37: implClassNames.add("java.util.TreeMap");
38: implClassNames.add("java.util.Vector");
39: }
40:
41: public Object visit(ASTClassOrInterfaceType node, Object data) {
42: Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
43: if (implClassNames.contains(node.getImage())
44: && (parent instanceof ASTFieldDeclaration
45: || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
46: RuleContext ctx = (RuleContext) data;
47: ctx.getReport().addRuleViolation(
48: createRuleViolation(ctx, node.getBeginLine(),
49: MessageFormat.format(getMessage(),
50: new Object[] { node.getImage() })));
51: }
52: return data;
53: }
54: }
|