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 * <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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 074 * </p> 075 * <p> 076 * Violation Message Keys: 077 * </p> 078 * <ul> 079 * <li> 080 * {@code block.nested} 081 * </li> 082 * </ul> 083 * 084 * @since 3.1 085 */ 086@StatelessCheck 087public class AvoidNestedBlocksCheck extends AbstractCheck { 088 089 /** 090 * A key is pointing to the warning message text in "messages.properties" 091 * file. 092 */ 093 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 094 095 /** 096 * Allow nested blocks if they are the only child of a switch case. 097 */ 098 private boolean allowInSwitchCase; 099 100 @Override 101 public int[] getDefaultTokens() { 102 return getRequiredTokens(); 103 } 104 105 @Override 106 public int[] getAcceptableTokens() { 107 return getRequiredTokens(); 108 } 109 110 @Override 111 public int[] getRequiredTokens() { 112 return new int[] {TokenTypes.SLIST}; 113 } 114 115 @Override 116 public void visitToken(DetailAST ast) { 117 final DetailAST parent = ast.getParent(); 118 if (parent.getType() == TokenTypes.SLIST 119 && (!allowInSwitchCase || hasSiblings(ast))) { 120 log(ast, MSG_KEY_BLOCK_NESTED); 121 } 122 } 123 124 /** 125 * Checks whether the AST node has any siblings or not. 126 * 127 * @param ast node to examine 128 * @return {@code true} if the node has one or more siblings 129 */ 130 private static boolean hasSiblings(DetailAST ast) { 131 return ast.getPreviousSibling() != null || ast.getNextSibling() != null; 132 } 133 134 /** 135 * Setter to allow nested blocks if they are the only child of a switch case. 136 * 137 * @param allowInSwitchCase whether nested blocks are allowed 138 * if they are the only child of a switch case. 139 * @since 3.2 140 */ 141 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 142 this.allowInSwitchCase = allowInSwitchCase; 143 } 144 145}