001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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, i.e. 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 * <pre> 065 * switch (a) 066 * { 067 * case 0: 068 * // Never OK, break outside block 069 * { 070 * x = 1; 071 * } 072 * break; 073 * case 1: 074 * // Never OK, statement outside block 075 * System.out.println("Hello"); 076 * { 077 * x = 2; 078 * break; 079 * } 080 * case 2: 081 * // OK if allowInSwitchCase is true 082 * { 083 * System.out.println("Hello"); 084 * x = 3; 085 * break; 086 * } 087 * } 088 * </pre> 089 * <ul> 090 * <li> 091 * Property {@code allowInSwitchCase} - Allow nested blocks if they are the 092 * only child of a switch case. 093 * Default value is {@code false}. 094 * </li> 095 * </ul> 096 * <p> 097 * To configure the check: 098 * </p> 099 * <pre> 100 * <module name="AvoidNestedBlocks"/> 101 * </pre> 102 * 103 * @since 3.1 104 */ 105@StatelessCheck 106public class AvoidNestedBlocksCheck extends AbstractCheck { 107 108 /** 109 * A key is pointing to the warning message text in "messages.properties" 110 * file. 111 */ 112 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 113 114 /** 115 * Allow nested blocks if they are the only child of a switch case. 116 */ 117 private boolean allowInSwitchCase; 118 119 @Override 120 public int[] getDefaultTokens() { 121 return getRequiredTokens(); 122 } 123 124 @Override 125 public int[] getAcceptableTokens() { 126 return getRequiredTokens(); 127 } 128 129 @Override 130 public int[] getRequiredTokens() { 131 return new int[] {TokenTypes.SLIST}; 132 } 133 134 @Override 135 public void visitToken(DetailAST ast) { 136 final DetailAST parent = ast.getParent(); 137 if (parent.getType() == TokenTypes.SLIST 138 && (!allowInSwitchCase 139 || parent.getNumberOfChildren() != 1)) { 140 log(ast, MSG_KEY_BLOCK_NESTED); 141 } 142 } 143 144 /** 145 * Setter to allow nested blocks if they are the only child of a switch case. 146 * @param allowInSwitchCase whether nested blocks are allowed 147 * if they are the only child of a switch case. 148 */ 149 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 150 this.allowInSwitchCase = allowInSwitchCase; 151 } 152 153}