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