View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql.rule.design;
5   
6   import java.util.logging.Logger;
7   
8   import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
9   import net.sourceforge.pmd.lang.plsql.rule.AbstractStatisticalPLSQLRule;
10  import net.sourceforge.pmd.stat.DataPoint;
11  
12  /**
13   * This is a common super class for things which
14   * have excessive length.
15   * <p/>
16   * i.e. LongMethod and LongClass rules.
17   * <p/>
18   * To implement an ExcessiveLength rule, you pass
19   * in the Class of node you want to check, and this
20   * does the rest for you.
21   */
22  public class ExcessiveLengthRule extends AbstractStatisticalPLSQLRule {
23      private final static Logger LOGGER = Logger.getLogger(ExcessiveLengthRule.class.getName()); 
24      private Class<?> nodeClass;
25  
26      public ExcessiveLengthRule(Class<?> nodeClass) {
27  	this.nodeClass = nodeClass;
28      }
29  
30      @Override
31      public Object visit(PLSQLNode node, Object data) {
32          //LOGGER.entering(CLASS_PATH,"visit(SimpleNode)");
33          LOGGER.finest("SimpleNode: line " + node.getBeginLine() +", column " + node.getBeginColumn()
34                        + " - is node " + node.getClass().getCanonicalName()
35                        + " instanceof " + this.nodeClass.getClass().getCanonicalName()
36                       );
37  	if (nodeClass.isInstance(node)) {
38              LOGGER.finest("SimpleNode: YES node " + node.getClass().getCanonicalName()
39                            + " IS instanceof " + this.nodeClass.getClass().getCanonicalName()
40                            + " with  length == (" + node.getEndLine() + " - " + node.getBeginLine()
41                            + " == "  + (node.getEndLine() - node.getBeginLine())
42                           );
43  	    DataPoint point = new DataPoint();
44  	    point.setNode(node);
45  	    point.setScore(1.0 * (node.getEndLine() - node.getBeginLine()));
46  	    point.setMessage(getMessage());
47  	    addDataPoint(point);
48              LOGGER.fine("SimpleNode: Score " + point.getScore() + " for " +  this.nodeClass.getCanonicalName() ) ; 
49  	}
50  
51          //LOGGER.exiting(CLASS_PATH,"visit(SimpleNode)");
52  	return node.childrenAccept(this, data);
53      }
54  
55      @Override
56      public Object[] getViolationParameters(DataPoint point) {
57  	return new String[] { String.valueOf((int) point.getScore()) };
58      }
59  }