1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <div>
29 * Restricts nested {@code for} blocks to a specified depth.
30 * </div>
31 * <ul>
32 * <li>
33 * Property {@code max} - Specify maximum allowed nesting depth.
34 * Type is {@code int}.
35 * Default value is {@code 1}.
36 * </li>
37 * </ul>
38 *
39 * <p>
40 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
41 * </p>
42 *
43 * <p>
44 * Violation Message Keys:
45 * </p>
46 * <ul>
47 * <li>
48 * {@code nested.for.depth}
49 * </li>
50 * </ul>
51 *
52 * @since 5.3
53 */
54 @FileStatefulCheck
55 public final class NestedForDepthCheck extends AbstractCheck {
56
57 /**
58 * A key is pointing to the warning message text in "messages.properties"
59 * file.
60 */
61 public static final String MSG_KEY = "nested.for.depth";
62
63 /** Specify maximum allowed nesting depth. */
64 private int max = 1;
65 /** Current nesting depth. */
66 private int depth;
67
68 /**
69 * Setter to specify maximum allowed nesting depth.
70 *
71 * @param max maximum allowed nesting depth.
72 * @since 5.3
73 */
74 public void setMax(int max) {
75 this.max = max;
76 }
77
78 @Override
79 public int[] getDefaultTokens() {
80 return getRequiredTokens();
81 }
82
83 @Override
84 public int[] getAcceptableTokens() {
85 return getRequiredTokens();
86 }
87
88 @Override
89 public int[] getRequiredTokens() {
90 return new int[] {TokenTypes.LITERAL_FOR};
91 }
92
93 @Override
94 public void visitToken(DetailAST ast) {
95 if (depth > max) {
96 log(ast, MSG_KEY, depth, max);
97 }
98 ++depth;
99 }
100
101 @Override
102 public void leaveToken(DetailAST ast) {
103 --depth;
104 }
105
106 }