001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * Check the number of nested {@code for} -statements. The maximum
027 * number of nested layers can be configured. The default value is 1. Most of
028 * the logic is implemented in the parent class. The code for the class is
029 * copied from the NestedIfDepthCheck-class. The only difference is the
030 * intercepted token (for instead of if). Example:
031 * <pre>
032 *  &lt;!-- Restricts nested for blocks to a specified depth (default = 1).
033 *                                                                        --&gt;
034 *  &lt;module name=&quot;com.puppycrawl.tools.checkstyle.checks.coding
035 *                                            .CatchWithLostStackCheck&quot;&gt;
036 *    &lt;property name=&quot;severity&quot; value=&quot;info&quot;/&gt;
037 *    &lt;property name=&quot;max&quot; value=&quot;1&quot;/&gt;
038 *  &lt;/module&gt;
039 * </pre>
040 * @author Alexander Jesse
041 * @see AbstractNestedDepthCheck
042 * @see NestedIfDepthCheck
043 */
044public final class NestedForDepthCheck extends AbstractNestedDepthCheck {
045
046    /**
047     * A key is pointing to the warning message text in "messages.properties"
048     * file.
049     */
050    public static final String MSG_KEY = "nested.for.depth";
051
052    /** Default allowed nesting depth. */
053    private static final int DEFAULT_MAX = 1;
054
055    /** Creates new check instance with default allowed nesting depth. */
056    public NestedForDepthCheck() {
057        super(DEFAULT_MAX);
058    }
059
060    @Override
061    public int[] getDefaultTokens() {
062        return new int[] {TokenTypes.LITERAL_FOR};
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return new int[] {TokenTypes.LITERAL_FOR};
068    }
069
070    @Override
071    public void visitToken(DetailAST ast) {
072        nestIn(ast, MSG_KEY);
073    }
074
075    @Override
076    public void leaveToken(DetailAST ast) {
077        nestOut();
078    }
079}