001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.coding;
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 * Restricts nested {@code for} blocks to a specified depth (default = 1).
030 * </p>
031 * <ul>
032 * <li>
033 * Property {@code max} - Specify maximum allowed nesting depth.
034 * Default value is {@code 1}.
035 * </li>
036 * </ul>
037 * <p>
038 * To configure the check:
039 * </p>
040 * <pre>
041 * &lt;module name=&quot;NestedForDepth&quot;/&gt;
042 * </pre>
043 * <p>
044 * To configure the check to allow nesting depth 3:
045 * </p>
046 * <pre>
047 * &lt;module name=&quot;NestedForDepth&quot;&gt;
048 *   &lt;property name=&quot;max&quot; value=&quot;3&quot;/&gt;
049 * &lt;/module&gt;
050 * </pre>
051 *
052 * @since 5.3
053 */
054@FileStatefulCheck
055public final class NestedForDepthCheck extends AbstractCheck {
056
057    /**
058     * A key is pointing to the warning message text in "messages.properties"
059     * file.
060     */
061    public static final String MSG_KEY = "nested.for.depth";
062
063    /** Specify maximum allowed nesting depth. */
064    private int max = 1;
065    /** Current nesting depth. */
066    private int depth;
067
068    /**
069     * Setter to specify maximum allowed nesting depth.
070     * @param max maximum allowed nesting depth.
071     */
072    public void setMax(int max) {
073        this.max = max;
074    }
075
076    @Override
077    public int[] getDefaultTokens() {
078        return getRequiredTokens();
079    }
080
081    @Override
082    public int[] getAcceptableTokens() {
083        return getRequiredTokens();
084    }
085
086    @Override
087    public int[] getRequiredTokens() {
088        return new int[] {TokenTypes.LITERAL_FOR};
089    }
090
091    @Override
092    public void beginTree(DetailAST rootAST) {
093        depth = 0;
094    }
095
096    @Override
097    public void visitToken(DetailAST ast) {
098        if (depth > max) {
099            log(ast, MSG_KEY, depth, max);
100        }
101        ++depth;
102    }
103
104    @Override
105    public void leaveToken(DetailAST ast) {
106        --depth;
107    }
108
109}