01: package org.acm.seguin.pmd.rules;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTConstructorDeclaration;
06: import net.sourceforge.jrefactory.ast.ASTUnmodifiedClassDeclaration;
07: import net.sourceforge.jrefactory.ast.ASTGenericNameList;
08: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
09:
10: import java.util.ArrayList;
11: import java.util.List;
12: import java.util.Iterator;
13:
14: public class AtLeastOneConstructorRule extends AbstractRule {
15:
16: public Object visit(ASTUnmodifiedClassDeclaration node, Object data) {
17: List constructors = new ArrayList();
18: node.findChildrenOfType(ASTConstructorDeclaration.class,
19: constructors, false);
20: if (constructors.isEmpty()) {
21: if (checkForSpecalCases(node)) {
22: RuleContext ctx = (RuleContext) data;
23: ctx.getReport().addRuleViolation(
24: createRuleViolation(ctx, node.getBeginLine()));
25: }
26: }
27: return super .visit(node, data);
28: }
29:
30: private boolean checkForSpecalCases(
31: ASTUnmodifiedClassDeclaration node) {
32: List interfaces = new ArrayList();
33: node.findChildrenOfType(ASTGenericNameList.class, interfaces,
34: false);
35: if (interfaces.size() == 0) {
36: return true;
37: }
38: ASTGenericNameList nameList = (ASTGenericNameList) interfaces
39: .get(0);
40: List interfaceNames = new ArrayList();
41: nameList.findChildrenOfType(ASTClassOrInterfaceType.class,
42: interfaceNames, false);
43: for (Iterator i = interfaceNames.iterator(); i.hasNext();) {
44: ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) i
45: .next();
46: if (type.getImage().equals("Comparator")) {
47: return false;
48: } else if (type.getImage().equals("java.util.Comparator")) {
49: return false;
50: }
51: }
52: return true;
53: }
54: }
|