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.sizes;
021
022import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * <div>
029 * Checks for the number of types declared at the <i>outer</i> (or <i>root</i>) level in a file.
030 * </div>
031 *
032 * <p>
033 * Rationale: It is considered good practice to only define one outer type per file.
034 * </p>
035 * <ul>
036 * <li>
037 * Property {@code max} - Specify the maximum number of outer types allowed.
038 * Type is {@code int}.
039 * Default value is {@code 1}.
040 * </li>
041 * </ul>
042 *
043 * <p>
044 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
045 * </p>
046 *
047 * <p>
048 * Violation Message Keys:
049 * </p>
050 * <ul>
051 * <li>
052 * {@code maxOuterTypes}
053 * </li>
054 * </ul>
055 *
056 * @since 5.0
057 */
058@FileStatefulCheck
059public class OuterTypeNumberCheck extends AbstractCheck {
060
061    /**
062     * A key is pointing to the warning message text in "messages.properties"
063     * file.
064     */
065    public static final String MSG_KEY = "maxOuterTypes";
066
067    /** Specify the maximum number of outer types allowed. */
068    private int max = 1;
069    /** Tracks the current depth in types. */
070    private int currentDepth;
071    /** Tracks the number of outer types found. */
072    private int outerNum;
073
074    @Override
075    public int[] getDefaultTokens() {
076        return getRequiredTokens();
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getRequiredTokens() {
086        return new int[] {
087            TokenTypes.CLASS_DEF,
088            TokenTypes.INTERFACE_DEF,
089            TokenTypes.ENUM_DEF,
090            TokenTypes.ANNOTATION_DEF,
091            TokenTypes.RECORD_DEF,
092        };
093    }
094
095    @Override
096    public void beginTree(DetailAST ast) {
097        currentDepth = 0;
098        outerNum = 0;
099    }
100
101    @Override
102    public void finishTree(DetailAST ast) {
103        if (max < outerNum) {
104            log(ast, MSG_KEY, outerNum, max);
105        }
106    }
107
108    @Override
109    public void visitToken(DetailAST ast) {
110        if (currentDepth == 0) {
111            outerNum++;
112        }
113        currentDepth++;
114    }
115
116    @Override
117    public void leaveToken(DetailAST ast) {
118        currentDepth--;
119    }
120
121    /**
122     * Setter to specify the maximum number of outer types allowed.
123     *
124     * @param max the new number.
125     * @since 5.0
126     */
127    public void setMax(int max) {
128        this.max = max;
129    }
130
131}