1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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 if-else 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.if.depth}
49 * </li>
50 * </ul>
51 *
52 * @since 3.2
53 */
54 @FileStatefulCheck
55 public final class NestedIfDepthCheck 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.if.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 3.2
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_IF};
91 }
92
93 @Override
94 public void visitToken(DetailAST literalIf) {
95 if (!isElseIf(literalIf)) {
96 if (depth > max) {
97 log(literalIf, MSG_KEY, depth, max);
98 }
99 ++depth;
100 }
101 }
102
103 @Override
104 public void leaveToken(DetailAST literalIf) {
105 if (!isElseIf(literalIf)) {
106 --depth;
107 }
108 }
109
110 /**
111 * Returns whether a token represents an ELSE as part of an ELSE / IF set.
112 *
113 * @param ast the token to check
114 * @return whether it is
115 */
116 private static boolean isElseIf(DetailAST ast) {
117 final DetailAST parentAST = ast.getParent();
118
119 return isElse(parentAST) || isElseWithCurlyBraces(parentAST);
120 }
121
122 /**
123 * Returns whether a token represents an ELSE.
124 *
125 * @param ast the token to check
126 * @return whether the token represents an ELSE
127 */
128 private static boolean isElse(DetailAST ast) {
129 return ast.getType() == TokenTypes.LITERAL_ELSE;
130 }
131
132 /**
133 * Returns whether a token represents an SLIST as part of an ELSE
134 * statement.
135 *
136 * @param ast the token to check
137 * @return whether the toke does represent an SLIST as part of an ELSE
138 */
139 private static boolean isElseWithCurlyBraces(DetailAST ast) {
140 return ast.getChildCount() == 2 && isElse(ast.getParent());
141 }
142 }