001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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 * <module name="ParameterNumber"/> 061 * </pre> 062 * <p> 063 * To configure the check to allow 10 parameters for a method: 064 * </p> 065 * <pre> 066 * <module name="ParameterNumber"> 067 * <property name="max" value="10"/> 068 * <property name="tokens" value="METHOD_DEF"/> 069 * </module> 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 * <module name="ParameterNumber"> 081 * <property name="ignoreOverriddenMethods" value="true"/> 082 * </module> 083 * </pre> 084 * <p> 085 * Java code example: 086 * </p> 087 * <pre> 088 * @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 /** Default maximum number of allowed parameters. */ 119 private static final int DEFAULT_MAX_PARAMETERS = 7; 120 121 /** Specify the maximum number of parameters allowed. */ 122 private int max = DEFAULT_MAX_PARAMETERS; 123 124 /** Ignore number of parameters for methods with {@code @Override} annotation. */ 125 private boolean ignoreOverriddenMethods; 126 127 /** 128 * Setter to specify the maximum number of parameters allowed. 129 * 130 * @param max the max allowed parameters 131 */ 132 public void setMax(int max) { 133 this.max = max; 134 } 135 136 /** 137 * Setter to ignore number of parameters for methods with {@code @Override} annotation. 138 * 139 * @param ignoreOverriddenMethods set ignore overridden methods 140 */ 141 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 142 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 143 } 144 145 @Override 146 public int[] getDefaultTokens() { 147 return getAcceptableTokens(); 148 } 149 150 @Override 151 public int[] getAcceptableTokens() { 152 return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF}; 153 } 154 155 @Override 156 public int[] getRequiredTokens() { 157 return CommonUtil.EMPTY_INT_ARRAY; 158 } 159 160 @Override 161 public void visitToken(DetailAST ast) { 162 final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); 163 final int count = params.getChildCount(TokenTypes.PARAMETER_DEF); 164 if (count > max && !shouldIgnoreNumberOfParameters(ast)) { 165 final DetailAST name = ast.findFirstToken(TokenTypes.IDENT); 166 log(name, MSG_KEY, max, count); 167 } 168 } 169 170 /** 171 * Determine whether to ignore number of parameters for the method. 172 * 173 * @param ast the token to process 174 * @return true if this is overridden method and number of parameters should be ignored 175 * false otherwise 176 */ 177 private boolean shouldIgnoreNumberOfParameters(DetailAST ast) { 178 // if you override a method, you have no power over the number of parameters 179 return ignoreOverriddenMethods 180 && AnnotationUtil.hasOverrideAnnotation(ast); 181 } 182 183}