001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.AnnotationUtility; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 027 028/** 029 * <p> 030 * Checks the number of parameters that a method or constructor has. 031 * The default allowable number of parameters is 7. 032 * To change the number of allowable parameters, set property max. 033 * Allows to ignore number of parameters for methods with 034 * @{@link Override} annotation. 035 * </p> 036 * <p> 037 * An example of how to configure the check is: 038 * </p> 039 * <pre> 040 * <module name="ParameterNumber"/> 041 * </pre> 042 * <p> 043 * An example of how to configure the check to allow 10 parameters 044 * and ignoring parameters for methods with @{@link Override} 045 * annotation is: 046 * </p> 047 * <pre> 048 * <module name="ParameterNumber"> 049 * <property name="max" value="10"/> 050 * <property name="ignoreOverriddenMethods" value="true"/> 051 * </module> 052 * </pre> 053 * Java code that will be ignored: 054 * <pre> 055 * {@code 056 * 057 * @Override 058 * public void needsLotsOfParameters(int a, 059 * int b, int c, int d, int e, int f, int g, int h) { 060 * ... 061 * } 062 * } 063 * </pre> 064 * @author Oliver Burn 065 */ 066public class ParameterNumberCheck 067 extends AbstractCheck { 068 069 /** 070 * A key is pointing to the warning message text in "messages.properties" 071 * file. 072 */ 073 public static final String MSG_KEY = "maxParam"; 074 075 /** {@link Override Override} annotation name. */ 076 private static final String OVERRIDE = "Override"; 077 078 /** Canonical {@link Override Override} annotation name. */ 079 private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE; 080 081 /** Default maximum number of allowed parameters. */ 082 private static final int DEFAULT_MAX_PARAMETERS = 7; 083 084 /** The maximum number of allowed parameters. */ 085 private int max = DEFAULT_MAX_PARAMETERS; 086 087 /** Ignore overridden methods. */ 088 private boolean ignoreOverriddenMethods; 089 090 /** 091 * Sets the maximum number of allowed parameters. 092 * @param max the max allowed parameters 093 */ 094 public void setMax(int max) { 095 this.max = max; 096 } 097 098 /** 099 * Ignore number of parameters for methods with 100 * @{@link Override} annotation. 101 * @param ignoreOverriddenMethods set ignore overridden methods 102 */ 103 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 104 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 105 } 106 107 @Override 108 public int[] getDefaultTokens() { 109 return getAcceptableTokens(); 110 } 111 112 @Override 113 public int[] getAcceptableTokens() { 114 return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF}; 115 } 116 117 @Override 118 public int[] getRequiredTokens() { 119 return CommonUtils.EMPTY_INT_ARRAY; 120 } 121 122 @Override 123 public void visitToken(DetailAST ast) { 124 final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); 125 final int count = params.getChildCount(TokenTypes.PARAMETER_DEF); 126 if (count > max && !shouldIgnoreNumberOfParameters(ast)) { 127 final DetailAST name = ast.findFirstToken(TokenTypes.IDENT); 128 log(name.getLineNo(), name.getColumnNo(), MSG_KEY, max, count); 129 } 130 } 131 132 /** Determine whether to ignore number of parameters for the method. 133 * 134 * @param ast the token to process 135 * @return true if this is overridden method and number of parameters should be ignored 136 * false otherwise 137 */ 138 private boolean shouldIgnoreNumberOfParameters(DetailAST ast) { 139 //if you override a method, you have no power over the number of parameters 140 return ignoreOverriddenMethods 141 && (AnnotationUtility.containsAnnotation(ast, OVERRIDE) 142 || AnnotationUtility.containsAnnotation(ast, CANONICAL_OVERRIDE)); 143 } 144}