001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 the original author or authors.
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.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
028
029/**
030 * <p>
031 * Checks the number of parameters of a method or constructor.
032 * </p>
033 * <ul>
034 * <li>
035 * Property {@code max} - Specify the maximum number of parameters allowed.
036 * Type is {@code int}.
037 * Default value is {@code 7}.
038 * </li>
039 * <li>
040 * Property {@code ignoreOverriddenMethods} - Ignore number of parameters for
041 * methods with {@code @Override} annotation.
042 * Type is {@code boolean}.
043 * Default value is {@code false}.
044 * </li>
045 * <li>
046 * Property {@code tokens} - tokens to check
047 * Type is {@code java.lang.String[]}.
048 * Validation type is {@code tokenSet}.
049 * Default value is:
050 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
051 * METHOD_DEF</a>,
052 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
053 * CTOR_DEF</a>.
054 * </li>
055 * </ul>
056 * <p>
057 * To configure the check:
058 * </p>
059 * <pre>
060 * &lt;module name="ParameterNumber"/&gt;
061 * </pre>
062 * <p>
063 * To configure the check to allow 10 parameters for a method:
064 * </p>
065 * <pre>
066 * &lt;module name="ParameterNumber"&gt;
067 *   &lt;property name="max" value="10"/&gt;
068 *   &lt;property name="tokens" value="METHOD_DEF"/&gt;
069 * &lt;/module&gt;
070 * </pre>
071 * <p>
072 * To configure the check to ignore number of parameters for methods with
073 * {@code @Override} or {@code @java.lang.Override annotation}.
074 * </p>
075 * <p>
076 * Rationale: developer may need to override method with many parameters from
077 * 3-rd party library. In this case developer has no control over number of parameters.
078 * </p>
079 * <pre>
080 * &lt;module name="ParameterNumber"&gt;
081 *   &lt;property name="ignoreOverriddenMethods" value="true"/&gt;
082 * &lt;/module&gt;
083 * </pre>
084 * <p>
085 * Java code example:
086 * </p>
087 * <pre>
088 * &#064;Override
089 * public void needsLotsOfParameters(int a,
090 *     int b, int c, int d, int e, int f, int g, int h) {
091 *     ...
092 * }
093 * </pre>
094 * <p>
095 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
096 * </p>
097 * <p>
098 * Violation Message Keys:
099 * </p>
100 * <ul>
101 * <li>
102 * {@code maxParam}
103 * </li>
104 * </ul>
105 *
106 * @since 3.0
107 */
108@StatelessCheck
109public class ParameterNumberCheck
110    extends AbstractCheck {
111
112    /**
113     * A key is pointing to the warning message text in "messages.properties"
114     * file.
115     */
116    public static final String MSG_KEY = "maxParam";
117
118    /** {@link Override Override} annotation name. */
119    private static final String OVERRIDE = "Override";
120
121    /** Canonical {@link Override Override} annotation name. */
122    private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE;
123
124    /** Default maximum number of allowed parameters. */
125    private static final int DEFAULT_MAX_PARAMETERS = 7;
126
127    /** Specify the maximum number of parameters allowed. */
128    private int max = DEFAULT_MAX_PARAMETERS;
129
130    /** Ignore number of parameters for methods with {@code @Override} annotation. */
131    private boolean ignoreOverriddenMethods;
132
133    /**
134     * Setter to specify the maximum number of parameters allowed.
135     *
136     * @param max the max allowed parameters
137     */
138    public void setMax(int max) {
139        this.max = max;
140    }
141
142    /**
143     * Setter to ignore number of parameters for methods with {@code @Override} annotation.
144     *
145     * @param ignoreOverriddenMethods set ignore overridden methods
146     */
147    public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
148        this.ignoreOverriddenMethods = ignoreOverriddenMethods;
149    }
150
151    @Override
152    public int[] getDefaultTokens() {
153        return getAcceptableTokens();
154    }
155
156    @Override
157    public int[] getAcceptableTokens() {
158        return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
159    }
160
161    @Override
162    public int[] getRequiredTokens() {
163        return CommonUtil.EMPTY_INT_ARRAY;
164    }
165
166    @Override
167    public void visitToken(DetailAST ast) {
168        final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
169        final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
170        if (count > max && !shouldIgnoreNumberOfParameters(ast)) {
171            final DetailAST name = ast.findFirstToken(TokenTypes.IDENT);
172            log(name, MSG_KEY, max, count);
173        }
174    }
175
176    /**
177     * Determine whether to ignore number of parameters for the method.
178     *
179     * @param ast the token to process
180     * @return true if this is overridden method and number of parameters should be ignored
181     *         false otherwise
182     */
183    private boolean shouldIgnoreNumberOfParameters(DetailAST ast) {
184        // if you override a method, you have no power over the number of parameters
185        return ignoreOverriddenMethods
186                && (AnnotationUtil.containsAnnotation(ast, OVERRIDE)
187                || AnnotationUtil.containsAnnotation(ast, CANONICAL_OVERRIDE));
188    }
189
190}