001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <div> 029 * Finds nested blocks (blocks that are used freely in the code). 030 * </div> 031 * 032 * <p> 033 * Rationale: Nested blocks are often leftovers from the 034 * debugging process, they confuse the reader. 035 * </p> 036 * 037 * <p> 038 * For example, this check finds the obsolete braces in 039 * </p> 040 * <pre> 041 * public void guessTheOutput() 042 * { 043 * int whichIsWhich = 0; 044 * { 045 * whichIsWhich = 2; 046 * } 047 * System.out.println("value = " + whichIsWhich); 048 * } 049 * </pre> 050 * 051 * <p> 052 * and debugging / refactoring leftovers such as 053 * </p> 054 * <pre> 055 * // if (conditionThatIsNotUsedAnyLonger) 056 * { 057 * System.out.println("unconditional"); 058 * } 059 * </pre> 060 * 061 * <p> 062 * A case in a switch statement does not implicitly form a block. 063 * Thus, to be able to introduce local variables that have case scope 064 * it is necessary to open a nested block. This is supported, set 065 * the allowInSwitchCase property to true and include all statements 066 * of the case in the block. 067 * </p> 068 * <ul> 069 * <li> 070 * Property {@code allowInSwitchCase} - Allow nested blocks if they are the 071 * only child of a switch case. 072 * Type is {@code boolean}. 073 * Default value is {@code false}. 074 * </li> 075 * </ul> 076 * 077 * <p> 078 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 079 * </p> 080 * 081 * <p> 082 * Violation Message Keys: 083 * </p> 084 * <ul> 085 * <li> 086 * {@code block.nested} 087 * </li> 088 * </ul> 089 * 090 * @since 3.1 091 */ 092@StatelessCheck 093public class AvoidNestedBlocksCheck extends AbstractCheck { 094 095 /** 096 * A key is pointing to the warning message text in "messages.properties" 097 * file. 098 */ 099 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}