001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.blocks; 020 021import com.puppycrawl.tools.checkstyle.api.Check; 022import com.puppycrawl.tools.checkstyle.api.TokenTypes; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024 025/** 026 * Finds nested blocks. 027 * 028 * <p> 029 * For example this Check flags confusing code like 030 * </p> 031 * <pre> 032 * public void guessTheOutput() 033 * { 034 * int whichIsWhich = 0; 035 * { 036 * int whichIsWhich = 2; 037 * } 038 * System.out.println("value = " + whichIsWhich); 039 * } 040 * </pre> 041 * 042 * and debugging / refactoring leftovers such as 043 * 044 * <pre> 045 * // if (someOldCondition) 046 * { 047 * System.out.println("unconditional"); 048 * } 049 * </pre> 050 * 051 * <p> 052 * A case in a switch statement does not implicitly form a block. 053 * Thus to be able to introduce local variables that have case scope 054 * it is necessary to open a nested block. This is supported, set 055 * the allowInSwitchCase property to true and include all statements 056 * of the case in the block. 057 * </p> 058 * 059 * <pre> 060 * switch (a) 061 * { 062 * case 0: 063 * // Never OK, break outside block 064 * { 065 * x = 1; 066 * } 067 * break; 068 * case 1: 069 * // Never OK, statement outside block 070 * System.out.println("Hello"); 071 * { 072 * x = 2; 073 * break; 074 * } 075 * case 1: 076 * // OK if allowInSwitchCase is true 077 * { 078 * System.out.println("Hello"); 079 * x = 2; 080 * break; 081 * } 082 * } 083 * </pre> 084 * 085 * @author lkuehne 086 */ 087public class AvoidNestedBlocksCheck extends Check 088{ 089 /** 090 * Whether nested blocks are allowed if they are the 091 * only child of a switch case. 092 */ 093 private boolean mAllowInSwitchCase; 094 095 @Override 096 public int[] getDefaultTokens() 097 { 098 return new int[] {TokenTypes.SLIST}; 099 } 100 101 @Override 102 public void visitToken(DetailAST aAST) 103 { 104 final DetailAST parent = aAST.getParent(); 105 if (parent.getType() == TokenTypes.SLIST) { 106 if (mAllowInSwitchCase 107 && (parent.getParent().getType() == TokenTypes.CASE_GROUP) 108 && (parent.getNumberOfChildren() == 1)) 109 { 110 return; 111 } 112 log(aAST.getLineNo(), aAST.getColumnNo(), "block.nested"); 113 } 114 } 115 116 /** 117 * Setter for allowInSwitchCase property. 118 * @param aAllowInSwitchCase whether nested blocks are allowed 119 * if they are the only child of a switch case. 120 */ 121 public void setAllowInSwitchCase(boolean aAllowInSwitchCase) 122 { 123 mAllowInSwitchCase = aAllowInSwitchCase; 124 } 125}