001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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 * Checks for the number of defined types at the "outer" level.
029 */
030@FileStatefulCheck
031public class OuterTypeNumberCheck extends AbstractCheck {
032
033    /**
034     * A key is pointing to the warning message text in "messages.properties"
035     * file.
036     */
037    public static final String MSG_KEY = "maxOuterTypes";
038
039    /** The maximum allowed number of outer types. */
040    private int max = 1;
041    /** Tracks the current depth in types. */
042    private int currentDepth;
043    /** Tracks the number of outer types found. */
044    private int outerNum;
045
046    @Override
047    public int[] getDefaultTokens() {
048        return getRequiredTokens();
049    }
050
051    @Override
052    public int[] getAcceptableTokens() {
053        return getRequiredTokens();
054    }
055
056    @Override
057    public int[] getRequiredTokens() {
058        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
059            TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, };
060    }
061
062    @Override
063    public void beginTree(DetailAST ast) {
064        currentDepth = 0;
065        outerNum = 0;
066    }
067
068    @Override
069    public void finishTree(DetailAST ast) {
070        if (max < outerNum) {
071            log(ast, MSG_KEY, outerNum, max);
072        }
073    }
074
075    @Override
076    public void visitToken(DetailAST ast) {
077        if (currentDepth == 0) {
078            outerNum++;
079        }
080        currentDepth++;
081    }
082
083    @Override
084    public void leaveToken(DetailAST ast) {
085        currentDepth--;
086    }
087
088    /**
089     * Sets the maximum allowed number of outer types.
090     * @param max the new number.
091     */
092    public void setMax(int max) {
093        this.max = max;
094    }
095
096}