KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package com.puppycrawl.tools.checkstyle.checks.sizes;
21
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.api.Check;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25
26 /**
27  * <p>
28  * Checks the number of parameters that a method or constructor has.
29  * The default allowable number of parameters is 7.
30  * To change the number of allowable parameters, set property max.
31  * </p>
32  * <p>
33  * An example of how to configure the check is:
34  * </p>
35  * <pre>
36  * &lt;module name="ParameterNumber"/&gt;
37  * </pre>
38  * <p>
39  * An example of how to configure the check to allow 10 parameters is:
40  * </p>
41  * <pre>
42  * &lt;module name="ParameterNumber"&gt;
43  * &lt;property name="max" value="10"/&gt;
44  * &lt;/module&gt;
45  * </pre>
46
47  * @author Oliver Burn
48  * @version 1.0
49  */

50 public class ParameterNumberCheck
51     extends Check
52 {
53     /** default maximum number of allowed parameters */
54     private static final int DEFAULT_MAX_PARAMETERS = 7;
55
56     /** the maximum number of allowed parameters */
57     private int mMax = DEFAULT_MAX_PARAMETERS;
58
59     /**
60      * Sets the maximum number of allowed parameters.
61      * @param aMax the max allowed parameters
62      */

63     public void setMax(int aMax)
64     {
65         mMax = aMax;
66     }
67
68     /** {@inheritDoc} */
69     public int[] getDefaultTokens()
70     {
71         return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
72     }
73
74     /** {@inheritDoc} */
75     public void visitToken(DetailAST aAST)
76     {
77         final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
78         final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
79         if (count > mMax) {
80             final DetailAST name = aAST.findFirstToken(TokenTypes.IDENT);
81             log(name.getLineNo(), name.getColumnNo(),
82                 "maxParam", new Integer JavaDoc(mMax));
83         }
84     }
85 }
86
Popular Tags