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 * <p>
29 * Restricts nested if-else blocks to a specified depth.
30 * </p>
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 * <p>
39 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
40 * </p>
41 * <p>
42 * Violation Message Keys:
43 * </p>
44 * <ul>
45 * <li>
46 * {@code nested.if.depth}
47 * </li>
48 * </ul>
49 *
50 * @since 3.2
51 */
52 @FileStatefulCheck
53 public final class NestedIfDepthCheck extends AbstractCheck {
54
55 /**
56 * A key is pointing to the warning message text in "messages.properties"
57 * file.
58 */
59 public static final String MSG_KEY = "nested.if.depth";
60
61 /** Specify maximum allowed nesting depth. */
62 private int max = 1;
63 /** Current nesting depth. */
64 private int depth;
65
66 /**
67 * Setter to specify maximum allowed nesting depth.
68 *
69 * @param max maximum allowed nesting depth.
70 * @since 3.2
71 */
72 public void setMax(int max) {
73 this.max = max;
74 }
75
76 @Override
77 public int[] getDefaultTokens() {
78 return getRequiredTokens();
79 }
80
81 @Override
82 public int[] getAcceptableTokens() {
83 return getRequiredTokens();
84 }
85
86 @Override
87 public int[] getRequiredTokens() {
88 return new int[] {TokenTypes.LITERAL_IF};
89 }
90
91 @Override
92 public void visitToken(DetailAST literalIf) {
93 if (!isElseIf(literalIf)) {
94 if (depth > max) {
95 log(literalIf, MSG_KEY, depth, max);
96 }
97 ++depth;
98 }
99 }
100
101 @Override
102 public void leaveToken(DetailAST literalIf) {
103 if (!isElseIf(literalIf)) {
104 --depth;
105 }
106 }
107
108 /**
109 * Returns whether a token represents an ELSE as part of an ELSE / IF set.
110 *
111 * @param ast the token to check
112 * @return whether it is
113 */
114 private static boolean isElseIf(DetailAST ast) {
115 final DetailAST parentAST = ast.getParent();
116
117 return isElse(parentAST) || isElseWithCurlyBraces(parentAST);
118 }
119
120 /**
121 * Returns whether a token represents an ELSE.
122 *
123 * @param ast the token to check
124 * @return whether the token represents an ELSE
125 */
126 private static boolean isElse(DetailAST ast) {
127 return ast.getType() == TokenTypes.LITERAL_ELSE;
128 }
129
130 /**
131 * Returns whether a token represents an SLIST as part of an ELSE
132 * statement.
133 *
134 * @param ast the token to check
135 * @return whether the toke does represent an SLIST as part of an ELSE
136 */
137 private static boolean isElseWithCurlyBraces(DetailAST ast) {
138 return ast.getChildCount() == 2 && isElse(ast.getParent());
139 }
140 }