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 * @since 5.2
039 */
040@FileStatefulCheck
041public class InnerTypeLastCheck extends AbstractCheck {
042
043    /**
044     * A key is pointing to the warning message text in "messages.properties"
045     * file.
046     */
047    public static final String MSG_KEY = "arrangement.members.before.inner";
048
049    /** Set of class member tokens. */
050    private static final BitSet CLASS_MEMBER_TOKENS = TokenUtil.asBitSet(
051            TokenTypes.VARIABLE_DEF,
052            TokenTypes.METHOD_DEF,
053            TokenTypes.CTOR_DEF,
054            TokenTypes.INSTANCE_INIT,
055            TokenTypes.STATIC_INIT,
056            TokenTypes.COMPACT_CTOR_DEF
057    );
058
059    /** Meet a root class. */
060    private boolean rootClass;
061
062    @Override
063    public int[] getDefaultTokens() {
064        return getRequiredTokens();
065    }
066
067    @Override
068    public int[] getAcceptableTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getRequiredTokens() {
074        return new int[] {
075            TokenTypes.CLASS_DEF,
076            TokenTypes.INTERFACE_DEF,
077            TokenTypes.RECORD_DEF,
078        };
079    }
080
081    @Override
082    public void beginTree(DetailAST rootAST) {
083        rootClass = true;
084    }
085
086    @Override
087    public void visitToken(DetailAST ast) {
088        // First root class
089        if (rootClass) {
090            rootClass = false;
091        }
092        else {
093            DetailAST nextSibling = ast;
094            while (nextSibling != null) {
095                if (!ScopeUtil.isInCodeBlock(ast)
096                        && CLASS_MEMBER_TOKENS.get(nextSibling.getType())) {
097                    log(nextSibling, MSG_KEY);
098                }
099                nextSibling = nextSibling.getNextSibling();
100            }
101        }
102    }
103
104    @Override
105    public void leaveToken(DetailAST ast) {
106        if (TokenUtil.isRootNode(ast.getParent())) {
107            rootClass = true;
108        }
109    }
110
111}