001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * {@snippet : 041 * public void guessTheOutput() 042 * { 043 * int whichIsWhich = 0; 044 * { 045 * whichIsWhich = 2; 046 * } 047 * System.out.println("value = " + whichIsWhich); 048 * } 049 * } 050 * 051 * <p> 052 * and debugging / refactoring leftovers such as 053 * </p> 054 * {@snippet : 055 * // if (conditionThatIsNotUsedAnyLonger) 056 * { 057 * System.out.println("unconditional"); 058 * } 059 * } 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 of the case in the block. 066 * </p> 067 * 068 * @since 3.1 069 */ 070@StatelessCheck 071public class AvoidNestedBlocksCheck extends AbstractCheck { 072 073 /** 074 * A key is pointing to the warning message text in "messages.properties" file. 075 */ 076 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 077 078 /** 079 * Allow nested blocks if they are the only child of a switch case. 080 */ 081 private boolean allowInSwitchCase; 082 083 /** 084 * Creates a new {@code AvoidNestedBlocksCheck} instance. 085 */ 086 public AvoidNestedBlocksCheck() { 087 // no code by default 088 } 089 090 @Override 091 public int[] getDefaultTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getAcceptableTokens() { 097 return getRequiredTokens(); 098 } 099 100 @Override 101 public int[] getRequiredTokens() { 102 return new int[] {TokenTypes.SLIST}; 103 } 104 105 @Override 106 public void visitToken(DetailAST ast) { 107 final DetailAST parent = ast.getParent(); 108 if (parent.getType() == TokenTypes.SLIST 109 && (!allowInSwitchCase || hasSiblings(ast))) { 110 log(ast, MSG_KEY_BLOCK_NESTED); 111 } 112 } 113 114 /** 115 * Checks whether the AST node has any siblings or not. 116 * 117 * @param ast node to examine 118 * @return {@code true} if the node has one or more siblings 119 */ 120 private static boolean hasSiblings(DetailAST ast) { 121 return ast.getPreviousSibling() != null || ast.getNextSibling() != null; 122 } 123 124 /** 125 * Setter to allow nested blocks if they are the only child of a switch case. 126 * 127 * @param allowInSwitchCase whether nested blocks are allowed 128 * if they are the only child of a switch case. 129 * @since 3.2 130 */ 131 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 132 this.allowInSwitchCase = allowInSwitchCase; 133 } 134 135}