KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.FileContents;
24 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25
26 /**
27  * <p>
28  * Checks for long methods.
29  * </p>
30  * <p>
31  * Rationale: If a method becomes very long it is hard to understand.
32  * Therefore long methods should usually be refactored into several
33  * individual methods that focus on a specific task.
34  * </p>
35  *<p>
36  * The default maximum method length is 150 lines. To change the maximum
37  * number of lines, set property max.
38  * </p>
39  * <p>
40  * An example of how to configure the check is:
41  * </p>
42  * <pre>
43  * &lt;module name="MethodLength"/&gt;
44  * </pre>
45  * <p>
46  * An example of how to configure the check so that it accepts methods with at
47  * most 60 lines is:
48  * </p>
49  * <pre>
50  * &lt;module name="MethodLength"&gt;
51  * &lt;property name="max" value="60"/&gt;
52  * &lt;/module&gt;
53  * </pre>
54  * @author Lars Kühne
55  */

56 public class MethodLengthCheck extends Check
57 {
58     /** whether to ignore empty lines and single line comments */
59     private boolean mCountEmpty = true;
60
61     /** default maximum number of lines */
62     private static final int DEFAULT_MAX_LINES = 150;
63
64     /** the maximum number of lines */
65     private int mMax = DEFAULT_MAX_LINES;
66
67     /** {@inheritDoc} */
68     public int[] getDefaultTokens()
69     {
70         return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
71     }
72
73     /** {@inheritDoc} */
74     public void visitToken(DetailAST aAST)
75     {
76         final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
77         if (openingBrace != null) {
78             final DetailAST closingBrace =
79                 openingBrace.findFirstToken(TokenTypes.RCURLY);
80             int length =
81                 closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
82
83             if (!mCountEmpty) {
84                 final FileContents contents = getFileContents();
85                 final int lastLine = closingBrace.getLineNo();
86                 for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
87                     if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
88                         length--;
89                     }
90                 }
91             }
92             if (length > mMax) {
93                 log(aAST.getLineNo(),
94                     aAST.getColumnNo(),
95                     "maxLen.method",
96                     new Integer JavaDoc(length),
97                     new Integer JavaDoc(mMax));
98             }
99         }
100     }
101
102     /**
103      * @param aLength the maximum length of a method.
104      */

105     public void setMax(int aLength)
106     {
107         mMax = aLength;
108     }
109
110     /**
111      * @param aCountEmpty whether to count empty and single line comments
112      * of the form //.
113      */

114     public void setCountEmpty(boolean aCountEmpty)
115     {
116         mCountEmpty = aCountEmpty;
117     }
118 }
119
Popular Tags