01: package org.acm.seguin.pmd.rules.design;
02:
03: import net.sourceforge.jrefactory.ast.SimpleNode;
04: import org.acm.seguin.pmd.stat.DataPoint;
05: import org.acm.seguin.pmd.stat.StatisticalRule;
06:
07: /**
08: * This is a common super class for things which
09: * 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))
34: .jjtAccept(this , data);
35: numNodes += treeSize.intValue();
36: }
37:
38: if (nodeClass.isInstance(node)) {
39: DataPoint point = new DataPoint();
40: point.setLineNumber(node.getBeginLine());
41: point.setScore(1.0 * numNodes);
42: point.setRule(this );
43: point.setMessage(getMessage());
44: addDataPoint(point);
45: }
46:
47: return new Integer(numNodes);
48: }
49: }
|