001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.api.AbstractCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
026
027/**
028 * <p>
029 * Checks that a token is followed by whitespace, with the exception that it
030 * does not check for whitespace after the semicolon of an empty for iterator.
031 * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
032 * empty for iterators.
033 * </p>
034 * <p> By default the check will check the following tokens:
035 *  {@link TokenTypes#COMMA COMMA},
036 *  {@link TokenTypes#SEMI SEMI},
037 *  {@link TokenTypes#TYPECAST TYPECAST},
038 *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
039 *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
040 *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
041 *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
042 *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
043 *  {@link TokenTypes#DO_WHILE DO_WHILE}.
044 * </p>
045 * <p>
046 * An example of how to configure the check is:
047 * </p>
048 * <pre>
049 * &lt;module name="WhitespaceAfter"/&gt;
050 * </pre>
051 * <p> An example of how to configure the check for whitespace only after
052 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
053 * </p>
054 * <pre>
055 * &lt;module name="WhitespaceAfter"&gt;
056 *     &lt;property name="tokens" value="COMMA, SEMI"/&gt;
057 * &lt;/module&gt;
058 * </pre>
059 * @author Oliver Burn
060 * @author Rick Giles
061 */
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 CommonUtils.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.getLineNo(),
109                    targetAST.getColumnNo() + targetAST.getText().length(),
110                    MSG_WS_TYPECAST);
111            }
112        }
113        else {
114            final String line = getLine(ast.getLineNo() - 1);
115            if (!isFollowedByWhitespace(ast, line)) {
116                final Object[] message = {ast.getText()};
117                log(ast.getLineNo(),
118                    ast.getColumnNo() + ast.getText().length(),
119                    MSG_WS_NOT_FOLLOWED,
120                    message);
121            }
122        }
123    }
124
125    /**
126     * Checks whether token is followed by a whitespace.
127     * @param targetAST Ast token.
128     * @param line The line associated with the ast token.
129     * @return true if ast token is followed by a whitespace.
130     */
131    private static boolean isFollowedByWhitespace(DetailAST targetAST, String line) {
132        final int after =
133            targetAST.getColumnNo() + targetAST.getText().length();
134        boolean followedByWhitespace = true;
135
136        if (after < line.length()) {
137            final char charAfter = line.charAt(after);
138            followedByWhitespace = charAfter == ';'
139                || charAfter == ')'
140                || Character.isWhitespace(charAfter);
141        }
142        return followedByWhitespace;
143    }
144}