001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.indentation;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * Handler for a list of statements.
026 *
027 * @author jrichard
028 */
029public class SlistHandler extends BlockParentHandler
030{
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 SlistHandler(IndentationCheck indentCheck,
040        DetailAST ast, ExpressionHandler parent)
041    {
042        super(indentCheck, "block", ast, parent);
043    }
044
045    @Override
046    public IndentLevel suggestedChildLevel(ExpressionHandler child)
047    {
048        // this is:
049        //  switch (var) {
050        //     case 3: {
051        //        break;
052        //     }
053        //  }
054        //  ... the case SLIST is followed by a user-created SLIST and
055        //  preceded by a switch
056
057        // if our parent is a block handler we want to be transparent
058        if (((getParent() instanceof BlockParentHandler)
059                && !(getParent() instanceof SlistHandler))
060            || ((getParent() instanceof CaseHandler)
061                && (child instanceof SlistHandler)))
062        {
063            return getParent().suggestedChildLevel(child);
064        }
065        return super.suggestedChildLevel(child);
066    }
067
068    @Override
069    protected DetailAST getNonlistChild()
070    {
071        // blocks always have either block children or they are transparent
072        // and aren't checking children at all.  In the later case, the
073        // superclass will want to check single children, so when it
074        // does tell it we have none.
075        return null;
076    }
077
078    @Override
079    protected DetailAST getListChild()
080    {
081        return getMainAst();
082    }
083
084    @Override
085    protected DetailAST getLCurly()
086    {
087        return getMainAst();
088    }
089
090    @Override
091    protected DetailAST getRCurly()
092    {
093        return getMainAst().findFirstToken(TokenTypes.RCURLY);
094    }
095
096    @Override
097    protected DetailAST getToplevelAST()
098    {
099        return null;
100    }
101
102    /**
103     * Determine if the expression we are handling has a block parent.
104     *
105     * @return true if it does, false otherwise
106     */
107    private boolean hasBlockParent()
108    {
109        final int parentType = getMainAst().getParent().getType();
110        return (parentType == TokenTypes.LITERAL_IF)
111            || (parentType == TokenTypes.LITERAL_FOR)
112            || (parentType == TokenTypes.LITERAL_WHILE)
113            || (parentType == TokenTypes.LITERAL_DO)
114            || (parentType == TokenTypes.LITERAL_ELSE)
115            || (parentType == TokenTypes.LITERAL_TRY)
116            || (parentType == TokenTypes.LITERAL_CATCH)
117            || (parentType == TokenTypes.LITERAL_FINALLY)
118            || (parentType == TokenTypes.CTOR_DEF)
119            || (parentType == TokenTypes.METHOD_DEF)
120            || (parentType == TokenTypes.STATIC_INIT);
121    }
122
123    @Override
124    public void checkIndentation()
125    {
126        // only need to check this if parent is not
127        // an if, else, while, do, ctor, method
128        if (hasBlockParent() || isSameLineCaseGroup()) {
129            return;
130        }
131        super.checkIndentation();
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    {
140        final DetailAST parentNode = getMainAst().getParent();
141        return parentNode.getType() == TokenTypes.CASE_GROUP
142            && getMainAst().getLineNo() == parentNode.getLineNo();
143    }
144}