001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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 getLeftCurly() { 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 getRightCurly() { 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(getLeftCurly()); 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 * Returns column number of first non-blank char after 109 * specified column on specified line or -1 if 110 * such char doesn't exist. 111 * 112 * @param lineNo number of line on which we search 113 * @param columnNo number of column after which we search 114 * 115 * @return column number of first non-blank char after 116 * specified column on specified line or -1 if 117 * such char doesn't exist. 118 */ 119 private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) { 120 int realColumnNo = columnNo + 1; 121 final String line = getIndentCheck().getLines()[lineNo - 1]; 122 final int lineLength = line.length(); 123 while (realColumnNo < lineLength 124 && Character.isWhitespace(line.charAt(realColumnNo))) { 125 realColumnNo++; 126 } 127 128 if (realColumnNo == lineLength) { 129 return -1; 130 } 131 else { 132 return realColumnNo; 133 } 134 } 135 136 /** 137 * A shortcut for {@code IndentationCheck} property. 138 * @return value of lineWrappingIndentation property 139 * of {@code IndentationCheck} 140 */ 141 private int getLineWrappingIndentation() { 142 return getIndentCheck().getLineWrappingIndentation(); 143 } 144}