001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.whitespace; 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; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 027 028/** 029 * <p> 030 * Checks that a token is followed by whitespace, with the exception that it 031 * does not check for whitespace after the semicolon of an empty for iterator. 032 * Use Check <a href="https://checkstyle.org/config_whitespace.html#EmptyForIteratorPad"> 033 * EmptyForIteratorPad</a> to validate empty for iterators. 034 * </p> 035 * <ul> 036 * <li> 037 * Property {@code tokens} - tokens to check 038 * Type is {@code java.lang.String[]}. 039 * Validation type is {@code tokenSet}. 040 * Default value is: 041 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMMA"> 042 * COMMA</a>, 043 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#SEMI"> 044 * SEMI</a>, 045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPECAST"> 046 * TYPECAST</a>, 047 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF"> 048 * LITERAL_IF</a>, 049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE"> 050 * LITERAL_ELSE</a>, 051 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE"> 052 * LITERAL_WHILE</a>, 053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO"> 054 * LITERAL_DO</a>, 055 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR"> 056 * LITERAL_FOR</a>, 057 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO"> 058 * DO_WHILE</a>, 059 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ELLIPSIS"> 060 * ELLIPSIS</a>, 061 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH"> 062 * LITERAL_SWITCH</a>, 063 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA"> 064 * LAMBDA</a>. 065 * </li> 066 * </ul> 067 * <p> 068 * To configure the check: 069 * </p> 070 * <pre> 071 * <module name="WhitespaceAfter"/> 072 * </pre> 073 * <p>Example:</p> 074 * <pre> 075 * public void myTest() { 076 * if (foo) { // OK 077 * //... 078 * } else if(bar) { // violation 079 * //... 080 * } 081 * 082 * testMethod(foo, bar); // OK 083 * testMethod(foo,bar); // violation 084 * 085 * for (;;){} // OK 086 * for(;;){} // violation, space after 'for' is required 087 * } 088 * </pre> 089 * <p> 090 * To configure the check for whitespace only after COMMA and SEMI tokens: 091 * </p> 092 * <pre> 093 * <module name="WhitespaceAfter"> 094 * <property name="tokens" value="COMMA, SEMI"/> 095 * </module> 096 * </pre> 097 * <p>Example:</p> 098 * <pre> 099 * public void myTest() { 100 * int a; int b; // OK 101 * int a;int b; // violation 102 * 103 * testMethod(foo, bar); // OK 104 * testMethod(foo,bar); // violation 105 * 106 * for(;;) {} // OK 107 * } 108 * </pre> 109 * <p> 110 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 111 * </p> 112 * <p> 113 * Violation Message Keys: 114 * </p> 115 * <ul> 116 * <li> 117 * {@code ws.notFollowed} 118 * </li> 119 * <li> 120 * {@code ws.typeCast} 121 * </li> 122 * </ul> 123 * 124 * @since 3.0 125 */ 126@StatelessCheck 127public class WhitespaceAfterCheck 128 extends AbstractCheck { 129 130 /** 131 * A key is pointing to the warning message text in "messages.properties" 132 * file. 133 */ 134 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 135 136 /** 137 * A key is pointing to the warning message text in "messages.properties" 138 * file. 139 */ 140 public static final String MSG_WS_TYPECAST = "ws.typeCast"; 141 142 @Override 143 public int[] getDefaultTokens() { 144 return getAcceptableTokens(); 145 } 146 147 @Override 148 public int[] getAcceptableTokens() { 149 return new int[] { 150 TokenTypes.COMMA, 151 TokenTypes.SEMI, 152 TokenTypes.TYPECAST, 153 TokenTypes.LITERAL_IF, 154 TokenTypes.LITERAL_ELSE, 155 TokenTypes.LITERAL_WHILE, 156 TokenTypes.LITERAL_DO, 157 TokenTypes.LITERAL_FOR, 158 TokenTypes.DO_WHILE, 159 TokenTypes.ELLIPSIS, 160 TokenTypes.LITERAL_SWITCH, 161 TokenTypes.LAMBDA, 162 }; 163 } 164 165 @Override 166 public int[] getRequiredTokens() { 167 return CommonUtil.EMPTY_INT_ARRAY; 168 } 169 170 @Override 171 public void visitToken(DetailAST ast) { 172 if (ast.getType() == TokenTypes.TYPECAST) { 173 final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN); 174 final int[] line = getLineCodePoints(targetAST.getLineNo() - 1); 175 if (!isFollowedByWhitespace(targetAST, line)) { 176 log(targetAST, MSG_WS_TYPECAST); 177 } 178 } 179 else { 180 final int[] line = getLineCodePoints(ast.getLineNo() - 1); 181 if (!isFollowedByWhitespace(ast, line)) { 182 final Object[] message = {ast.getText()}; 183 log(ast, MSG_WS_NOT_FOLLOWED, message); 184 } 185 } 186 } 187 188 /** 189 * Checks whether token is followed by a whitespace. 190 * 191 * @param targetAST Ast token. 192 * @param line Unicode code points array of line associated with the ast token. 193 * @return true if ast token is followed by a whitespace. 194 */ 195 private static boolean isFollowedByWhitespace(DetailAST targetAST, int... line) { 196 final int after = 197 targetAST.getColumnNo() + targetAST.getText().length(); 198 boolean followedByWhitespace = true; 199 200 if (after < line.length) { 201 final int codePoint = line[after]; 202 203 followedByWhitespace = codePoint == ';' 204 || codePoint == ')' 205 || Character.isWhitespace(codePoint); 206 } 207 return followedByWhitespace; 208 } 209 210}