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