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