001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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 {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
033 * empty for iterators.
034 * </p>
035 * <p> By default the check will check the following tokens:
036 *  {@link TokenTypes#COMMA COMMA},
037 *  {@link TokenTypes#SEMI SEMI},
038 *  {@link TokenTypes#TYPECAST TYPECAST},
039 *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
040 *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
041 *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
042 *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
043 *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
044 *  {@link TokenTypes#DO_WHILE DO_WHILE}.
045 * </p>
046 * <p>
047 * An example of how to configure the check is:
048 * </p>
049 * <pre>
050 * &lt;module name="WhitespaceAfter"/&gt;
051 * </pre>
052 * <p> An example of how to configure the check for whitespace only after
053 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
054 * </p>
055 * <pre>
056 * &lt;module name="WhitespaceAfter"&gt;
057 *     &lt;property name="tokens" value="COMMA, SEMI"/&gt;
058 * &lt;/module&gt;
059 * </pre>
060 */
061@StatelessCheck
062public class WhitespaceAfterCheck
063    extends AbstractCheck {
064
065    /**
066     * A key is pointing to the warning message text in "messages.properties"
067     * file.
068     */
069    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
070
071    /**
072     * A key is pointing to the warning message text in "messages.properties"
073     * file.
074     */
075    public static final String MSG_WS_TYPECAST = "ws.typeCast";
076
077    @Override
078    public int[] getDefaultTokens() {
079        return getAcceptableTokens();
080    }
081
082    @Override
083    public int[] getAcceptableTokens() {
084        return new int[] {
085            TokenTypes.COMMA,
086            TokenTypes.SEMI,
087            TokenTypes.TYPECAST,
088            TokenTypes.LITERAL_IF,
089            TokenTypes.LITERAL_ELSE,
090            TokenTypes.LITERAL_WHILE,
091            TokenTypes.LITERAL_DO,
092            TokenTypes.LITERAL_FOR,
093            TokenTypes.DO_WHILE,
094        };
095    }
096
097    @Override
098    public int[] getRequiredTokens() {
099        return CommonUtil.EMPTY_INT_ARRAY;
100    }
101
102    @Override
103    public void visitToken(DetailAST ast) {
104        if (ast.getType() == TokenTypes.TYPECAST) {
105            final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN);
106            final String line = getLine(targetAST.getLineNo() - 1);
107            if (!isFollowedByWhitespace(targetAST, line)) {
108                log(targetAST, MSG_WS_TYPECAST);
109            }
110        }
111        else {
112            final String line = getLine(ast.getLineNo() - 1);
113            if (!isFollowedByWhitespace(ast, line)) {
114                final Object[] message = {ast.getText()};
115                log(ast, MSG_WS_NOT_FOLLOWED, message);
116            }
117        }
118    }
119
120    /**
121     * Checks whether token is followed by a whitespace.
122     * @param targetAST Ast token.
123     * @param line The line associated with the ast token.
124     * @return true if ast token is followed by a whitespace.
125     */
126    private static boolean isFollowedByWhitespace(DetailAST targetAST, String line) {
127        final int after =
128            targetAST.getColumnNo() + targetAST.getText().length();
129        boolean followedByWhitespace = true;
130
131        if (after < line.length()) {
132            final char charAfter = line.charAt(after);
133            followedByWhitespace = charAfter == ';'
134                || charAfter == ')'
135                || Character.isWhitespace(charAfter);
136        }
137        return followedByWhitespace;
138    }
139
140}