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