| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AvoidNestedBlocksCheck |
|
| 2.6666666666666665;2.667 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.blocks; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | ||
| 25 | /** | |
| 26 | * Finds nested blocks. | |
| 27 | * | |
| 28 | * <p> | |
| 29 | * For example this Check flags confusing code like | |
| 30 | * </p> | |
| 31 | * <pre> | |
| 32 | * public void guessTheOutput() | |
| 33 | * { | |
| 34 | * int whichIsWhich = 0; | |
| 35 | * { | |
| 36 | * int whichIsWhich = 2; | |
| 37 | * } | |
| 38 | * System.out.println("value = " + whichIsWhich); | |
| 39 | * } | |
| 40 | * </pre> | |
| 41 | * | |
| 42 | * and debugging / refactoring leftovers such as | |
| 43 | * | |
| 44 | * <pre> | |
| 45 | * // if (someOldCondition) | |
| 46 | * { | |
| 47 | * System.out.println("unconditional"); | |
| 48 | * } | |
| 49 | * </pre> | |
| 50 | * | |
| 51 | * <p> | |
| 52 | * A case in a switch statement does not implicitly form a block. | |
| 53 | * Thus to be able to introduce local variables that have case scope | |
| 54 | * it is necessary to open a nested block. This is supported, set | |
| 55 | * the allowInSwitchCase property to true and include all statements | |
| 56 | * of the case in the block. | |
| 57 | * </p> | |
| 58 | * | |
| 59 | * <pre> | |
| 60 | * switch (a) | |
| 61 | * { | |
| 62 | * case 0: | |
| 63 | * // Never OK, break outside block | |
| 64 | * { | |
| 65 | * x = 1; | |
| 66 | * } | |
| 67 | * break; | |
| 68 | * case 1: | |
| 69 | * // Never OK, statement outside block | |
| 70 | * System.out.println("Hello"); | |
| 71 | * { | |
| 72 | * x = 2; | |
| 73 | * break; | |
| 74 | * } | |
| 75 | * case 1: | |
| 76 | * // OK if allowInSwitchCase is true | |
| 77 | * { | |
| 78 | * System.out.println("Hello"); | |
| 79 | * x = 2; | |
| 80 | * break; | |
| 81 | * } | |
| 82 | * } | |
| 83 | * </pre> | |
| 84 | * | |
| 85 | * @author lkuehne | |
| 86 | */ | |
| 87 | 2 | public class AvoidNestedBlocksCheck extends Check |
| 88 | { | |
| 89 | /** | |
| 90 | * Whether nested blocks are allowed if they are the | |
| 91 | * only child of a switch case. | |
| 92 | */ | |
| 93 | private boolean mAllowInSwitchCase; | |
| 94 | ||
| 95 | @Override | |
| 96 | public int[] getDefaultTokens() | |
| 97 | { | |
| 98 | 2 | return new int[] {TokenTypes.SLIST}; |
| 99 | } | |
| 100 | ||
| 101 | @Override | |
| 102 | public void visitToken(DetailAST aAST) | |
| 103 | { | |
| 104 | 22 | final DetailAST parent = aAST.getParent(); |
| 105 | 22 | if (parent.getType() == TokenTypes.SLIST) { |
| 106 | 8 | if (mAllowInSwitchCase |
| 107 | && (parent.getParent().getType() == TokenTypes.CASE_GROUP) | |
| 108 | && (parent.getNumberOfChildren() == 1)) | |
| 109 | { | |
| 110 | 1 | return; |
| 111 | } | |
| 112 | 7 | log(aAST.getLineNo(), aAST.getColumnNo(), "block.nested"); |
| 113 | } | |
| 114 | 21 | } |
| 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 | 1 | mAllowInSwitchCase = aAllowInSwitchCase; |
| 124 | 1 | } |
| 125 | } |