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