| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NoWhitespaceAfterCheck |
|
| 2.75;2.75 |
| 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 | ||
| 20 | package com.puppycrawl.tools.checkstyle.checks.whitespace; | |
| 21 | ||
| 22 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 25 | ||
| 26 | /** | |
| 27 | * <p> | |
| 28 | * Checks that there is no whitespace after a token. | |
| 29 | * More specifically, it checks that it is not followed by whitespace, | |
| 30 | * or (if linebreaks are allowed) all characters on the line after are | |
| 31 | * whitespace. To forbid linebreaks afer a token, set property | |
| 32 | * allowLineBreaks to false. | |
| 33 | * </p> | |
| 34 | * <p> By default the check will check the following operators: | |
| 35 | * {@link TokenTypes#ARRAY_INIT ARRAY_INIT}, | |
| 36 | * {@link TokenTypes#BNOT BNOT}, | |
| 37 | * {@link TokenTypes#DEC DEC}, | |
| 38 | * {@link TokenTypes#DOT DOT}, | |
| 39 | * {@link TokenTypes#INC INC}, | |
| 40 | * {@link TokenTypes#LNOT LNOT}, | |
| 41 | * {@link TokenTypes#UNARY_MINUS UNARY_MINUS}, | |
| 42 | * {@link TokenTypes#UNARY_PLUS UNARY_PLUS}. It also supports the operator | |
| 43 | * {@link TokenTypes#TYPECAST TYPECAST}. | |
| 44 | * </p> | |
| 45 | * <p> | |
| 46 | * An example of how to configure the check is: | |
| 47 | * </p> | |
| 48 | * <pre> | |
| 49 | * <module name="NoWhitespaceAfter"/> | |
| 50 | * </pre> | |
| 51 | * <p> An example of how to configure the check to forbid linebreaks after | |
| 52 | * a {@link TokenTypes#DOT DOT} token is: | |
| 53 | * </p> | |
| 54 | * <pre> | |
| 55 | * <module name="NoWhitespaceAfter"> | |
| 56 | * <property name="tokens" value="DOT"/> | |
| 57 | * <property name="allowLineBreaks" value="false"/> | |
| 58 | * </module> | |
| 59 | * </pre> | |
| 60 | * @author Rick Giles | |
| 61 | * @author lkuehne | |
| 62 | * @version 1.0 | |
| 63 | */ | |
| 64 | 3 | public class NoWhitespaceAfterCheck extends Check |
| 65 | { | |
| 66 | /** Whether whitespace is allowed if the AST is at a linebreak */ | |
| 67 | 3 | private boolean mAllowLineBreaks = true; |
| 68 | ||
| 69 | @Override | |
| 70 | public int[] getDefaultTokens() | |
| 71 | { | |
| 72 | 1 | return new int[] { |
| 73 | TokenTypes.ARRAY_INIT, | |
| 74 | TokenTypes.INC, | |
| 75 | TokenTypes.DEC, | |
| 76 | TokenTypes.UNARY_MINUS, | |
| 77 | TokenTypes.UNARY_PLUS, | |
| 78 | TokenTypes.BNOT, | |
| 79 | TokenTypes.LNOT, | |
| 80 | TokenTypes.DOT, | |
| 81 | }; | |
| 82 | } | |
| 83 | ||
| 84 | @Override | |
| 85 | public int[] getAcceptableTokens() | |
| 86 | { | |
| 87 | 2 | return new int[] { |
| 88 | TokenTypes.ARRAY_INIT, | |
| 89 | TokenTypes.INC, | |
| 90 | TokenTypes.DEC, | |
| 91 | TokenTypes.UNARY_MINUS, | |
| 92 | TokenTypes.UNARY_PLUS, | |
| 93 | TokenTypes.BNOT, | |
| 94 | TokenTypes.LNOT, | |
| 95 | TokenTypes.DOT, | |
| 96 | TokenTypes.TYPECAST, | |
| 97 | }; | |
| 98 | } | |
| 99 | ||
| 100 | @Override | |
| 101 | public void visitToken(DetailAST aAST) | |
| 102 | { | |
| 103 | 37 | DetailAST targetAST = aAST; |
| 104 | 37 | if (targetAST.getType() == TokenTypes.TYPECAST) { |
| 105 | 5 | targetAST = targetAST.findFirstToken(TokenTypes.RPAREN); |
| 106 | } | |
| 107 | 37 | final String line = getLines()[aAST.getLineNo() - 1]; |
| 108 | 37 | final int after = |
| 109 | targetAST.getColumnNo() + targetAST.getText().length(); | |
| 110 | ||
| 111 | 37 | if ((after >= line.length()) |
| 112 | || Character.isWhitespace(line.charAt(after))) | |
| 113 | { | |
| 114 | 20 | boolean flag = !mAllowLineBreaks; |
| 115 | 27 | for (int i = after + 1; !flag && (i < line.length()); i++) { |
| 116 | 7 | if (!Character.isWhitespace(line.charAt(i))) { |
| 117 | 6 | flag = true; |
| 118 | } | |
| 119 | } | |
| 120 | 20 | if (flag) { |
| 121 | 17 | log(targetAST.getLineNo(), after, |
| 122 | "ws.followed", targetAST.getText()); | |
| 123 | } | |
| 124 | } | |
| 125 | 37 | } |
| 126 | ||
| 127 | /** | |
| 128 | * Control whether whitespace is flagged at linebreaks. | |
| 129 | * @param aAllowLineBreaks whether whitespace should be | |
| 130 | * flagged at linebreaks. | |
| 131 | */ | |
| 132 | public void setAllowLineBreaks(boolean aAllowLineBreaks) | |
| 133 | { | |
| 134 | 1 | mAllowLineBreaks = aAllowLineBreaks; |
| 135 | 1 | } |
| 136 | } |