KickJava   Java API By Example, From Geeks To Geeks.

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


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.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26  * <p>
27  * Checks for long anonymous inner classes.
28  * </p>
29  * <p>
30  * Rationale: If an anonymous inner class becomes very long
31  * it is hard to understand and to see the flow of the method
32  * where the class is defined. Therefore long anonymous inner
33  * classes should usually be refactored into a named inner class.
34  * See also Bloch, Effective Java, p. 93.
35  * </p>
36  * <p>
37  * The default maximum anonymous inner class length is 20 lines.
38  * To change the maximum number of lines, set property max.
39  * </p>
40  * <p>
41  * An example of how to configure the check is:
42  * </p>
43  * <pre>
44  * &lt;module name="AnonInnerLength"/&gt;
45  * </pre>
46  * <p>
47  * An example of how to configure the check so that it accepts anonymous
48  * inner classes with up to 60 lines is:
49  * </p>
50  * <pre>
51  * &lt;module name="AnonInnerLength"&gt;
52  * &lt;property name="max" value="60"/&gt;
53  * &lt;/module&gt;
54  * </pre>
55  *
56  * @author Rob Worth
57  */

58 public class AnonInnerLengthCheck extends Check
59 {
60     /** default maximum number of lines */
61     private static final int DEFAULT_MAX = 20;
62
63     /** maximum number of lines */
64     private int mMax = DEFAULT_MAX;
65
66     /** {@inheritDoc} */
67     public int[] getDefaultTokens()
68     {
69         return new int[] {TokenTypes.LITERAL_NEW};
70     }
71
72     /** {@inheritDoc} */
73     public void visitToken(DetailAST aAST)
74     {
75         final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.OBJBLOCK);
76         if (openingBrace != null) {
77             final DetailAST closingBrace =
78                 openingBrace.findFirstToken(TokenTypes.RCURLY);
79             final int length =
80                 closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
81             if (length > mMax) {
82                 log(aAST.getLineNo(),
83                     aAST.getColumnNo(),
84                     "maxLen.anonInner",
85                     new Integer JavaDoc(length),
86                     new Integer JavaDoc(mMax));
87             }
88         }
89     }
90
91
92     /**
93      * @param aLength the maximum length of an anonymous inner class.
94      */

95     public void setMax(int aLength)
96     {
97         mMax = aLength;
98     }
99 }
100
Popular Tags