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