001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 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 * Type is {@code int}.
035 * Default value is {@code 1}.
036 * </li>
037 * </ul>
038 * <p>
039 * To configure the check:
040 * </p>
041 * <pre>
042 * &lt;module name=&quot;NestedForDepth&quot;/&gt;
043 * </pre>
044 * <p>Example:</p>
045 * <pre>
046 * for(int i=0; i&lt;10; i++) {
047 *   for(int j=0; j&lt;i; j++) {
048 *     for(int k=0; k&lt;j; k++) { // violation, max allowed nested loop number is 1
049 *     }
050 *   }
051 * }
052 *
053 * for(int i=0; i&lt;10; i++) {
054 *   for(int j=0; j&lt;i; j++) { // ok
055 *   }
056 * }
057 * </pre>
058 * <p>
059 * To configure the check to allow nesting depth 2:
060 * </p>
061 * <pre>
062 * &lt;module name=&quot;NestedForDepth&quot;&gt;
063 *   &lt;property name=&quot;max&quot; value=&quot;2&quot;/&gt;
064 * &lt;/module&gt;
065 * </pre>
066 * <p>Example:</p>
067 * <pre>
068 * for(int i=0; i&lt;10; i++) {
069 *   for(int j=0; j&lt;i; j++) {
070 *     for(int k=0; k&lt;j; k++) {
071 *       for(int l=0; l&lt;k; l++) { // violation, max allowed nested loop number is 2
072 *       }
073 *     }
074 *    }
075 * }
076 *
077 * for(int i=0; i&lt;10; i++) {
078 *   for(int j=0; j&lt;i; j++) {
079 *     for(int k=0; k&lt;j; k++) { // ok
080 *     }
081 *   }
082 * }
083 * </pre>
084 * <p>
085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
086 * </p>
087 * <p>
088 * Violation Message Keys:
089 * </p>
090 * <ul>
091 * <li>
092 * {@code nested.for.depth}
093 * </li>
094 * </ul>
095 *
096 * @since 5.3
097 */
098@FileStatefulCheck
099public final class NestedForDepthCheck extends AbstractCheck {
100
101    /**
102     * A key is pointing to the warning message text in "messages.properties"
103     * file.
104     */
105    public static final String MSG_KEY = "nested.for.depth";
106
107    /** Specify maximum allowed nesting depth. */
108    private int max = 1;
109    /** Current nesting depth. */
110    private int depth;
111
112    /**
113     * Setter to specify maximum allowed nesting depth.
114     *
115     * @param max maximum allowed nesting depth.
116     */
117    public void setMax(int max) {
118        this.max = max;
119    }
120
121    @Override
122    public int[] getDefaultTokens() {
123        return getRequiredTokens();
124    }
125
126    @Override
127    public int[] getAcceptableTokens() {
128        return getRequiredTokens();
129    }
130
131    @Override
132    public int[] getRequiredTokens() {
133        return new int[] {TokenTypes.LITERAL_FOR};
134    }
135
136    @Override
137    public void beginTree(DetailAST rootAST) {
138        depth = 0;
139    }
140
141    @Override
142    public void visitToken(DetailAST ast) {
143        if (depth > max) {
144            log(ast, MSG_KEY, depth, max);
145        }
146        ++depth;
147    }
148
149    @Override
150    public void leaveToken(DetailAST ast) {
151        --depth;
152    }
153
154}