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.codesize;
5   
6   import java.util.logging.Logger;
7   
8   import net.sourceforge.pmd.lang.plsql.ast.ASTFieldDeclaration;
9   import net.sourceforge.pmd.lang.plsql.ast.ASTGlobal;
10  import net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit;
11  import net.sourceforge.pmd.lang.plsql.ast.OracleObject;
12  import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
13  import net.sourceforge.pmd.stat.DataPoint;
14  import net.sourceforge.pmd.util.NumericConstants;
15  
16  /**
17   * Non-commented source statement counter for Oracle Object declarations.
18   * 
19   * @author Stuart Turton
20   */
21  public class NcssObjectCountRule extends AbstractNcssCountRule {
22      private final static String CLASS_NAME =NcssObjectCountRule.class.getName(); 
23      private final static Logger LOGGER = Logger.getLogger(NcssObjectCountRule.class.getName()); 
24  
25      /**
26       * Count type declarations. This includes Oracle Objects. 
27       */
28      public NcssObjectCountRule() {
29  	super(OracleObject.class);
30  	setProperty(MINIMUM_DESCRIPTOR, 1500d);
31      }
32  
33  
34  
35      //@Override
36      public Object visit(OracleObject node, Object data) {
37          LOGGER.entering(CLASS_NAME,"visit(NcssObjectCountRule)");
38          //Treat Schema-level ProgramUnits as Oracle Objects, otherwise as subprograms
39          if (node.jjtGetParent() instanceof  ASTGlobal ) {
40              LOGGER.fine("Schema-level");
41  	    return super.visit(node, data);
42  	}
43  
44          LOGGER.fine("not Schema-level");
45  	return countNodeChildren(node, data);
46      }
47  
48      /** Override super.visit(PLSQLNode, Object) for ASTProgramUnit nodes,
49       *only adding DataPoints for Schema-level Functions and Procedures 
50       */
51      public Object visit(ASTProgramUnit node, Object data) {
52  	int numNodes = 0;
53  
54  	for (int i = 0; i < node.jjtGetNumChildren(); i++) {
55  		PLSQLNode n = (PLSQLNode) node.jjtGetChild(i);
56  	    Integer treeSize = (Integer) n.jjtAccept(this, data);
57  	    numNodes += treeSize.intValue();
58  	}
59  
60          //This override is necessary because only Schema-level OracleObject 
61          //instances should result in DataPoints 
62  	if (node instanceof OracleObject 
63              && node.jjtGetParent() instanceof ASTGlobal
64             ) {
65            
66  	    // Add 1 to account for base node
67  	    numNodes++;
68  	    DataPoint point = new DataPoint();
69  	    point.setNode(node);
70  	    point.setScore(1.0 * numNodes);
71  	    point.setMessage(getMessage());
72  	    addDataPoint(point);
73              LOGGER.fine("Running score is " +  point.getScore());
74  	}
75  
76  	return Integer.valueOf(numNodes);
77      }
78  
79      @Override
80      public Object visit(ASTFieldDeclaration node, Object data) {
81          LOGGER.entering(CLASS_NAME,"visit(ASTFieldDeclaration)");
82  	return NumericConstants.ONE;
83      }
84  
85      @Override
86      public Object[] getViolationParameters(DataPoint point) {
87          LOGGER.entering(CLASS_NAME,"visit(getViolationParameters)");
88          LOGGER.fine("Node Count ==" + point.getScore() );
89  	return new String[] { String.valueOf((int) point.getScore()) };
90      }
91  
92  }