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