001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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;
026import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
027
028/**
029 * Handler for a list of statements.
030 *
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        TokenTypes.COMPACT_CTOR_DEF,
051    };
052
053    static {
054        // Array sorting for binary search
055        Arrays.sort(PARENT_TOKEN_TYPES);
056    }
057
058    /**
059     * Construct an instance of this handler with the given indentation check,
060     * abstract syntax tree, and parent handler.
061     *
062     * @param indentCheck   the indentation check
063     * @param ast           the abstract syntax tree
064     * @param parent        the parent handler
065     */
066    public SlistHandler(IndentationCheck indentCheck,
067        DetailAST ast, AbstractExpressionHandler parent) {
068        super(indentCheck, "block", ast, parent);
069    }
070
071    @Override
072    public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
073        // this is:
074        //  switch (var) {
075        //     case 3: {
076        //        break;
077        //     }
078        //  }
079        //  ... the case SLIST is followed by a user-created SLIST and
080        //  preceded by a switch
081
082        final IndentLevel result;
083        // if our parent is a block handler we want to be transparent
084        if (getParent() instanceof BlockParentHandler
085                && !(getParent() instanceof SlistHandler)
086            || child instanceof SlistHandler
087                && getParent() instanceof CaseHandler) {
088            result = getParent().getSuggestedChildIndent(child);
089        }
090        else {
091            result = super.getSuggestedChildIndent(child);
092        }
093        return result;
094    }
095
096    @Override
097    protected DetailAST getListChild() {
098        return getMainAst();
099    }
100
101    @Override
102    protected DetailAST getLeftCurly() {
103        return getMainAst();
104    }
105
106    @Override
107    protected DetailAST getRightCurly() {
108        return getMainAst().findFirstToken(TokenTypes.RCURLY);
109    }
110
111    @Override
112    protected DetailAST getTopLevelAst() {
113        return null;
114    }
115
116    /**
117     * Determine if the expression we are handling has a block parent.
118     *
119     * @return true if it does, false otherwise
120     */
121    private boolean hasBlockParent() {
122        final int parentType = getMainAst().getParent().getType();
123        return Arrays.binarySearch(PARENT_TOKEN_TYPES, parentType) >= 0;
124    }
125
126    @Override
127    public void checkIndentation() {
128        // only need to check this if parent is not
129        // an if, else, while, do, ctor, method
130        if (!hasBlockParent() && !isSameLineCaseGroup()) {
131            super.checkIndentation();
132        }
133    }
134
135    /**
136     * Checks if SLIST node is placed at the same line as CASE_GROUP node.
137     *
138     * @return true, if SLIST node is places at the same line as CASE_GROUP node.
139     */
140    private boolean isSameLineCaseGroup() {
141        final DetailAST parentNode = getMainAst().getParent();
142        return parentNode.getType() == TokenTypes.CASE_GROUP
143            && TokenUtil.areOnSameLine(getMainAst(), parentNode);
144    }
145
146}