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 java.util.Arrays;
023
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * Handler for a list of statements.
029 *
030 * @author jrichard
031 */
032public class SlistHandler extends BlockParentHandler {
033
034    /**
035     * Parent token types.
036     */
037    private static final int[] PARENT_TOKEN_TYPES = {
038        TokenTypes.CTOR_DEF,
039        TokenTypes.METHOD_DEF,
040        TokenTypes.STATIC_INIT,
041        TokenTypes.LITERAL_SYNCHRONIZED,
042        TokenTypes.LITERAL_IF,
043        TokenTypes.LITERAL_WHILE,
044        TokenTypes.LITERAL_DO,
045        TokenTypes.LITERAL_FOR,
046        TokenTypes.LITERAL_ELSE,
047        TokenTypes.LITERAL_TRY,
048        TokenTypes.LITERAL_CATCH,
049        TokenTypes.LITERAL_FINALLY,
050    };
051
052    static {
053        // Array sorting for binary search
054        Arrays.sort(PARENT_TOKEN_TYPES);
055    }
056
057    /**
058     * Construct an instance of this handler with the given indentation check,
059     * abstract syntax tree, and parent handler.
060     *
061     * @param indentCheck   the indentation check
062     * @param ast           the abstract syntax tree
063     * @param parent        the parent handler
064     */
065    public SlistHandler(IndentationCheck indentCheck,
066        DetailAST ast, AbstractExpressionHandler parent) {
067        super(indentCheck, "block", ast, parent);
068    }
069
070    @Override
071    public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
072        // this is:
073        //  switch (var) {
074        //     case 3: {
075        //        break;
076        //     }
077        //  }
078        //  ... the case SLIST is followed by a user-created SLIST and
079        //  preceded by a switch
080
081        final IndentLevel result;
082        // if our parent is a block handler we want to be transparent
083        if (getParent() instanceof BlockParentHandler
084                && !(getParent() instanceof SlistHandler)
085            || child instanceof SlistHandler
086                && getParent() instanceof CaseHandler) {
087            result = getParent().getSuggestedChildIndent(child);
088        }
089        else {
090            result = super.getSuggestedChildIndent(child);
091        }
092        return result;
093    }
094
095    @Override
096    protected DetailAST getListChild() {
097        return getMainAst();
098    }
099
100    @Override
101    protected DetailAST getLeftCurly() {
102        return getMainAst();
103    }
104
105    @Override
106    protected DetailAST getRightCurly() {
107        return getMainAst().findFirstToken(TokenTypes.RCURLY);
108    }
109
110    @Override
111    protected DetailAST getTopLevelAst() {
112        return null;
113    }
114
115    /**
116     * Determine if the expression we are handling has a block parent.
117     *
118     * @return true if it does, false otherwise
119     */
120    private boolean hasBlockParent() {
121        final int parentType = getMainAst().getParent().getType();
122        return Arrays.binarySearch(PARENT_TOKEN_TYPES, parentType) >= 0;
123    }
124
125    @Override
126    public void checkIndentation() {
127        // only need to check this if parent is not
128        // an if, else, while, do, ctor, method
129        if (!hasBlockParent() && !isSameLineCaseGroup()) {
130            super.checkIndentation();
131        }
132    }
133
134    /**
135     * Checks if SLIST node is placed at the same line as CASE_GROUP node.
136     * @return true, if SLIST node is places at the same line as CASE_GROUP node.
137     */
138    private boolean isSameLineCaseGroup() {
139        final DetailAST parentNode = getMainAst().getParent();
140        return parentNode.getType() == TokenTypes.CASE_GROUP
141            && getMainAst().getLineNo() == parentNode.getLineNo();
142    }
143
144}