01: package org.acm.seguin.pmd.rules;
02:
03: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
04: import net.sourceforge.jrefactory.ast.ASTFieldDeclaration;
05: import net.sourceforge.jrefactory.ast.ASTMethodDeclarator;
06: import net.sourceforge.jrefactory.ast.AccessNode;
07: import org.acm.seguin.pmd.rules.design.ExcessiveNodeCountRule;
08:
09: /**
10: * @author aglover
11: *
12: * Class Name: ExcessivePublicCountRule
13: *
14: * Rule attempts to count all public methods and public attributes defined in a class.
15: *
16: * If a class has a high number of public operations, it might be wise to consider whether
17: * it would be appropriate to divide it into subclasses.
18: *
19: * A large proportion of public members and operations means the class has high potential to be
20: * affected by external classes. Futhermore, increased effort will be required to
21: * thoroughly test the class.
22: */
23: public class ExcessivePublicCountRule extends ExcessiveNodeCountRule {
24:
25: public ExcessivePublicCountRule() {
26: super (ASTCompilationUnit.class);
27: }
28:
29: /**
30: * Method counts ONLY public methods.
31: */
32: public Object visit(ASTMethodDeclarator node, Object data) {
33: return this .getTallyOnAccessType((AccessNode) node
34: .jjtGetParent());
35: }
36:
37: /**
38: * Method counts ONLY public class attributes which are not PUBLIC and
39: * static- these usually represent constants....
40: */
41: public Object visit(ASTFieldDeclaration node, Object data) {
42: if (node.isFinal() && node.isStatic()) {
43: return new Integer(0);
44: } else {
45: return this .getTallyOnAccessType(node);
46: }
47: }
48:
49: /**
50: * Method counts a node if it is public
51: * @param AccessNode node
52: * @return Integer 1 if node is public 0 otherwise
53: */
54: private Integer getTallyOnAccessType(AccessNode node) {
55: if (node.isPublic()) {
56: return new Integer(1);
57: }
58: return new Integer(0);
59: }
60: }
|