| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EmptyBlockCheck |
|
| 4.0;4 |
| 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.DetailAST; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 23 | import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; | |
| 24 | ||
| 25 | /** | |
| 26 | * Checks for empty blocks. The policy to verify is specified using the {@link | |
| 27 | * BlockOption} class and defaults to {@link BlockOption#STMT}. | |
| 28 | * | |
| 29 | * <p> By default the check will check the following blocks: | |
| 30 | * {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE}, | |
| 31 | * {@link TokenTypes#LITERAL_TRY LITERAL_TRY}, | |
| 32 | * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH}, | |
| 33 | * {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY}, | |
| 34 | * {@link TokenTypes#LITERAL_DO LITERAL_DO}, | |
| 35 | * {@link TokenTypes#LITERAL_IF LITERAL_IF}, | |
| 36 | * {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE}, | |
| 37 | * {@link TokenTypes#LITERAL_FOR LITERAL_FOR}, | |
| 38 | * {@link TokenTypes#STATIC_INIT STATIC_INIT}. | |
| 39 | * </p> | |
| 40 | * | |
| 41 | * <p> An example of how to configure the check is: | |
| 42 | * </p> | |
| 43 | * <pre> | |
| 44 | * <module name="EmptyBlock"/> | |
| 45 | * </pre> | |
| 46 | * | |
| 47 | * <p> An example of how to configure the check for the {@link | |
| 48 | * BlockOption#TEXT} policy and only catch blocks is: | |
| 49 | * </p> | |
| 50 | * | |
| 51 | * <pre> | |
| 52 | * <module name="EmptyBlock"> | |
| 53 | * <property name="tokens" value="LITERAL_CATCH"/> | |
| 54 | * <property name="option" value="text"/> | |
| 55 | * </module> | |
| 56 | * </pre> | |
| 57 | * | |
| 58 | * @author Lars Kühne | |
| 59 | */ | |
| 60 | public class EmptyBlockCheck | |
| 61 | extends AbstractOptionCheck<BlockOption> | |
| 62 | { | |
| 63 | /** | |
| 64 | * Creates a new <code>EmptyBlockCheck</code> instance. | |
| 65 | */ | |
| 66 | public EmptyBlockCheck() | |
| 67 | { | |
| 68 | 3 | super(BlockOption.STMT, BlockOption.class); |
| 69 | 3 | } |
| 70 | ||
| 71 | @Override | |
| 72 | public int[] getDefaultTokens() | |
| 73 | { | |
| 74 | 3 | return new int[] { |
| 75 | TokenTypes.LITERAL_WHILE, | |
| 76 | TokenTypes.LITERAL_TRY, | |
| 77 | TokenTypes.LITERAL_CATCH, | |
| 78 | TokenTypes.LITERAL_FINALLY, | |
| 79 | TokenTypes.LITERAL_DO, | |
| 80 | TokenTypes.LITERAL_IF, | |
| 81 | TokenTypes.LITERAL_ELSE, | |
| 82 | TokenTypes.LITERAL_FOR, | |
| 83 | TokenTypes.INSTANCE_INIT, | |
| 84 | TokenTypes.STATIC_INIT, | |
| 85 | // TODO: need to handle.... | |
| 86 | //TokenTypes.LITERAL_SWITCH, | |
| 87 | //TODO: does this handle TokenTypes.LITERAL_SYNCHRONIZED? | |
| 88 | }; | |
| 89 | } | |
| 90 | ||
| 91 | @Override | |
| 92 | public void visitToken(DetailAST aAST) | |
| 93 | { | |
| 94 | 57 | final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST); |
| 95 | 57 | if (slistAST != null) { |
| 96 | 54 | if (getAbstractOption() == BlockOption.STMT) { |
| 97 | 36 | if (slistAST.getChildCount() <= 1) { |
| 98 | 20 | log(slistAST.getLineNo(), |
| 99 | slistAST.getColumnNo(), | |
| 100 | "block.noStmt", | |
| 101 | aAST.getText()); | |
| 102 | } | |
| 103 | } | |
| 104 | 18 | else if (getAbstractOption() == BlockOption.TEXT |
| 105 | && !hasText(slistAST)) | |
| 106 | { | |
| 107 | 6 | log(slistAST.getLineNo(), |
| 108 | slistAST.getColumnNo(), | |
| 109 | "block.empty", | |
| 110 | aAST.getText()); | |
| 111 | } | |
| 112 | } | |
| 113 | 57 | } |
| 114 | ||
| 115 | /** | |
| 116 | * @param aSlistAST a <code>DetailAST</code> value | |
| 117 | * @return whether the SLIST token contains any text. | |
| 118 | */ | |
| 119 | protected boolean hasText(final DetailAST aSlistAST) | |
| 120 | { | |
| 121 | 18 | boolean retVal = false; |
| 122 | ||
| 123 | 18 | final DetailAST rcurlyAST = aSlistAST.findFirstToken(TokenTypes.RCURLY); |
| 124 | 18 | if (rcurlyAST != null) { |
| 125 | 18 | final int slistLineNo = aSlistAST.getLineNo(); |
| 126 | 18 | final int slistColNo = aSlistAST.getColumnNo(); |
| 127 | 18 | final int rcurlyLineNo = rcurlyAST.getLineNo(); |
| 128 | 18 | final int rcurlyColNo = rcurlyAST.getColumnNo(); |
| 129 | 18 | final String[] lines = getLines(); |
| 130 | 18 | if (slistLineNo == rcurlyLineNo) { |
| 131 | // Handle braces on the same line | |
| 132 | 3 | final String txt = lines[slistLineNo - 1] |
| 133 | .substring(slistColNo + 1, rcurlyColNo); | |
| 134 | 3 | if (txt.trim().length() != 0) { |
| 135 | 1 | retVal = true; |
| 136 | } | |
| 137 | 3 | } |
| 138 | else { | |
| 139 | // check only whitespace of first & last lines | |
| 140 | 15 | if ((lines[slistLineNo - 1] |
| 141 | .substring(slistColNo + 1).trim().length() != 0) | |
| 142 | || (lines[rcurlyLineNo - 1] | |
| 143 | .substring(0, rcurlyColNo).trim().length() != 0)) | |
| 144 | { | |
| 145 | 0 | retVal = true; |
| 146 | } | |
| 147 | else { | |
| 148 | // check if all lines are also only whitespace | |
| 149 | 15 | for (int i = slistLineNo; i < (rcurlyLineNo - 1); i++) { |
| 150 | 11 | if (lines[i].trim().length() > 0) { |
| 151 | 11 | retVal = true; |
| 152 | 11 | break; |
| 153 | } | |
| 154 | } | |
| 155 | } | |
| 156 | } | |
| 157 | } | |
| 158 | 18 | return retVal; |
| 159 | } | |
| 160 | } |