001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 java.util.Set; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 029import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 030 031/** 032 * <div> 033 * Checks the number of parameters of a method or constructor. 034 * </div> 035 * 036 * @since 3.0 037 */ 038@StatelessCheck 039public class ParameterNumberCheck 040 extends AbstractCheck { 041 042 /** 043 * A key is pointing to the warning message text in "messages.properties" 044 * file. 045 */ 046 public static final String MSG_KEY = "maxParam"; 047 048 /** Default maximum number of allowed parameters. */ 049 private static final int DEFAULT_MAX_PARAMETERS = 7; 050 051 /** Specify the maximum number of parameters allowed. */ 052 private int max = DEFAULT_MAX_PARAMETERS; 053 054 /** Ignore number of parameters for methods with {@code @Override} annotation. */ 055 private boolean ignoreOverriddenMethods; 056 057 /** 058 * Ignore methods and constructors annotated with the specified annotation(s). 059 */ 060 private Set<String> ignoreAnnotatedBy = Set.of(); 061 062 /** 063 * Creates a new {@code ParameterNumberCheck} instance. 064 */ 065 public ParameterNumberCheck() { 066 // no code by default 067 } 068 069 /** 070 * Setter to specify the maximum number of parameters allowed. 071 * 072 * @param max the max allowed parameters 073 * @since 3.0 074 */ 075 public void setMax(int max) { 076 this.max = max; 077 } 078 079 /** 080 * Setter to ignore number of parameters for methods with {@code @Override} annotation. 081 * 082 * @param ignoreOverriddenMethods set ignore overridden methods 083 * @since 6.2 084 */ 085 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 086 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 087 } 088 089 /** 090 * Setter to ignore methods and constructors annotated with the specified annotation(s). 091 * 092 * @param annotationNames specified annotation(s) 093 * @since 10.15.0 094 */ 095 public void setIgnoreAnnotatedBy(String... annotationNames) { 096 ignoreAnnotatedBy = Set.of(annotationNames); 097 } 098 099 @Override 100 public int[] getDefaultTokens() { 101 return getAcceptableTokens(); 102 } 103 104 @Override 105 public int[] getAcceptableTokens() { 106 return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF}; 107 } 108 109 @Override 110 public int[] getRequiredTokens() { 111 return CommonUtil.EMPTY_INT_ARRAY; 112 } 113 114 @Override 115 public void visitToken(DetailAST ast) { 116 final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); 117 final int count = params.getChildCount(TokenTypes.PARAMETER_DEF); 118 if (count > max && !shouldIgnoreNumberOfParameters(ast)) { 119 final DetailAST name = ast.findFirstToken(TokenTypes.IDENT); 120 log(name, MSG_KEY, max, count); 121 } 122 } 123 124 /** 125 * Determine whether to ignore number of parameters. 126 * 127 * @param ast the token to process 128 * @return true if number of parameters should be ignored. 129 */ 130 private boolean shouldIgnoreNumberOfParameters(DetailAST ast) { 131 return isIgnoredOverriddenMethod(ast) || isAnnotatedByIgnoredAnnotations(ast); 132 } 133 134 /** 135 * Checks if method is overridden and should be ignored. 136 * 137 * @param ast method definition to check 138 * @return true if method is overridden and should be ignored. 139 */ 140 private boolean isIgnoredOverriddenMethod(DetailAST ast) { 141 return ignoreOverriddenMethods && AnnotationUtil.hasOverrideAnnotation(ast); 142 } 143 144 /** 145 * Checks if method or constructor is annotated by ignored annotation(s). 146 * 147 * @param ast method or constructor definition to check 148 * @return true if annotated by ignored annotation(s). 149 */ 150 private boolean isAnnotatedByIgnoredAnnotations(DetailAST ast) { 151 return AnnotationUtil.containsAnnotation(ast, ignoreAnnotatedBy); 152 } 153 154}