1 package net.sourceforge.pmd.rules;
2
3 import net.sourceforge.pmd.ast.ASTCompilationUnit;
4 import net.sourceforge.pmd.ast.ASTFieldDeclaration;
5 import net.sourceforge.pmd.ast.ASTMethodDeclarator;
6 import net.sourceforge.pmd.ast.AccessNode;
7 import net.sourceforge.pmd.rules.design.ExcessiveNodeCountRule;
8
9 /***
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.jjtGetParent());
34 }
35
36 /***
37 * Method counts ONLY public class attributes which are not PUBLIC and
38 * static- these usually represent constants....
39 */
40 public Object visit(ASTFieldDeclaration node, Object data) {
41 if (node.isFinal() && node.isStatic()) {
42 return new Integer(0);
43 } else {
44 return this.getTallyOnAccessType(node);
45 }
46 }
47
48 /***
49 * Method counts a node if it is public
50 * @param AccessNode node
51 * @return Integer 1 if node is public 0 otherwise
52 */
53 private Integer getTallyOnAccessType(AccessNode node) {
54 if (node.isPublic()) {
55 return new Integer(1);
56 }
57 return new Integer(0);
58 }
59 }
This page was automatically generated by Maven