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 final IndentLevel indentLevel; 049 if (type == TokenTypes.LITERAL_NEW || type == TokenTypes.ASSIGN) { 050 // note: assumes new or assignment is line to align with 051 indentLevel = new IndentLevel(getLineStart(parentAST)); 052 } 053 else { 054 // at this point getParent() is instance of BlockParentHandler 055 indentLevel = ((BlockParentHandler) getParent()).getChildrenExpectedIndent(); 056 } 057 return indentLevel; 058 } 059 060 @Override 061 protected DetailAST getTopLevelAst() { 062 return null; 063 } 064 065 @Override 066 protected DetailAST getLeftCurly() { 067 return getMainAst(); 068 } 069 070 @Override 071 protected IndentLevel curlyIndent() { 072 final IndentLevel level = new IndentLevel(getIndent(), getBraceAdjustment()); 073 level.addAcceptedIndent(level.getLastIndentLevel() + getLineWrappingIndentation()); 074 return level; 075 } 076 077 @Override 078 protected DetailAST getRightCurly() { 079 return getMainAst().findFirstToken(TokenTypes.RCURLY); 080 } 081 082 @Override 083 protected boolean canChildrenBeNested() { 084 return true; 085 } 086 087 @Override 088 protected DetailAST getListChild() { 089 return getMainAst(); 090 } 091 092 @Override 093 protected IndentLevel getChildrenExpectedIndent() { 094 final IndentLevel expectedIndent = 095 new IndentLevel(getIndent(), getIndentCheck().getArrayInitIndent(), 096 getIndentCheck().getLineWrappingIndentation()); 097 098 final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild()); 099 final int lcurlyPos = expandedTabsColumnNo(getLeftCurly()); 100 final int firstChildPos = 101 getNextFirstNonBlankOnLineAfter(firstLine, lcurlyPos); 102 if (firstChildPos >= 0) { 103 expectedIndent.addAcceptedIndent(firstChildPos); 104 expectedIndent.addAcceptedIndent(lcurlyPos + getLineWrappingIndentation()); 105 } 106 return expectedIndent; 107 } 108 109 /** 110 * Returns column number of first non-blank char after 111 * specified column on specified line or -1 if 112 * such char doesn't exist. 113 * 114 * @param lineNo number of line on which we search 115 * @param columnNo number of column after which we search 116 * 117 * @return column number of first non-blank char after 118 * specified column on specified line or -1 if 119 * such char doesn't exist. 120 */ 121 private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) { 122 int realColumnNo = columnNo + 1; 123 final String line = getIndentCheck().getLines()[lineNo - 1]; 124 final int lineLength = line.length(); 125 while (realColumnNo < lineLength 126 && Character.isWhitespace(line.charAt(realColumnNo))) { 127 realColumnNo++; 128 } 129 130 if (realColumnNo == lineLength) { 131 realColumnNo = -1; 132 } 133 return realColumnNo; 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}