KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > sizes > ExecutableStatementCountCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks.sizes;
20
21 import java.util.Stack JavaDoc;
22
23 import com.puppycrawl.tools.checkstyle.api.Check;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28  * Restricts the number of executable statements to a specified limit
29  * (default = 30).
30  * @author Simon Harris
31  */

32 public final class ExecutableStatementCountCheck
33     extends Check
34 {
35     /** default threshold */
36     private static final int DEFAULT_MAX = 30;
37
38     /** threshold to report error for */
39     private int mMax;
40
41     /** Stack of method contexts. */
42     private final Stack JavaDoc mContextStack = new Stack JavaDoc();
43
44     /** Current method context. */
45     private Context mContext;
46
47     /** Constructs a <code>ExecutableStatementCountCheck</code>. */
48     public ExecutableStatementCountCheck()
49     {
50         setMax(DEFAULT_MAX);
51     }
52
53     /** {@inheritDoc} */
54     public int[] getDefaultTokens()
55     {
56         return new int[] {
57             TokenTypes.CTOR_DEF,
58             TokenTypes.METHOD_DEF,
59             TokenTypes.INSTANCE_INIT,
60             TokenTypes.STATIC_INIT,
61             TokenTypes.SLIST,
62         };
63     }
64
65     /** {@inheritDoc} */
66     public int[] getRequiredTokens()
67     {
68         return new int[] {TokenTypes.SLIST};
69     }
70
71     /**
72      * Gets the maximum threshold.
73      * @return the maximum threshold.
74      */

75     public int getMax()
76     {
77         return mMax;
78     }
79
80     /**
81      * Sets the maximum threshold.
82      * @param aMax the maximum threshold.
83      */

84     public void setMax(int aMax)
85     {
86         mMax = aMax;
87     }
88
89     /** {@inheritDoc} */
90     public void beginTree(DetailAST aRootAST)
91     {
92         mContext = null;
93         mContextStack.clear();
94     }
95
96     /** {@inheritDoc} */
97     public void visitToken(DetailAST aAST)
98     {
99         switch (aAST.getType()) {
100         case TokenTypes.CTOR_DEF:
101         case TokenTypes.METHOD_DEF:
102         case TokenTypes.INSTANCE_INIT:
103         case TokenTypes.STATIC_INIT:
104             visitMemberDef(aAST);
105             break;
106         case TokenTypes.SLIST:
107             visitSlist(aAST);
108             break;
109         default:
110             throw new IllegalStateException JavaDoc(aAST.toString());
111         }
112     }
113
114     /** {@inheritDoc} */
115     public void leaveToken(DetailAST aAST)
116     {
117         switch (aAST.getType()) {
118         case TokenTypes.CTOR_DEF:
119         case TokenTypes.METHOD_DEF:
120         case TokenTypes.INSTANCE_INIT:
121         case TokenTypes.STATIC_INIT:
122             leaveMemberDef(aAST);
123             break;
124         case TokenTypes.SLIST:
125             // Do nothing
126
break;
127         default:
128             throw new IllegalStateException JavaDoc(aAST.toString());
129         }
130     }
131
132     /**
133      * Process the start of the member definition.
134      * @param aAST the token representing the member definition.
135      */

136     private void visitMemberDef(DetailAST aAST)
137     {
138         mContextStack.push(mContext);
139         mContext = new Context(aAST);
140     }
141
142     /**
143      * Process the end of a member definition.
144      *
145      * @param aAST the token representing the member definition.
146      */

147     private void leaveMemberDef(DetailAST aAST)
148     {
149         final int count = mContext.getCount();
150         if (count > getMax()) {
151             log(
152                 aAST.getLineNo(),
153                 aAST.getColumnNo(),
154                 "executableStatementCount",
155                 new Integer JavaDoc(count),
156                 new Integer JavaDoc(getMax()));
157         }
158         mContext = (Context) mContextStack.pop();
159     }
160
161     /**
162      * Process the end of a statement list.
163      *
164      * @param aAST the token representing the statement list.
165      */

166     private void visitSlist(DetailAST aAST)
167     {
168         if (mContext != null) {
169             // find member AST for the statement list
170
final DetailAST contextAST = mContext.getAST();
171             DetailAST parent = aAST.getParent();
172             while (parent != null) {
173                 final int type = parent.getType();
174                 if ((type == TokenTypes.CTOR_DEF)
175                     || (type == TokenTypes.METHOD_DEF)
176                     || (type == TokenTypes.INSTANCE_INIT)
177                     || (type == TokenTypes.STATIC_INIT))
178                 {
179                     if (parent == contextAST) {
180                         mContext.addCount(aAST.getChildCount() / 2);
181                     }
182                     break;
183                 }
184                 parent = parent.getParent();
185             }
186         }
187     }
188
189     /**
190      * Class to encapsulate counting information about one member.
191      * @author Simon Harris
192      */

193     private class Context
194     {
195         /** Member AST node. */
196         private DetailAST mAST;
197
198         /** Counter for context elements. */
199         private int mCount;
200
201         /**
202          * Creates new member context.
203          * @param aAST member AST node.
204          */

205         public Context(DetailAST aAST)
206         {
207             mAST = aAST;
208             mCount = 0;
209         }
210
211         /**
212          * Increase count.
213          * @param aCount the count increment.
214          */

215         public void addCount(int aCount)
216         {
217             mCount += aCount;
218         }
219
220         /**
221          * Gets the member AST node.
222          * @return the member AST node.
223          */

224         public DetailAST getAST()
225         {
226             return mAST;
227         }
228
229         /**
230          * Gets the count.
231          * @return the count.
232          */

233         public int getCount()
234         {
235             return mCount;
236         }
237     }
238 }
239
Popular Tags