|
1 |
| package net.sourceforge.pmd.rules.codesize; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.ast.ASTBreakStatement; |
|
4 |
| import net.sourceforge.pmd.ast.ASTCatchStatement; |
|
5 |
| import net.sourceforge.pmd.ast.ASTContinueStatement; |
|
6 |
| import net.sourceforge.pmd.ast.ASTDoStatement; |
|
7 |
| import net.sourceforge.pmd.ast.ASTFinallyStatement; |
|
8 |
| import net.sourceforge.pmd.ast.ASTForInit; |
|
9 |
| import net.sourceforge.pmd.ast.ASTForStatement; |
|
10 |
| import net.sourceforge.pmd.ast.ASTIfStatement; |
|
11 |
| import net.sourceforge.pmd.ast.ASTLabeledStatement; |
|
12 |
| import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; |
|
13 |
| import net.sourceforge.pmd.ast.ASTReturnStatement; |
|
14 |
| import net.sourceforge.pmd.ast.ASTStatementExpression; |
|
15 |
| import net.sourceforge.pmd.ast.ASTStatementExpressionList; |
|
16 |
| import net.sourceforge.pmd.ast.ASTSwitchLabel; |
|
17 |
| import net.sourceforge.pmd.ast.ASTSwitchStatement; |
|
18 |
| import net.sourceforge.pmd.ast.ASTSynchronizedStatement; |
|
19 |
| import net.sourceforge.pmd.ast.ASTThrowStatement; |
|
20 |
| import net.sourceforge.pmd.ast.ASTWhileStatement; |
|
21 |
| import net.sourceforge.pmd.ast.SimpleJavaNode; |
|
22 |
| import net.sourceforge.pmd.stat.DataPoint; |
|
23 |
| import net.sourceforge.pmd.stat.StatisticalRule; |
|
24 |
| import net.sourceforge.pmd.util.NumericConstants; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public abstract class AbstractNcssCount extends StatisticalRule { |
|
33 |
| |
|
34 |
| private Class nodeClass; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
39
| protected AbstractNcssCount(Class nodeClass) {
|
|
43 |
39
| this.nodeClass = nodeClass;
|
|
44 |
| } |
|
45 |
| |
|
46 |
306
| public Object visit(SimpleJavaNode node, Object data) {
|
|
47 |
306
| int numNodes = 0;
|
|
48 |
| |
|
49 |
306
| for ( int i = 0; i < node.jjtGetNumChildren(); i++ ) {
|
|
50 |
367
| SimpleJavaNode simpleNode = (SimpleJavaNode) node.jjtGetChild( i );
|
|
51 |
367
| Integer treeSize = (Integer) simpleNode.jjtAccept( this, data );
|
|
52 |
367
| numNodes += treeSize.intValue();
|
|
53 |
| } |
|
54 |
| |
|
55 |
306
| if ( this.nodeClass.isInstance( node ) ) {
|
|
56 |
| |
|
57 |
12
| numNodes++;
|
|
58 |
12
| DataPoint point = new DataPoint();
|
|
59 |
12
| point.setNode( node );
|
|
60 |
12
| point.setScore( 1.0 * numNodes );
|
|
61 |
12
| point.setMessage( getMessage() );
|
|
62 |
12
| addDataPoint( point );
|
|
63 |
| } |
|
64 |
| |
|
65 |
306
| return new Integer( numNodes );
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
4
| protected Integer countNodeChildren(SimpleJavaNode node, Object data) {
|
|
79 |
4
| Integer nodeCount = null;
|
|
80 |
4
| int lineCount = 0;
|
|
81 |
4
| for ( int i = 0; i < node.jjtGetNumChildren(); i++ ) {
|
|
82 |
12
| nodeCount = (Integer) ( (SimpleJavaNode) node.jjtGetChild( i ) ).jjtAccept(
|
|
83 |
| this, data ); |
|
84 |
12
| lineCount += nodeCount.intValue();
|
|
85 |
| } |
|
86 |
4
| return new Integer( ++lineCount );
|
|
87 |
| } |
|
88 |
| |
|
89 |
0
| public Object visit(ASTForStatement node, Object data) {
|
|
90 |
0
| return countNodeChildren( node, data );
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public Object visit(ASTDoStatement node, Object data) {
|
|
94 |
0
| return countNodeChildren( node, data );
|
|
95 |
| } |
|
96 |
| |
|
97 |
0
| public Object visit(ASTIfStatement node, Object data) {
|
|
98 |
| |
|
99 |
0
| Integer lineCount = countNodeChildren( node, data );
|
|
100 |
| |
|
101 |
0
| if ( node.hasElse() ) {
|
|
102 |
0
| int lines = lineCount.intValue();
|
|
103 |
0
| lines++;
|
|
104 |
0
| lineCount = new Integer( lines );
|
|
105 |
| } |
|
106 |
| |
|
107 |
0
| return lineCount;
|
|
108 |
| } |
|
109 |
| |
|
110 |
0
| public Object visit(ASTWhileStatement node, Object data) {
|
|
111 |
0
| return countNodeChildren( node, data );
|
|
112 |
| } |
|
113 |
| |
|
114 |
0
| public Object visit(ASTBreakStatement node, Object data) {
|
|
115 |
0
| return NumericConstants.ONE;
|
|
116 |
| } |
|
117 |
| |
|
118 |
0
| public Object visit(ASTCatchStatement node, Object data) {
|
|
119 |
0
| return countNodeChildren( node, data );
|
|
120 |
| } |
|
121 |
| |
|
122 |
0
| public Object visit(ASTContinueStatement node, Object data) {
|
|
123 |
0
| return NumericConstants.ONE;
|
|
124 |
| } |
|
125 |
| |
|
126 |
0
| public Object visit(ASTFinallyStatement node, Object data) {
|
|
127 |
0
| return countNodeChildren( node, data );
|
|
128 |
| } |
|
129 |
| |
|
130 |
0
| public Object visit(ASTReturnStatement node, Object data) {
|
|
131 |
0
| return countNodeChildren( node, data );
|
|
132 |
| } |
|
133 |
| |
|
134 |
0
| public Object visit(ASTSwitchStatement node, Object data) {
|
|
135 |
0
| return countNodeChildren( node, data );
|
|
136 |
| } |
|
137 |
| |
|
138 |
0
| public Object visit(ASTSynchronizedStatement node, Object data) {
|
|
139 |
0
| return countNodeChildren( node, data );
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| public Object visit(ASTThrowStatement node, Object data) {
|
|
143 |
0
| return NumericConstants.ONE;
|
|
144 |
| } |
|
145 |
| |
|
146 |
81
| public Object visit(ASTStatementExpression node, Object data) {
|
|
147 |
| |
|
148 |
| |
|
149 |
81
| if ( node.jjtGetParent() instanceof ASTStatementExpressionList ) {
|
|
150 |
0
| return NumericConstants.ZERO;
|
|
151 |
| } |
|
152 |
| |
|
153 |
81
| return NumericConstants.ONE;
|
|
154 |
| } |
|
155 |
| |
|
156 |
0
| public Object visit(ASTLabeledStatement node, Object data) {
|
|
157 |
0
| return countNodeChildren( node, data );
|
|
158 |
| } |
|
159 |
| |
|
160 |
0
| public Object visit(ASTLocalVariableDeclaration node, Object data) {
|
|
161 |
| |
|
162 |
| |
|
163 |
0
| if ( node.jjtGetParent() instanceof ASTForInit ) {
|
|
164 |
0
| return NumericConstants.ZERO;
|
|
165 |
| } |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
0
| return countNodeChildren( node, data );
|
|
174 |
| } |
|
175 |
| |
|
176 |
0
| public Object visit(ASTSwitchLabel node, Object data) {
|
|
177 |
0
| return countNodeChildren( node, data );
|
|
178 |
| } |
|
179 |
| |
|
180 |
| } |