1 package net.sourceforge.pmd.rules.design;
2
3 import net.sourceforge.pmd.ast.SimpleNode;
4 import net.sourceforge.pmd.stat.DataPoint;
5 import net.sourceforge.pmd.stat.StatisticalRule;
6
7 /***
8 * This is a common super class for things which
9 * shouldn't have excessive nodes underneath.
10 *
11 * It expects all "visit" calls to return an
12 * Integer. It will sum all the values it gets,
13 * and use that as its score.
14 *
15 * To use it, override the "visit" for the nodes that
16 * need to be counted. On those return "new Integer(1)"
17 *
18 * All others will return 0 (or the sum of counted nodes
19 * underneath.)
20 */
21
22 public class ExcessiveNodeCountRule extends StatisticalRule {
23 private Class nodeClass;
24
25 public ExcessiveNodeCountRule(Class nodeClass) {
26 this.nodeClass = nodeClass;
27 }
28
29 public Object visit(SimpleNode node, Object data) {
30 int numNodes = 0;
31
32 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
33 Integer treeSize = (Integer) (node.jjtGetChild(i)).jjtAccept(this, data);
34 numNodes += treeSize.intValue();
35 }
36
37 if (nodeClass.isInstance(node)) {
38 DataPoint point = new DataPoint();
39 point.setLineNumber(node.getBeginLine());
40 point.setScore(1.0 * numNodes);
41 point.setRule(this);
42 point.setMessage(getMessage());
43 addDataPoint(point);
44 }
45
46 return new Integer(numNodes);
47 }
48 }
This page was automatically generated by Maven