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