001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.blocks; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 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 * Finds nested blocks (blocks that are used freely in the code). 030 * </p> 031 * <p> 032 * Rationale: Nested blocks are often leftovers from the 033 * debugging process, they confuse the reader. 034 * </p> 035 * <p> 036 * For example, this check finds the obsolete braces in 037 * </p> 038 * <pre> 039 * public void guessTheOutput() 040 * { 041 * int whichIsWhich = 0; 042 * { 043 * whichIsWhich = 2; 044 * } 045 * System.out.println("value = " + whichIsWhich); 046 * } 047 * </pre> 048 * <p> 049 * and debugging / refactoring leftovers such as 050 * </p> 051 * <pre> 052 * // if (conditionThatIsNotUsedAnyLonger) 053 * { 054 * System.out.println("unconditional"); 055 * } 056 * </pre> 057 * <p> 058 * A case in a switch statement does not implicitly form a block. 059 * Thus to be able to introduce local variables that have case scope 060 * it is necessary to open a nested block. This is supported, set 061 * the allowInSwitchCase property to true and include all statements 062 * of the case in the block. 063 * </p> 064 * <ul> 065 * <li> 066 * Property {@code allowInSwitchCase} - Allow nested blocks if they are the 067 * only child of a switch case. 068 * Type is {@code boolean}. 069 * Default value is {@code false}. 070 * </li> 071 * </ul> 072 * <p> 073 * To configure the check: 074 * </p> 075 * <pre> 076 * <module name="AvoidNestedBlocks"/> 077 * </pre> 078 * <p> 079 * Example: 080 * </p> 081 * <pre> 082 * public void foo() { 083 * int myInteger = 0; 084 * { // violation 085 * myInteger = 2; 086 * } 087 * System.out.println("myInteger = " + myInteger); 088 * 089 * switch (a) { 090 * case 1: 091 * { // violation 092 * System.out.println("Case 1"); 093 * break; 094 * } 095 * case 2: 096 * System.out.println("Case 2"); // OK 097 * break; 098 * } 099 * } 100 * </pre> 101 * <p> 102 * To configure the check to allow nested blocks in switch case: 103 * </p> 104 * <pre> 105 * <module name="AvoidNestedBlocks"> 106 * <property name="allowInSwitchCase" value="true"/> 107 * </module> 108 * </pre> 109 * <p> 110 * Example: 111 * </p> 112 * <pre> 113 * public void foo() { 114 * int myInteger = 0; 115 * { // violation 116 * myInteger = 2; 117 * } 118 * System.out.println("myInteger = " + myInteger); 119 * 120 * switch (a) 121 * { 122 * case 1: 123 * { // OK 124 * System.out.println("Case 1"); 125 * break; 126 * } 127 * case 2: 128 * System.out.println("Case 2"); // OK 129 * break; 130 * } 131 * } 132 * </pre> 133 * <p> 134 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 135 * </p> 136 * <p> 137 * Violation Message Keys: 138 * </p> 139 * <ul> 140 * <li> 141 * {@code block.nested} 142 * </li> 143 * </ul> 144 * 145 * @since 3.1 146 */ 147@StatelessCheck 148public class AvoidNestedBlocksCheck extends AbstractCheck { 149 150 /** 151 * A key is pointing to the warning message text in "messages.properties" 152 * file. 153 */ 154 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 155 156 /** 157 * Allow nested blocks if they are the only child of a switch case. 158 */ 159 private boolean allowInSwitchCase; 160 161 @Override 162 public int[] getDefaultTokens() { 163 return getRequiredTokens(); 164 } 165 166 @Override 167 public int[] getAcceptableTokens() { 168 return getRequiredTokens(); 169 } 170 171 @Override 172 public int[] getRequiredTokens() { 173 return new int[] {TokenTypes.SLIST}; 174 } 175 176 @Override 177 public void visitToken(DetailAST ast) { 178 final DetailAST parent = ast.getParent(); 179 if (parent.getType() == TokenTypes.SLIST 180 && (!allowInSwitchCase || hasSiblings(ast))) { 181 log(ast, MSG_KEY_BLOCK_NESTED); 182 } 183 } 184 185 /** 186 * Checks whether the AST node has any siblings or not. 187 * 188 * @param ast node to examine 189 * @return {@code true} if the node has one or more siblings 190 */ 191 private static boolean hasSiblings(DetailAST ast) { 192 return ast.getPreviousSibling() != null || ast.getNextSibling() != null; 193 } 194 195 /** 196 * Setter to allow nested blocks if they are the only child of a switch case. 197 * 198 * @param allowInSwitchCase whether nested blocks are allowed 199 * if they are the only child of a switch case. 200 */ 201 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 202 this.allowInSwitchCase = allowInSwitchCase; 203 } 204 205}