01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.design;
04:
05: import net.sourceforge.pmd.ast.SimpleJavaNode;
06: import net.sourceforge.pmd.stat.DataPoint;
07: import net.sourceforge.pmd.stat.StatisticalRule;
08:
09: /**
10: * This is a common super class for things which
11: * shouldn't have excessive nodes underneath.
12: * <p/>
13: * It expects all "visit" calls to return an
14: * Integer. It will sum all the values it gets,
15: * and use that as its score.
16: * <p/>
17: * To use it, override the "visit" for the nodes that
18: * need to be counted. On those return "new Integer(1)"
19: * <p/>
20: * All others will return 0 (or the sum of counted nodes
21: * underneath.)
22: */
23:
24: public class ExcessiveNodeCountRule extends StatisticalRule {
25: private Class nodeClass;
26:
27: public ExcessiveNodeCountRule(Class nodeClass) {
28: this .nodeClass = nodeClass;
29: }
30:
31: public Object visit(SimpleJavaNode node, Object data) {
32: int numNodes = 0;
33:
34: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
35: Integer treeSize = (Integer) ((SimpleJavaNode) node
36: .jjtGetChild(i)).jjtAccept(this , data);
37: numNodes += treeSize;
38: }
39:
40: if (nodeClass.isInstance(node)) {
41: DataPoint point = new DataPoint();
42: point.setNode(node);
43: point.setScore(1.0 * numNodes);
44: point.setMessage(getMessage());
45: addDataPoint(point);
46: }
47:
48: return Integer.valueOf(numNodes);
49: }
50: }
|