001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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 * <div>
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
033 * <a href="https://checkstyle.org/checks/whitespace/emptyforiteratorpad.html">
034 * EmptyForIteratorPad</a> to validate empty for iterators.
035 * </div>
036 *
037 * @since 3.0
038 */
039@StatelessCheck
040public class WhitespaceAfterCheck
041    extends AbstractCheck {
042
043    /**
044     * A key is pointing to the warning message text in "messages.properties"
045     * file.
046     */
047    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
048
049    /**
050     * A key is pointing to the warning message text in "messages.properties"
051     * file.
052     */
053    public static final String MSG_WS_TYPECAST = "ws.typeCast";
054
055    @Override
056    public int[] getDefaultTokens() {
057        return getAcceptableTokens();
058    }
059
060    @Override
061    public int[] getAcceptableTokens() {
062        return new int[] {
063            TokenTypes.COMMA,
064            TokenTypes.SEMI,
065            TokenTypes.TYPECAST,
066            TokenTypes.LITERAL_IF,
067            TokenTypes.LITERAL_ELSE,
068            TokenTypes.LITERAL_WHILE,
069            TokenTypes.LITERAL_DO,
070            TokenTypes.LITERAL_FOR,
071            TokenTypes.LITERAL_FINALLY,
072            TokenTypes.LITERAL_RETURN,
073            TokenTypes.LITERAL_YIELD,
074            TokenTypes.LITERAL_CATCH,
075            TokenTypes.DO_WHILE,
076            TokenTypes.ELLIPSIS,
077            TokenTypes.LITERAL_SWITCH,
078            TokenTypes.LITERAL_SYNCHRONIZED,
079            TokenTypes.LITERAL_TRY,
080            TokenTypes.LITERAL_CASE,
081            TokenTypes.LAMBDA,
082            TokenTypes.LITERAL_WHEN,
083        };
084    }
085
086    @Override
087    public int[] getRequiredTokens() {
088        return CommonUtil.EMPTY_INT_ARRAY;
089    }
090
091    @Override
092    public void visitToken(DetailAST ast) {
093        if (ast.getType() == TokenTypes.TYPECAST) {
094            final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN);
095            final int[] line = getLineCodePoints(targetAST.getLineNo() - 1);
096            if (!isFollowedByWhitespace(targetAST, line)) {
097                log(targetAST, MSG_WS_TYPECAST);
098            }
099        }
100        else {
101            final int[] line = getLineCodePoints(ast.getLineNo() - 1);
102            if (!isFollowedByWhitespace(ast, line)) {
103                final Object[] message = {ast.getText()};
104                log(ast, MSG_WS_NOT_FOLLOWED, message);
105            }
106        }
107    }
108
109    /**
110     * Checks whether token is followed by a whitespace.
111     *
112     * @param targetAST Ast token.
113     * @param line Unicode code points array of line associated with the ast token.
114     * @return true if ast token is followed by a whitespace.
115     */
116    private static boolean isFollowedByWhitespace(DetailAST targetAST, int... line) {
117        final int after =
118            targetAST.getColumnNo() + targetAST.getText().length();
119        boolean followedByWhitespace = true;
120
121        if (after < line.length) {
122            final int codePoint = line[after];
123
124            followedByWhitespace = codePoint == ';'
125                || codePoint == ')'
126                || Character.isWhitespace(codePoint);
127        }
128        return followedByWhitespace;
129    }
130
131}