Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.ArrayInitHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayInitHandler
96%
28/29
83%
15/18
2.1
 
 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 array initialization blocks.
 26  
  *
 27  
  * @author jrichard
 28  
  */
 29  
 public class ArrayInitHandler 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 ArrayInitHandler(IndentationCheck aIndentCheck,
 40  
         DetailAST aAst, ExpressionHandler aParent)
 41  
     {
 42  60
         super(aIndentCheck, "array initialization", aAst, aParent);
 43  60
     }
 44  
 
 45  
     @Override
 46  
     protected IndentLevel getLevelImpl()
 47  
     {
 48  60
         final DetailAST parentAST = getMainAst().getParent();
 49  60
         final int type = parentAST.getType();
 50  60
         if ((type == TokenTypes.LITERAL_NEW) || (type == TokenTypes.ASSIGN)) {
 51  
             // note: assumes new or assignment is line to align with
 52  58
             return new IndentLevel(getLineStart(parentAST));
 53  
         }
 54  2
         else if (getParent() instanceof ArrayInitHandler) {
 55  2
             return ((ArrayInitHandler) getParent()).getChildrenExpectedLevel();
 56  
         }
 57  
         else {
 58  0
             return getParent().getLevel();
 59  
         }
 60  
     }
 61  
 
 62  
     @Override
 63  
     protected DetailAST getToplevelAST()
 64  
     {
 65  60
         return null;
 66  
     }
 67  
 
 68  
     @Override
 69  
     protected DetailAST getLCurly()
 70  
     {
 71  420
         return getMainAst();
 72  
     }
 73  
 
 74  
     @Override
 75  
     protected DetailAST getRCurly()
 76  
     {
 77  280
         return getMainAst().findFirstToken(TokenTypes.RCURLY);
 78  
     }
 79  
 
 80  
     @Override
 81  
     protected boolean rcurlyMustStart()
 82  
     {
 83  42
         return false;
 84  
     }
 85  
 
 86  
     @Override
 87  
     protected boolean childrenMayNest()
 88  
     {
 89  30
         return true;
 90  
     }
 91  
 
 92  
     @Override
 93  
     protected DetailAST getListChild()
 94  
     {
 95  100
         return getMainAst();
 96  
     }
 97  
 
 98  
     @Override
 99  
     protected IndentLevel getChildrenExpectedLevel()
 100  
     {
 101  
         // now we accept
 102  
         // new int[] {1,
 103  
         //            2};
 104  
         // and
 105  
         // new int[] {1, 2,
 106  
         //     3};
 107  
 
 108  40
         final IndentLevel expectedIndent = super.getChildrenExpectedLevel();
 109  
 
 110  40
         final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild());
 111  40
         if (hasCurlys() && (firstLine == getLCurly().getLineNo())) {
 112  40
             final int lcurlyPos = expandedTabsColumnNo(getLCurly());
 113  40
             final int firstChildPos =
 114  
                 getNextFirstNonblankOnLineAfter(firstLine, lcurlyPos);
 115  40
             if (firstChildPos >= 0) {
 116  3
                 expectedIndent.addAcceptedIndent(firstChildPos);
 117  
             }
 118  
         }
 119  40
         return expectedIndent;
 120  
     }
 121  
 
 122  
     /**
 123  
      * @param aLineNo   number of line on which we search
 124  
      * @param aColumnNo number of column after which we search
 125  
      *
 126  
      * @return column number of first non-blank char after
 127  
      *         specified column on specified line or -1 if
 128  
      *         such char doesn't exist.
 129  
      */
 130  
     private int getNextFirstNonblankOnLineAfter(int aLineNo, int aColumnNo)
 131  
     {
 132  40
         int columnNo = aColumnNo + 1;
 133  40
         final String line = getIndentCheck().getLines()[aLineNo - 1];
 134  40
         final int lineLength = line.length();
 135  
         while ((columnNo < lineLength)
 136  59
                && Character.isWhitespace(line.charAt(columnNo)))
 137  
         {
 138  19
             columnNo++;
 139  
         }
 140  
 
 141  40
         return (columnNo == lineLength) ? -1 : columnNo;
 142  
     }
 143  
 }