Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
NestedIfDepthCheck
89%
17/19
75%
6/8
2
 
 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.coding;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
 24  
 
 25  
 /**
 26  
  * Restricts nested if-else blocks to a specified depth (default = 1).
 27  
  *
 28  
  * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
 29  
  */
 30  
 public final class NestedIfDepthCheck extends AbstractNestedDepthCheck
 31  
 {
 32  
     /** default allowed nesting depth. */
 33  
     private static final int DEFAULT_MAX = 1;
 34  
 
 35  
     /** Creates new check instance with default allowed nesting depth. */
 36  
     public NestedIfDepthCheck()
 37  
     {
 38  2
         super(DEFAULT_MAX);
 39  2
     }
 40  
 
 41  
     @Override
 42  
     public int[] getDefaultTokens()
 43  
     {
 44  2
         return new int[] {TokenTypes.LITERAL_IF};
 45  
     }
 46  
 
 47  
     @Override
 48  
     public void visitToken(DetailAST aAST)
 49  
     {
 50  34
         switch (aAST.getType()) {
 51  
         case TokenTypes.LITERAL_IF:
 52  34
             visitLiteralIf(aAST);
 53  34
             break;
 54  
         default:
 55  0
             throw new IllegalStateException(aAST.toString());
 56  
         }
 57  34
     }
 58  
 
 59  
     @Override
 60  
     public void leaveToken(DetailAST aAST)
 61  
     {
 62  34
         switch (aAST.getType()) {
 63  
         case TokenTypes.LITERAL_IF:
 64  34
             leaveLiteralIf(aAST);
 65  34
             break;
 66  
         default:
 67  0
             throw new IllegalStateException(aAST.toString());
 68  
         }
 69  34
     }
 70  
 
 71  
     /**
 72  
      * Increases current nesting depth.
 73  
      * @param aIf node for if.
 74  
      */
 75  
     private void visitLiteralIf(DetailAST aIf)
 76  
     {
 77  34
         if (!CheckUtils.isElseIf(aIf)) {
 78  26
             nestIn(aIf, "nested.if.depth");
 79  
         }
 80  34
     }
 81  
 
 82  
     /**
 83  
      * Decreases current nesting depth.
 84  
      * @param aIf node for if.
 85  
      */
 86  
     private void leaveLiteralIf(DetailAST aIf)
 87  
     {
 88  34
         if (!CheckUtils.isElseIf(aIf)) {
 89  26
             nestOut();
 90  
         }
 91  34
     }
 92  
 }