Coverage Report - com.puppycrawl.tools.checkstyle.checks.sizes.MethodLengthCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodLengthCheck
100%
21/21
100%
12/12
2.5
 
 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  
 package com.puppycrawl.tools.checkstyle.checks.sizes;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.Check;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.FileContents;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks for long methods.
 29  
  * </p>
 30  
  * <p>
 31  
  * Rationale: If a method becomes very long it is hard to understand.
 32  
  * Therefore long methods should usually be refactored into several
 33  
  * individual methods that focus on a specific task.
 34  
  * </p>
 35  
  *<p>
 36  
  * The default maximum method length is 150 lines. To change the maximum
 37  
  * number of lines, set property max.
 38  
  * </p>
 39  
  * <p>
 40  
  * An example of how to configure the check is:
 41  
  * </p>
 42  
  * <pre>
 43  
  * &lt;module name="MethodLength"/&gt;
 44  
  * </pre>
 45  
  * <p>
 46  
  * An example of how to configure the check so that it accepts methods with at
 47  
  * most 60 lines is:
 48  
  * </p>
 49  
  * <pre>
 50  
  * &lt;module name="MethodLength"&gt;
 51  
  *    &lt;property name="max" value="60"/&gt;
 52  
  * &lt;/module&gt;
 53  
  * </pre>
 54  
  * @author Lars Kühne
 55  
  */
 56  3
 public class MethodLengthCheck extends Check
 57  
 {
 58  
     /** whether to ignore empty lines and single line comments */
 59  3
     private boolean mCountEmpty = true;
 60  
 
 61  
     /** default maximum number of lines */
 62  
     private static final int DEFAULT_MAX_LINES = 150;
 63  
 
 64  
     /** the maximum number of lines */
 65  3
     private int mMax = DEFAULT_MAX_LINES;
 66  
 
 67  
     @Override
 68  
     public int[] getDefaultTokens()
 69  
     {
 70  3
         return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
 71  
     }
 72  
 
 73  
     @Override
 74  
     public void visitToken(DetailAST aAST)
 75  
     {
 76  32
         final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
 77  32
         if (openingBrace != null) {
 78  28
             final DetailAST closingBrace =
 79  
                 openingBrace.findFirstToken(TokenTypes.RCURLY);
 80  28
             int length =
 81  
                 closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
 82  
 
 83  28
             if (!mCountEmpty) {
 84  9
                 final FileContents contents = getFileContents();
 85  9
                 final int lastLine = closingBrace.getLineNo();
 86  101
                 for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
 87  92
                     if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
 88  35
                         length--;
 89  
                     }
 90  
                 }
 91  
             }
 92  28
             if (length > mMax) {
 93  1
                 log(aAST.getLineNo(), aAST.getColumnNo(), "maxLen.method",
 94  
                         length, mMax);
 95  
             }
 96  
         }
 97  32
     }
 98  
 
 99  
     /**
 100  
      * @param aLength the maximum length of a method.
 101  
      */
 102  
     public void setMax(int aLength)
 103  
     {
 104  2
         mMax = aLength;
 105  2
     }
 106  
 
 107  
     /**
 108  
      * @param aCountEmpty whether to count empty and single line comments
 109  
      * of the form //.
 110  
      */
 111  
     public void setCountEmpty(boolean aCountEmpty)
 112  
     {
 113  1
         mCountEmpty = aCountEmpty;
 114  1
     }
 115  
 }