001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.indentation; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * Handler for array initialization blocks. 027 * 028 * @author jrichard 029 */ 030public class ArrayInitHandler extends BlockParentHandler { 031 /** 032 * Construct an instance of this handler with the given indentation check, 033 * abstract syntax tree, and parent handler. 034 * 035 * @param indentCheck the indentation check 036 * @param ast the abstract syntax tree 037 * @param parent the parent handler 038 */ 039 public ArrayInitHandler(IndentationCheck indentCheck, 040 DetailAST ast, AbstractExpressionHandler parent) { 041 super(indentCheck, "array initialization", ast, parent); 042 } 043 044 @Override 045 protected IndentLevel getIndentImpl() { 046 final DetailAST parentAST = getMainAst().getParent(); 047 final int type = parentAST.getType(); 048 if (type == TokenTypes.LITERAL_NEW || type == TokenTypes.ASSIGN) { 049 // note: assumes new or assignment is line to align with 050 return new IndentLevel(getLineStart(parentAST)); 051 } 052 else { 053 // at this point getParent() is instance of BlockParentHandler 054 return ((BlockParentHandler) getParent()).getChildrenExpectedIndent(); 055 } 056 } 057 058 @Override 059 protected DetailAST getTopLevelAst() { 060 return null; 061 } 062 063 @Override 064 protected DetailAST getLCurly() { 065 return getMainAst(); 066 } 067 068 @Override 069 protected IndentLevel curlyIndent() { 070 final IndentLevel level = new IndentLevel(getIndent(), getBraceAdjustment()); 071 level.addAcceptedIndent(level.getLastIndentLevel() + getLineWrappingIndentation()); 072 return level; 073 } 074 075 @Override 076 protected DetailAST getRCurly() { 077 return getMainAst().findFirstToken(TokenTypes.RCURLY); 078 } 079 080 @Override 081 protected boolean canChildrenBeNested() { 082 return true; 083 } 084 085 @Override 086 protected DetailAST getListChild() { 087 return getMainAst(); 088 } 089 090 @Override 091 protected IndentLevel getChildrenExpectedIndent() { 092 final IndentLevel expectedIndent = 093 new IndentLevel(getIndent(), getIndentCheck().getArrayInitIndent(), 094 getIndentCheck().getLineWrappingIndentation()); 095 096 final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild()); 097 final int lcurlyPos = expandedTabsColumnNo(getLCurly()); 098 final int firstChildPos = 099 getNextFirstNonBlankOnLineAfter(firstLine, lcurlyPos); 100 if (firstChildPos >= 0) { 101 expectedIndent.addAcceptedIndent(firstChildPos); 102 expectedIndent.addAcceptedIndent(lcurlyPos + getLineWrappingIndentation()); 103 } 104 return expectedIndent; 105 } 106 107 /** 108 * @param lineNo number of line on which we search 109 * @param columnNo number of column after which we search 110 * 111 * @return column number of first non-blank char after 112 * specified column on specified line or -1 if 113 * such char doesn't exist. 114 */ 115 private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) { 116 int realColumnNo = columnNo + 1; 117 final String line = getIndentCheck().getLines()[lineNo - 1]; 118 final int lineLength = line.length(); 119 while (realColumnNo < lineLength 120 && Character.isWhitespace(line.charAt(realColumnNo))) { 121 realColumnNo++; 122 } 123 124 if (realColumnNo == lineLength) { 125 return -1; 126 } 127 else { 128 return realColumnNo; 129 } 130 } 131 132 /** 133 * A shortcut for {@code IndentationCheck} property. 134 * @return value of lineWrappingIndentation property 135 * of {@code IndentationCheck} 136 */ 137 private int getLineWrappingIndentation() { 138 return getIndentCheck().getLineWrappingIndentation(); 139 } 140}