Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.SlistHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SlistHandler
93%
15/16
100%
32/32
2.889
 
 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.indentation;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 
 24  
 /**
 25  
  * Handler for a list of statements.
 26  
  *
 27  
  * @author jrichard
 28  
  */
 29  
 public class SlistHandler extends BlockParentHandler
 30  
 {
 31  
     /**
 32  
      * Construct an instance of this handler with the given indentation check,
 33  
      * abstract syntax tree, and parent handler.
 34  
      *
 35  
      * @param aIndentCheck   the indentation check
 36  
      * @param aAst           the abstract syntax tree
 37  
      * @param aParent        the parent handler
 38  
      */
 39  
     public SlistHandler(IndentationCheck aIndentCheck,
 40  
         DetailAST aAst, ExpressionHandler aParent)
 41  
     {
 42  426
         super(aIndentCheck, "block", aAst, aParent);
 43  426
     }
 44  
 
 45  
     @Override
 46  
     public IndentLevel suggestedChildLevel(ExpressionHandler aChild)
 47  
     {
 48  
         // this is:
 49  
         //  switch (var) {
 50  
         //     case 3: {
 51  
         //        break;
 52  
         //     }
 53  
         //  }
 54  
         //  ... the case SLIST is followed by a user-created SLIST and
 55  
         //  preceded by a switch
 56  
 
 57  
         // if our parent is a block handler we want to be transparent
 58  539
         if (((getParent() instanceof BlockParentHandler)
 59  
                 && !(getParent() instanceof SlistHandler))
 60  
             || ((getParent() instanceof CaseHandler)
 61  
                 && (aChild instanceof SlistHandler)))
 62  
         {
 63  489
             return getParent().suggestedChildLevel(aChild);
 64  
         }
 65  50
         return super.suggestedChildLevel(aChild);
 66  
     }
 67  
 
 68  
     @Override
 69  
     protected DetailAST getNonlistChild()
 70  
     {
 71  
         // blocks always have either block children or they are transparent
 72  
         // and aren't checking children at all.  In the later case, the
 73  
         // superclass will want to check single children, so when it
 74  
         // does tell it we have none.
 75  0
         return null;
 76  
     }
 77  
 
 78  
     @Override
 79  
     protected DetailAST getListChild()
 80  
     {
 81  72
         return getMainAst();
 82  
     }
 83  
 
 84  
     @Override
 85  
     protected DetailAST getLCurly()
 86  
     {
 87  258
         return getMainAst();
 88  
     }
 89  
 
 90  
     @Override
 91  
     protected DetailAST getRCurly()
 92  
     {
 93  220
         return getMainAst().findFirstToken(TokenTypes.RCURLY);
 94  
     }
 95  
 
 96  
     @Override
 97  
     protected DetailAST getToplevelAST()
 98  
     {
 99  72
         return null;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Determine if the expression we are handling has a block parent.
 104  
      *
 105  
      * @return true if it does, false otherwise
 106  
      */
 107  
     private boolean hasBlockParent()
 108  
     {
 109  426
         final int parentType = getMainAst().getParent().getType();
 110  426
         return (parentType == TokenTypes.LITERAL_IF)
 111  
             || (parentType == TokenTypes.LITERAL_FOR)
 112  
             || (parentType == TokenTypes.LITERAL_WHILE)
 113  
             || (parentType == TokenTypes.LITERAL_DO)
 114  
             || (parentType == TokenTypes.LITERAL_ELSE)
 115  
             || (parentType == TokenTypes.LITERAL_TRY)
 116  
             || (parentType == TokenTypes.LITERAL_CATCH)
 117  
             || (parentType == TokenTypes.LITERAL_FINALLY)
 118  
             || (parentType == TokenTypes.CTOR_DEF)
 119  
             || (parentType == TokenTypes.METHOD_DEF)
 120  
             || (parentType == TokenTypes.STATIC_INIT);
 121  
     }
 122  
 
 123  
     @Override
 124  
     public void checkIndentation()
 125  
     {
 126  
         // only need to check this if parent is not
 127  
         // an if, else, while, do, ctor, method
 128  426
         if (hasBlockParent()) {
 129  354
             return;
 130  
         }
 131  72
         super.checkIndentation();
 132  72
     }
 133  
 }