001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * <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 * Type is {@code boolean}. 094 * Default value is {@code false}. 095 * </li> 096 * </ul> 097 * <p> 098 * To configure the check: 099 * </p> 100 * <pre> 101 * <module name="AvoidNestedBlocks"/> 102 * </pre> 103 * <p> 104 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 105 * </p> 106 * <p> 107 * Violation Message Keys: 108 * </p> 109 * <ul> 110 * <li> 111 * {@code block.nested} 112 * </li> 113 * </ul> 114 * 115 * @since 3.1 116 */ 117@StatelessCheck 118public class AvoidNestedBlocksCheck extends AbstractCheck { 119 120 /** 121 * A key is pointing to the warning message text in "messages.properties" 122 * file. 123 */ 124 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 125 126 /** 127 * Allow nested blocks if they are the only child of a switch case. 128 */ 129 private boolean allowInSwitchCase; 130 131 @Override 132 public int[] getDefaultTokens() { 133 return getRequiredTokens(); 134 } 135 136 @Override 137 public int[] getAcceptableTokens() { 138 return getRequiredTokens(); 139 } 140 141 @Override 142 public int[] getRequiredTokens() { 143 return new int[] {TokenTypes.SLIST}; 144 } 145 146 @Override 147 public void visitToken(DetailAST ast) { 148 final DetailAST parent = ast.getParent(); 149 if (parent.getType() == TokenTypes.SLIST 150 && (!allowInSwitchCase || hasSiblings(ast))) { 151 log(ast, MSG_KEY_BLOCK_NESTED); 152 } 153 } 154 155 /** 156 * Checks whether the AST node has any siblings or not. 157 * 158 * @param ast node to examine 159 * @return {@code true} if the node has one or more siblings 160 */ 161 private static boolean hasSiblings(DetailAST ast) { 162 return ast.getPreviousSibling() != null || ast.getNextSibling() != null; 163 } 164 165 /** 166 * Setter to allow nested blocks if they are the only child of a switch case. 167 * 168 * @param allowInSwitchCase whether nested blocks are allowed 169 * if they are the only child of a switch case. 170 */ 171 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 172 this.allowInSwitchCase = allowInSwitchCase; 173 } 174 175}