View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.design;
5   
6   import net.sourceforge.pmd.lang.java.ast.JavaNode;
7   import net.sourceforge.pmd.lang.java.rule.AbstractStatisticalJavaRule;
8   import net.sourceforge.pmd.stat.DataPoint;
9   
10  /**
11   * This is a common super class for things which
12   * have excessive length.
13   * <p/>
14   * i.e. LongMethod and LongClass rules.
15   * <p/>
16   * To implement an ExcessiveLength rule, you pass
17   * in the Class of node you want to check, and this
18   * does the rest for you.
19   */
20  public class ExcessiveLengthRule extends AbstractStatisticalJavaRule {
21      private Class<?> nodeClass;
22  
23      public ExcessiveLengthRule(Class<?> nodeClass) {
24  	this.nodeClass = nodeClass;
25      }
26  
27      @Override
28      public Object visit(JavaNode node, Object data) {
29  	if (nodeClass.isInstance(node)) {
30  	    DataPoint point = new DataPoint();
31  	    point.setNode(node);
32  	    point.setScore(1.0 * (node.getEndLine() - node.getBeginLine()));
33  	    point.setMessage(getMessage());
34  	    addDataPoint(point);
35  	}
36  
37  	return node.childrenAccept(this, data);
38      }
39  }