001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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    /**
033     * Construct an instance of this handler with the given indentation check,
034     * abstract syntax tree, and parent handler.
035     *
036     * @param indentCheck   the indentation check
037     * @param ast           the abstract syntax tree
038     * @param parent        the parent handler
039     */
040    public ArrayInitHandler(IndentationCheck indentCheck,
041        DetailAST ast, AbstractExpressionHandler parent) {
042        super(indentCheck, "array initialization", ast, parent);
043    }
044
045    @Override
046    protected IndentLevel getIndentImpl() {
047        final DetailAST parentAST = getMainAst().getParent();
048        final int type = parentAST.getType();
049        final IndentLevel indentLevel;
050        if (type == TokenTypes.LITERAL_NEW || type == TokenTypes.ASSIGN) {
051            // note: assumes new or assignment is line to align with
052            indentLevel = new IndentLevel(getLineStart(parentAST));
053        }
054        else {
055            // at this point getParent() is instance of BlockParentHandler
056            indentLevel = ((BlockParentHandler) getParent()).getChildrenExpectedIndent();
057        }
058        return indentLevel;
059    }
060
061    @Override
062    protected DetailAST getTopLevelAst() {
063        return null;
064    }
065
066    @Override
067    protected DetailAST getLeftCurly() {
068        return getMainAst();
069    }
070
071    @Override
072    protected IndentLevel curlyIndent() {
073        final IndentLevel level = new IndentLevel(getIndent(), getBraceAdjustment());
074        level.addAcceptedIndent(level.getLastIndentLevel() + getLineWrappingIndentation());
075        return level;
076    }
077
078    @Override
079    protected DetailAST getRightCurly() {
080        return getMainAst().findFirstToken(TokenTypes.RCURLY);
081    }
082
083    @Override
084    protected boolean canChildrenBeNested() {
085        return true;
086    }
087
088    @Override
089    protected DetailAST getListChild() {
090        return getMainAst();
091    }
092
093    @Override
094    protected IndentLevel getChildrenExpectedIndent() {
095        final IndentLevel expectedIndent =
096            new IndentLevel(getIndent(), getIndentCheck().getArrayInitIndent(),
097                    getIndentCheck().getLineWrappingIndentation());
098
099        final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild());
100        final int lcurlyPos = expandedTabsColumnNo(getLeftCurly());
101        final int firstChildPos =
102            getNextFirstNonBlankOnLineAfter(firstLine, lcurlyPos);
103        if (firstChildPos >= 0) {
104            expectedIndent.addAcceptedIndent(firstChildPos);
105            expectedIndent.addAcceptedIndent(lcurlyPos + getLineWrappingIndentation());
106        }
107        return expectedIndent;
108    }
109
110    /**
111     * Returns 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     * @param lineNo   number of line on which we search
116     * @param columnNo number of column after which we search
117     *
118     * @return column number of first non-blank char after
119     *         specified column on specified line or -1 if
120     *         such char doesn't exist.
121     */
122    private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) {
123        int realColumnNo = columnNo + 1;
124        final String line = getIndentCheck().getLines()[lineNo - 1];
125        final int lineLength = line.length();
126        while (realColumnNo < lineLength
127               && Character.isWhitespace(line.charAt(realColumnNo))) {
128            realColumnNo++;
129        }
130
131        if (realColumnNo == lineLength) {
132            realColumnNo = -1;
133        }
134        return realColumnNo;
135    }
136
137    /**
138     * A shortcut for {@code IndentationCheck} property.
139     * @return value of lineWrappingIndentation property
140     *         of {@code IndentationCheck}
141     */
142    private int getLineWrappingIndentation() {
143        return getIndentCheck().getLineWrappingIndentation();
144    }
145
146}