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