001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.design;
021
022import java.util.BitSet;
023
024import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
029import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
030
031/**
032 * <div>
033 * Checks nested (internal) classes/interfaces are declared at the bottom of the
034 * primary (top-level) class after all init and static init blocks,
035 * method, constructor and field declarations.
036 * </div>
037 *
038 * <p>
039 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
040 * </p>
041 *
042 * <p>
043 * Violation Message Keys:
044 * </p>
045 * <ul>
046 * <li>
047 * {@code arrangement.members.before.inner}
048 * </li>
049 * </ul>
050 *
051 * @since 5.2
052 */
053@FileStatefulCheck
054public class InnerTypeLastCheck extends AbstractCheck {
055
056    /**
057     * A key is pointing to the warning message text in "messages.properties"
058     * file.
059     */
060    public static final String MSG_KEY = "arrangement.members.before.inner";
061
062    /** Set of class member tokens. */
063    private static final BitSet CLASS_MEMBER_TOKENS = TokenUtil.asBitSet(
064            TokenTypes.VARIABLE_DEF,
065            TokenTypes.METHOD_DEF,
066            TokenTypes.CTOR_DEF,
067            TokenTypes.INSTANCE_INIT,
068            TokenTypes.STATIC_INIT,
069            TokenTypes.COMPACT_CTOR_DEF
070    );
071
072    /** Meet a root class. */
073    private boolean rootClass;
074
075    @Override
076    public int[] getDefaultTokens() {
077        return getRequiredTokens();
078    }
079
080    @Override
081    public int[] getAcceptableTokens() {
082        return getRequiredTokens();
083    }
084
085    @Override
086    public int[] getRequiredTokens() {
087        return new int[] {
088            TokenTypes.CLASS_DEF,
089            TokenTypes.INTERFACE_DEF,
090            TokenTypes.RECORD_DEF,
091        };
092    }
093
094    @Override
095    public void beginTree(DetailAST rootAST) {
096        rootClass = true;
097    }
098
099    @Override
100    public void visitToken(DetailAST ast) {
101        // First root class
102        if (rootClass) {
103            rootClass = false;
104        }
105        else {
106            DetailAST nextSibling = ast;
107            while (nextSibling != null) {
108                if (!ScopeUtil.isInCodeBlock(ast)
109                        && CLASS_MEMBER_TOKENS.get(nextSibling.getType())) {
110                    log(nextSibling, MSG_KEY);
111                }
112                nextSibling = nextSibling.getNextSibling();
113            }
114        }
115    }
116
117    @Override
118    public void leaveToken(DetailAST ast) {
119        if (TokenUtil.isRootNode(ast.getParent())) {
120            rootClass = true;
121        }
122    }
123
124}