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> 066 * To configure the check for whitespace only after COMMA and SEMI tokens: 067 * </p> 068 * <pre> 069 * <module name="WhitespaceAfter"> 070 * <property name="tokens" value="COMMA, SEMI"/> 071 * </module> 072 * </pre> 073 * 074 * @since 3.0 075 */ 076@StatelessCheck 077public class WhitespaceAfterCheck 078 extends AbstractCheck { 079 080 /** 081 * A key is pointing to the warning message text in "messages.properties" 082 * file. 083 */ 084 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 085 086 /** 087 * A key is pointing to the warning message text in "messages.properties" 088 * file. 089 */ 090 public static final String MSG_WS_TYPECAST = "ws.typeCast"; 091 092 @Override 093 public int[] getDefaultTokens() { 094 return getAcceptableTokens(); 095 } 096 097 @Override 098 public int[] getAcceptableTokens() { 099 return new int[] { 100 TokenTypes.COMMA, 101 TokenTypes.SEMI, 102 TokenTypes.TYPECAST, 103 TokenTypes.LITERAL_IF, 104 TokenTypes.LITERAL_ELSE, 105 TokenTypes.LITERAL_WHILE, 106 TokenTypes.LITERAL_DO, 107 TokenTypes.LITERAL_FOR, 108 TokenTypes.DO_WHILE, 109 }; 110 } 111 112 @Override 113 public int[] getRequiredTokens() { 114 return CommonUtil.EMPTY_INT_ARRAY; 115 } 116 117 @Override 118 public void visitToken(DetailAST ast) { 119 if (ast.getType() == TokenTypes.TYPECAST) { 120 final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN); 121 final String line = getLine(targetAST.getLineNo() - 1); 122 if (!isFollowedByWhitespace(targetAST, line)) { 123 log(targetAST, MSG_WS_TYPECAST); 124 } 125 } 126 else { 127 final String line = getLine(ast.getLineNo() - 1); 128 if (!isFollowedByWhitespace(ast, line)) { 129 final Object[] message = {ast.getText()}; 130 log(ast, MSG_WS_NOT_FOLLOWED, message); 131 } 132 } 133 } 134 135 /** 136 * Checks whether token is followed by a whitespace. 137 * @param targetAST Ast token. 138 * @param line The line associated with the ast token. 139 * @return true if ast token is followed by a whitespace. 140 */ 141 private static boolean isFollowedByWhitespace(DetailAST targetAST, String line) { 142 final int after = 143 targetAST.getColumnNo() + targetAST.getText().length(); 144 boolean followedByWhitespace = true; 145 146 if (after < line.length()) { 147 final char charAfter = line.charAt(after); 148 followedByWhitespace = charAfter == ';' 149 || charAfter == ')' 150 || Character.isWhitespace(charAfter); 151 } 152 return followedByWhitespace; 153 } 154 155}