Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.NestedForDepthCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
NestedForDepthCheck
100%
15/15
100%
2/2
1.143
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 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.coding;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
 23  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * Check the number of nested {@code for} -statements. The maximum
 29  
  * number of nested layers can be configured. The default value is 1.
 30  
  * The code for the class is copied from the NestedIfDepthCheck-class.
 31  
  * The only difference is the intercepted token (for instead of if).
 32  
  * Example:
 33  
  * <pre>
 34  
  *  &lt;!-- Restricts nested for blocks to a specified depth (default = 1).
 35  
  *                                                                        --&gt;
 36  
  *  &lt;module name=&quot;com.puppycrawl.tools.checkstyle.checks.coding
 37  
  *                                            .CatchWithLostStackCheck&quot;&gt;
 38  
  *    &lt;property name=&quot;severity&quot; value=&quot;info&quot;/&gt;
 39  
  *    &lt;property name=&quot;max&quot; value=&quot;1&quot;/&gt;
 40  
  *  &lt;/module&gt;
 41  
  * </pre>
 42  
  * @author Alexander Jesse
 43  
  * @see NestedIfDepthCheck
 44  
  */
 45  
 @FileStatefulCheck
 46  11
 public final class NestedForDepthCheck extends AbstractCheck {
 47  
 
 48  
     /**
 49  
      * A key is pointing to the warning message text in "messages.properties"
 50  
      * file.
 51  
      */
 52  
     public static final String MSG_KEY = "nested.for.depth";
 53  
 
 54  
     /** Maximum allowed nesting depth. */
 55  11
     private int max = 1;
 56  
     /** Current nesting depth. */
 57  
     private int depth;
 58  
 
 59  
     /**
 60  
      * Setter for maximum allowed nesting depth.
 61  
      * @param max maximum allowed nesting depth.
 62  
      */
 63  
     public void setMax(int max) {
 64  3
         this.max = max;
 65  3
     }
 66  
 
 67  
     @Override
 68  
     public int[] getDefaultTokens() {
 69  15
         return getRequiredTokens();
 70  
     }
 71  
 
 72  
     @Override
 73  
     public int[] getAcceptableTokens() {
 74  5
         return getRequiredTokens();
 75  
     }
 76  
 
 77  
     @Override
 78  
     public int[] getRequiredTokens() {
 79  35
         return new int[] {TokenTypes.LITERAL_FOR};
 80  
     }
 81  
 
 82  
     @Override
 83  
     public void beginTree(DetailAST rootAST) {
 84  3
         depth = 0;
 85  3
     }
 86  
 
 87  
     @Override
 88  
     public void visitToken(DetailAST ast) {
 89  12
         if (depth > max) {
 90  3
             log(ast, MSG_KEY, depth, max);
 91  
         }
 92  12
         ++depth;
 93  12
     }
 94  
 
 95  
     @Override
 96  
     public void leaveToken(DetailAST ast) {
 97  12
         --depth;
 98  12
     }
 99  
 }