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