001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.
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     *
071     * @param max maximum allowed nesting depth.
072     */
073    public void setMax(int max) {
074        this.max = max;
075    }
076
077    @Override
078    public int[] getDefaultTokens() {
079        return getRequiredTokens();
080    }
081
082    @Override
083    public int[] getAcceptableTokens() {
084        return getRequiredTokens();
085    }
086
087    @Override
088    public int[] getRequiredTokens() {
089        return new int[] {TokenTypes.LITERAL_FOR};
090    }
091
092    @Override
093    public void beginTree(DetailAST rootAST) {
094        depth = 0;
095    }
096
097    @Override
098    public void visitToken(DetailAST ast) {
099        if (depth > max) {
100            log(ast, MSG_KEY, depth, max);
101        }
102        ++depth;
103    }
104
105    @Override
106    public void leaveToken(DetailAST ast) {
107        --depth;
108    }
109
110}