001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.sizes;
021
022import com.puppycrawl.tools.checkstyle.api.Check;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Checks the number of parameters that a method or constructor has.
029 * The default allowable number of parameters is 7.
030 * To change the number of allowable parameters, set property max.
031 * </p>
032 * <p>
033 * An example of how to configure the check is:
034 * </p>
035 * <pre>
036 * &lt;module name="ParameterNumber"/&gt;
037 * </pre>
038 * <p>
039 * An example of how to configure the check to allow 10 parameters is:
040 * </p>
041 * <pre>
042 * &lt;module name="ParameterNumber"&gt;
043 *    &lt;property name="max" value="10"/&gt;
044 * &lt;/module&gt;
045 * </pre>
046
047 * @author Oliver Burn
048 * @version 1.0
049 */
050public class ParameterNumberCheck
051    extends Check
052{
053    /** default maximum number of allowed parameters */
054    private static final int DEFAULT_MAX_PARAMETERS = 7;
055
056    /** the maximum number of allowed parameters */
057    private int mMax = DEFAULT_MAX_PARAMETERS;
058
059    /**
060     * Sets the maximum number of allowed parameters.
061     * @param aMax the max allowed parameters
062     */
063    public void setMax(int aMax)
064    {
065        mMax = aMax;
066    }
067
068    @Override
069    public int[] getDefaultTokens()
070    {
071        return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
072    }
073
074    @Override
075    public void visitToken(DetailAST aAST)
076    {
077        final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
078        final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
079        if (count > mMax) {
080            final DetailAST name = aAST.findFirstToken(TokenTypes.IDENT);
081            log(name.getLineNo(), name.getColumnNo(), "maxParam", mMax, count);
082        }
083    }
084}