Coverage Report - com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterNumberCheck
100%
11/11
100%
2/2
1.333
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.sizes;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.Check;
 23  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks the number of parameters that a method or constructor has.
 29  
  * The default allowable number of parameters is 7.
 30  
  * To change the number of allowable parameters, set property max.
 31  
  * </p>
 32  
  * <p>
 33  
  * An example of how to configure the check is:
 34  
  * </p>
 35  
  * <pre>
 36  
  * &lt;module name="ParameterNumber"/&gt;
 37  
  * </pre>
 38  
  * <p>
 39  
  * An example of how to configure the check to allow 10 parameters is:
 40  
  * </p>
 41  
  * <pre>
 42  
  * &lt;module name="ParameterNumber"&gt;
 43  
  *    &lt;property name="max" value="10"/&gt;
 44  
  * &lt;/module&gt;
 45  
  * </pre>
 46  
 
 47  
  * @author Oliver Burn
 48  
  * @version 1.0
 49  
  */
 50  5
 public class ParameterNumberCheck
 51  
     extends Check
 52  
 {
 53  
     /** default maximum number of allowed parameters */
 54  
     private static final int DEFAULT_MAX_PARAMETERS = 7;
 55  
 
 56  
     /** the maximum number of allowed parameters */
 57  5
     private int mMax = DEFAULT_MAX_PARAMETERS;
 58  
 
 59  
     /**
 60  
      * Sets the maximum number of allowed parameters.
 61  
      * @param aMax the max allowed parameters
 62  
      */
 63  
     public void setMax(int aMax)
 64  
     {
 65  1
         mMax = aMax;
 66  1
     }
 67  
 
 68  
     @Override
 69  
     public int[] getDefaultTokens()
 70  
     {
 71  5
         return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
 72  
     }
 73  
 
 74  
     @Override
 75  
     public void visitToken(DetailAST aAST)
 76  
     {
 77  33
         final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
 78  33
         final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
 79  33
         if (count > mMax) {
 80  6
             final DetailAST name = aAST.findFirstToken(TokenTypes.IDENT);
 81  6
             log(name.getLineNo(), name.getColumnNo(), "maxParam", mMax, count);
 82  
         }
 83  33
     }
 84  
 }