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 * @see NestedIfDepthCheck
043 */
044@FileStatefulCheck
045public final class NestedForDepthCheck extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties"
049     * file.
050     */
051    public static final String MSG_KEY = "nested.for.depth";
052
053    /** Maximum allowed nesting depth. */
054    private int max = 1;
055    /** Current nesting depth. */
056    private int depth;
057
058    /**
059     * Setter for maximum allowed nesting depth.
060     * @param max maximum allowed nesting depth.
061     */
062    public void setMax(int max) {
063        this.max = max;
064    }
065
066    @Override
067    public int[] getDefaultTokens() {
068        return getRequiredTokens();
069    }
070
071    @Override
072    public int[] getAcceptableTokens() {
073        return getRequiredTokens();
074    }
075
076    @Override
077    public int[] getRequiredTokens() {
078        return new int[] {TokenTypes.LITERAL_FOR};
079    }
080
081    @Override
082    public void beginTree(DetailAST rootAST) {
083        depth = 0;
084    }
085
086    @Override
087    public void visitToken(DetailAST ast) {
088        if (depth > max) {
089            log(ast, MSG_KEY, depth, max);
090        }
091        ++depth;
092    }
093
094    @Override
095    public void leaveToken(DetailAST ast) {
096        --depth;
097    }
098
099}