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