001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 class definitions.
027 *
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                           AbstractExpressionHandler parent) {
042        super(indentCheck, getHandlerName(ast), ast, parent);
043    }
044
045    @Override
046    protected DetailAST getLeftCurly() {
047        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
048            .findFirstToken(TokenTypes.LCURLY);
049    }
050
051    @Override
052    protected DetailAST getRightCurly() {
053        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
054            .findFirstToken(TokenTypes.RCURLY);
055    }
056
057    @Override
058    protected DetailAST getTopLevelAst() {
059        return null;
060        // note: ident checked by hand in check indentation;
061    }
062
063    @Override
064    protected DetailAST getListChild() {
065        return getMainAst().findFirstToken(TokenTypes.OBJBLOCK);
066    }
067
068    @Override
069    public void checkIndentation() {
070        final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
071        if (modifiers.hasChildren()) {
072            checkModifiers();
073        }
074        else {
075            if (getMainAst().getType() != TokenTypes.ANNOTATION_DEF) {
076                final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
077                final int lineStart = getLineStart(ident);
078                if (!getIndent().isAcceptable(lineStart)) {
079                    logError(ident, "ident", lineStart);
080                }
081            }
082        }
083        if (getMainAst().getType() == TokenTypes.ANNOTATION_DEF) {
084            final DetailAST atAst = getMainAst().findFirstToken(TokenTypes.AT);
085            if (isOnStartOfLine(atAst)) {
086                checkWrappingIndentation(getMainAst(), getListChild(), 0,
087                        getIndent().getFirstIndentLevel(), false);
088            }
089        }
090        else {
091            checkWrappingIndentation(getMainAst(), getListChild());
092        }
093        super.checkIndentation();
094    }
095
096    @Override
097    protected int[] getCheckedChildren() {
098        return new int[] {
099            TokenTypes.EXPR,
100            TokenTypes.OBJBLOCK,
101            TokenTypes.LITERAL_BREAK,
102            TokenTypes.LITERAL_RETURN,
103            TokenTypes.LITERAL_THROW,
104            TokenTypes.LITERAL_CONTINUE,
105        };
106    }
107
108    /**
109     * Creates a handler name for this class according to ast type.
110     *
111     * @param ast the abstract syntax tree.
112     * @return handler name for this class.
113     */
114    private static String getHandlerName(DetailAST ast) {
115        final String name;
116        final int tokenType = ast.getType();
117
118        switch (tokenType) {
119            case TokenTypes.CLASS_DEF:
120                name = "class def";
121                break;
122            case TokenTypes.ENUM_DEF:
123                name = "enum def";
124                break;
125            case TokenTypes.ANNOTATION_DEF:
126                name = "annotation def";
127                break;
128            case TokenTypes.RECORD_DEF:
129                name = "record def";
130                break;
131            default:
132                name = "interface def";
133        }
134
135        return name;
136    }
137
138}