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.blocks;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
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 * Finds nested blocks (blocks that are used freely in the code).
30 * </div>
31 *
32 * <p>
33 * Rationale: Nested blocks are often leftovers from the
34 * debugging process, they confuse the reader.
35 * </p>
36 *
37 * <p>
38 * For example, this check finds the obsolete braces in
39 * </p>
40 * <pre>
41 * public void guessTheOutput()
42 * {
43 * int whichIsWhich = 0;
44 * {
45 * whichIsWhich = 2;
46 * }
47 * System.out.println("value = " + whichIsWhich);
48 * }
49 * </pre>
50 *
51 * <p>
52 * and debugging / refactoring leftovers such as
53 * </p>
54 * <pre>
55 * // if (conditionThatIsNotUsedAnyLonger)
56 * {
57 * System.out.println("unconditional");
58 * }
59 * </pre>
60 *
61 * <p>
62 * A case in a switch statement does not implicitly form a block.
63 * Thus, to be able to introduce local variables that have case scope
64 * it is necessary to open a nested block. This is supported, set
65 * the allowInSwitchCase property to true and include all statements
66 * of the case in the block.
67 * </p>
68 * <ul>
69 * <li>
70 * Property {@code allowInSwitchCase} - Allow nested blocks if they are the
71 * only child of a switch case.
72 * Type is {@code boolean}.
73 * Default value is {@code false}.
74 * </li>
75 * </ul>
76 *
77 * <p>
78 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
79 * </p>
80 *
81 * <p>
82 * Violation Message Keys:
83 * </p>
84 * <ul>
85 * <li>
86 * {@code block.nested}
87 * </li>
88 * </ul>
89 *
90 * @since 3.1
91 */
92 @StatelessCheck
93 public class AvoidNestedBlocksCheck extends AbstractCheck {
94
95 /**
96 * A key is pointing to the warning message text in "messages.properties"
97 * file.
98 */
99 public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
100
101 /**
102 * Allow nested blocks if they are the only child of a switch case.
103 */
104 private boolean allowInSwitchCase;
105
106 @Override
107 public int[] getDefaultTokens() {
108 return getRequiredTokens();
109 }
110
111 @Override
112 public int[] getAcceptableTokens() {
113 return getRequiredTokens();
114 }
115
116 @Override
117 public int[] getRequiredTokens() {
118 return new int[] {TokenTypes.SLIST};
119 }
120
121 @Override
122 public void visitToken(DetailAST ast) {
123 final DetailAST parent = ast.getParent();
124 if (parent.getType() == TokenTypes.SLIST
125 && (!allowInSwitchCase || hasSiblings(ast))) {
126 log(ast, MSG_KEY_BLOCK_NESTED);
127 }
128 }
129
130 /**
131 * Checks whether the AST node has any siblings or not.
132 *
133 * @param ast node to examine
134 * @return {@code true} if the node has one or more siblings
135 */
136 private static boolean hasSiblings(DetailAST ast) {
137 return ast.getPreviousSibling() != null || ast.getNextSibling() != null;
138 }
139
140 /**
141 * Setter to allow nested blocks if they are the only child of a switch case.
142 *
143 * @param allowInSwitchCase whether nested blocks are allowed
144 * if they are the only child of a switch case.
145 * @since 3.2
146 */
147 public void setAllowInSwitchCase(boolean allowInSwitchCase) {
148 this.allowInSwitchCase = allowInSwitchCase;
149 }
150
151 }