001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 ignoreOverriddenMethods} - Ignore number of parameters for
036 * methods with {@code @Override} annotation.
037 * Type is {@code boolean}.
038 * Default value is {@code false}.
039 * </li>
040 * <li>
041 * Property {@code max} - Specify the maximum number of parameters allowed.
042 * Type is {@code int}.
043 * Default value is {@code 7}.
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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
058 * </p>
059 * <p>
060 * Violation Message Keys:
061 * </p>
062 * <ul>
063 * <li>
064 * {@code maxParam}
065 * </li>
066 * </ul>
067 *
068 * @since 3.0
069 */
070@StatelessCheck
071public class ParameterNumberCheck
072    extends AbstractCheck {
073
074    /**
075     * A key is pointing to the warning message text in "messages.properties"
076     * file.
077     */
078    public static final String MSG_KEY = "maxParam";
079
080    /** Default maximum number of allowed parameters. */
081    private static final int DEFAULT_MAX_PARAMETERS = 7;
082
083    /** Specify the maximum number of parameters allowed. */
084    private int max = DEFAULT_MAX_PARAMETERS;
085
086    /** Ignore number of parameters for methods with {@code @Override} annotation. */
087    private boolean ignoreOverriddenMethods;
088
089    /**
090     * Setter to specify the maximum number of parameters allowed.
091     *
092     * @param max the max allowed parameters
093     * @since 3.0
094     */
095    public void setMax(int max) {
096        this.max = max;
097    }
098
099    /**
100     * Setter to ignore number of parameters for methods with {@code @Override} annotation.
101     *
102     * @param ignoreOverriddenMethods set ignore overridden methods
103     * @since 6.2
104     */
105    public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
106        this.ignoreOverriddenMethods = ignoreOverriddenMethods;
107    }
108
109    @Override
110    public int[] getDefaultTokens() {
111        return getAcceptableTokens();
112    }
113
114    @Override
115    public int[] getAcceptableTokens() {
116        return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
117    }
118
119    @Override
120    public int[] getRequiredTokens() {
121        return CommonUtil.EMPTY_INT_ARRAY;
122    }
123
124    @Override
125    public void visitToken(DetailAST ast) {
126        final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
127        final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
128        if (count > max && !shouldIgnoreNumberOfParameters(ast)) {
129            final DetailAST name = ast.findFirstToken(TokenTypes.IDENT);
130            log(name, MSG_KEY, max, count);
131        }
132    }
133
134    /**
135     * Determine whether to ignore number of parameters for the method.
136     *
137     * @param ast the token to process
138     * @return true if this is overridden method and number of parameters should be ignored
139     *         false otherwise
140     */
141    private boolean shouldIgnoreNumberOfParameters(DetailAST ast) {
142        // if you override a method, you have no power over the number of parameters
143        return ignoreOverriddenMethods
144                && AnnotationUtil.hasOverrideAnnotation(ast);
145    }
146
147}