Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
NestedIfDepthCheck
100%
17/17
100%
6/6
1.429
 
 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  
 import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
 27  
 
 28  
 /**
 29  
  * Restricts nested if-else blocks to a specified depth (default = 1).
 30  
  *
 31  
  * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
 32  
  */
 33  
 @FileStatefulCheck
 34  11
 public final class NestedIfDepthCheck extends AbstractCheck {
 35  
 
 36  
     /**
 37  
      * A key is pointing to the warning message text in "messages.properties"
 38  
      * file.
 39  
      */
 40  
     public static final String MSG_KEY = "nested.if.depth";
 41  
 
 42  
     /** Maximum allowed nesting depth. */
 43  11
     private int max = 1;
 44  
     /** Current nesting depth. */
 45  
     private int depth;
 46  
 
 47  
     /**
 48  
      * Setter for maximum allowed nesting depth.
 49  
      * @param max maximum allowed nesting depth.
 50  
      */
 51  
     public void setMax(int max) {
 52  2
         this.max = max;
 53  2
     }
 54  
 
 55  
     @Override
 56  
     public int[] getDefaultTokens() {
 57  15
         return getRequiredTokens();
 58  
     }
 59  
 
 60  
     @Override
 61  
     public int[] getAcceptableTokens() {
 62  5
         return getRequiredTokens();
 63  
     }
 64  
 
 65  
     @Override
 66  
     public int[] getRequiredTokens() {
 67  35
         return new int[] {TokenTypes.LITERAL_IF};
 68  
     }
 69  
 
 70  
     @Override
 71  
     public void beginTree(DetailAST rootAST) {
 72  3
         depth = 0;
 73  3
     }
 74  
 
 75  
     @Override
 76  
     public void visitToken(DetailAST literalIf) {
 77  34
         if (!CheckUtils.isElseIf(literalIf)) {
 78  26
             if (depth > max) {
 79  2
                 log(literalIf, MSG_KEY, depth, max);
 80  
             }
 81  26
             ++depth;
 82  
         }
 83  34
     }
 84  
 
 85  
     @Override
 86  
     public void leaveToken(DetailAST literalIf) {
 87  34
         if (!CheckUtils.isElseIf(literalIf)) {
 88  26
             --depth;
 89  
         }
 90  34
     }
 91  
 
 92  
 }