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.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 * Check the number of nested {@code for} -statements. The maximum
029 * number of nested layers can be configured. The default value is 1.
030 * The code for the class is copied from the NestedIfDepthCheck-class.
031 * The only difference is the intercepted token (for instead of if).
032 * Example:
033 * <pre>
034 *  &lt;!-- Restricts nested for blocks to a specified depth (default = 1).
035 *                                                                        --&gt;
036 *  &lt;module name=&quot;com.puppycrawl.tools.checkstyle.checks.coding
037 *                                            .CatchWithLostStackCheck&quot;&gt;
038 *    &lt;property name=&quot;severity&quot; value=&quot;info&quot;/&gt;
039 *    &lt;property name=&quot;max&quot; value=&quot;1&quot;/&gt;
040 *  &lt;/module&gt;
041 * </pre>
042 * @author Alexander Jesse
043 * @see NestedIfDepthCheck
044 */
045@FileStatefulCheck
046public final class NestedForDepthCheck extends AbstractCheck {
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties"
050     * file.
051     */
052    public static final String MSG_KEY = "nested.for.depth";
053
054    /** Maximum allowed nesting depth. */
055    private int max = 1;
056    /** Current nesting depth. */
057    private int depth;
058
059    /**
060     * Setter for maximum allowed nesting depth.
061     * @param max maximum allowed nesting depth.
062     */
063    public void setMax(int max) {
064        this.max = max;
065    }
066
067    @Override
068    public int[] getDefaultTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getAcceptableTokens() {
074        return getRequiredTokens();
075    }
076
077    @Override
078    public int[] getRequiredTokens() {
079        return new int[] {TokenTypes.LITERAL_FOR};
080    }
081
082    @Override
083    public void beginTree(DetailAST rootAST) {
084        depth = 0;
085    }
086
087    @Override
088    public void visitToken(DetailAST ast) {
089        if (depth > max) {
090            log(ast, MSG_KEY, depth, max);
091        }
092        ++depth;
093    }
094
095    @Override
096    public void leaveToken(DetailAST ast) {
097        --depth;
098    }
099
100}