Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.SlistHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SlistHandler
100%
23/23
100%
18/18
1.778
 
 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.indentation;
 21  
 
 22  
 import java.util.Arrays;
 23  
 
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * Handler for a list of statements.
 29  
  *
 30  
  * @author jrichard
 31  
  */
 32  
 public class SlistHandler extends BlockParentHandler {
 33  
 
 34  
     /**
 35  
      * Parent token types.
 36  
      */
 37  1
     private static final int[] PARENT_TOKEN_TYPES = {
 38  
         TokenTypes.CTOR_DEF,
 39  
         TokenTypes.METHOD_DEF,
 40  
         TokenTypes.STATIC_INIT,
 41  
         TokenTypes.LITERAL_SYNCHRONIZED,
 42  
         TokenTypes.LITERAL_IF,
 43  
         TokenTypes.LITERAL_WHILE,
 44  
         TokenTypes.LITERAL_DO,
 45  
         TokenTypes.LITERAL_FOR,
 46  
         TokenTypes.LITERAL_ELSE,
 47  
         TokenTypes.LITERAL_TRY,
 48  
         TokenTypes.LITERAL_CATCH,
 49  
         TokenTypes.LITERAL_FINALLY,
 50  
     };
 51  
 
 52  
     static {
 53  
         // Array sorting for binary search
 54  1
         Arrays.sort(PARENT_TOKEN_TYPES);
 55  1
     }
 56  
 
 57  
     /**
 58  
      * Construct an instance of this handler with the given indentation check,
 59  
      * abstract syntax tree, and parent handler.
 60  
      *
 61  
      * @param indentCheck   the indentation check
 62  
      * @param ast           the abstract syntax tree
 63  
      * @param parent        the parent handler
 64  
      */
 65  
     public SlistHandler(IndentationCheck indentCheck,
 66  
         DetailAST ast, AbstractExpressionHandler parent) {
 67  803
         super(indentCheck, "block", ast, parent);
 68  803
     }
 69  
 
 70  
     @Override
 71  
     public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
 72  
         // this is:
 73  
         //  switch (var) {
 74  
         //     case 3: {
 75  
         //        break;
 76  
         //     }
 77  
         //  }
 78  
         //  ... the case SLIST is followed by a user-created SLIST and
 79  
         //  preceded by a switch
 80  
 
 81  
         final IndentLevel result;
 82  
         // if our parent is a block handler we want to be transparent
 83  823
         if (getParent() instanceof BlockParentHandler
 84  794
                 && !(getParent() instanceof SlistHandler)
 85  
             || child instanceof SlistHandler
 86  12
                 && getParent() instanceof CaseHandler) {
 87  772
             result = getParent().getSuggestedChildIndent(child);
 88  
         }
 89  
         else {
 90  51
             result = super.getSuggestedChildIndent(child);
 91  
         }
 92  823
         return result;
 93  
     }
 94  
 
 95  
     @Override
 96  
     protected DetailAST getListChild() {
 97  100
         return getMainAst();
 98  
     }
 99  
 
 100  
     @Override
 101  
     protected DetailAST getLeftCurly() {
 102  341
         return getMainAst();
 103  
     }
 104  
 
 105  
     @Override
 106  
     protected DetailAST getRightCurly() {
 107  337
         return getMainAst().findFirstToken(TokenTypes.RCURLY);
 108  
     }
 109  
 
 110  
     @Override
 111  
     protected DetailAST getTopLevelAst() {
 112  100
         return null;
 113  
     }
 114  
 
 115  
     /**
 116  
      * Determine if the expression we are handling has a block parent.
 117  
      *
 118  
      * @return true if it does, false otherwise
 119  
      */
 120  
     private boolean hasBlockParent() {
 121  803
         final int parentType = getMainAst().getParent().getType();
 122  803
         return Arrays.binarySearch(PARENT_TOKEN_TYPES, parentType) >= 0;
 123  
     }
 124  
 
 125  
     @Override
 126  
     public void checkIndentation() {
 127  
         // only need to check this if parent is not
 128  
         // an if, else, while, do, ctor, method
 129  803
         if (!hasBlockParent() && !isSameLineCaseGroup()) {
 130  100
             super.checkIndentation();
 131  
         }
 132  803
     }
 133  
 
 134  
     /**
 135  
      * Checks if SLIST node is placed at the same line as CASE_GROUP node.
 136  
      * @return true, if SLIST node is places at the same line as CASE_GROUP node.
 137  
      */
 138  
     private boolean isSameLineCaseGroup() {
 139  107
         final DetailAST parentNode = getMainAst().getParent();
 140  214
         return parentNode.getType() == TokenTypes.CASE_GROUP
 141  33
             && getMainAst().getLineNo() == parentNode.getLineNo();
 142  
     }
 143  
 }