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 class definitions.
026 *
027 * @author jrichard
028 */
029public class ClassDefHandler 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 ClassDefHandler(IndentationCheck indentCheck,
040                           DetailAST ast,
041                           ExpressionHandler parent)
042    {
043        super(indentCheck,
044              (ast.getType() == TokenTypes.CLASS_DEF)
045              ? "class def" : ((ast.getType() == TokenTypes.ENUM_DEF)
046                  ? "enum def" : "interface def"),
047              ast, parent);
048    }
049
050    @Override
051    protected DetailAST getLCurly()
052    {
053        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
054            .findFirstToken(TokenTypes.LCURLY);
055    }
056
057    @Override
058    protected DetailAST getRCurly()
059    {
060        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
061            .findFirstToken(TokenTypes.RCURLY);
062    }
063
064    @Override
065    protected DetailAST getToplevelAST()
066    {
067        return null;
068        // note: ident checked by hand in check indentation;
069    }
070
071    @Override
072    protected DetailAST getListChild()
073    {
074        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK);
075    }
076
077    @Override
078    public void checkIndentation()
079    {
080        final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
081        if (modifiers.getChildCount() == 0) {
082            final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
083            final int lineStart = getLineStart(ident);
084            if (!getLevel().accept(lineStart)) {
085                logError(ident, "ident", lineStart);
086            }
087
088        }
089        else {
090            checkModifiers();
091        }
092
093        final LineWrappingHandler lineWrap =
094            new LineWrappingHandler(getIndentCheck(), getMainAst()) {
095                @Override
096                public DetailAST findLastNode(DetailAST firstNode)
097                {
098                    return firstNode.getLastChild();
099                }
100            };
101        lineWrap.checkIndentation();
102        super.checkIndentation();
103    }
104
105    @Override
106    protected int[] getCheckedChildren()
107    {
108        return new int[] {
109            TokenTypes.EXPR,
110            TokenTypes.OBJBLOCK,
111            TokenTypes.LITERAL_BREAK,
112            TokenTypes.LITERAL_RETURN,
113            TokenTypes.LITERAL_THROW,
114            TokenTypes.LITERAL_CONTINUE,
115        };
116    }
117
118}