001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties" file.
044     */
045    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties" file.
049     */
050    public static final String MSG_WS_TYPECAST = "ws.typeCast";
051
052    /**
053     * Creates a new {@code WhitespaceAfterCheck} instance.
054     */
055    public WhitespaceAfterCheck() {
056        // no code by default
057    }
058
059    @Override
060    public int[] getDefaultTokens() {
061        return new int[] {
062            TokenTypes.COMMA,
063            TokenTypes.SEMI,
064            TokenTypes.TYPECAST,
065            TokenTypes.LITERAL_IF,
066            TokenTypes.LITERAL_ELSE,
067            TokenTypes.LITERAL_WHILE,
068            TokenTypes.LITERAL_DO,
069            TokenTypes.LITERAL_FOR,
070            TokenTypes.LITERAL_FINALLY,
071            TokenTypes.LITERAL_RETURN,
072            TokenTypes.LITERAL_YIELD,
073            TokenTypes.LITERAL_CATCH,
074            TokenTypes.DO_WHILE,
075            TokenTypes.ELLIPSIS,
076            TokenTypes.LITERAL_SWITCH,
077            TokenTypes.LITERAL_SYNCHRONIZED,
078            TokenTypes.LITERAL_TRY,
079            TokenTypes.LITERAL_CASE,
080            TokenTypes.LAMBDA,
081            TokenTypes.LITERAL_WHEN,
082        };
083    }
084
085    @Override
086    public int[] getAcceptableTokens() {
087        return new int[] {
088            TokenTypes.COMMA,
089            TokenTypes.SEMI,
090            TokenTypes.TYPECAST,
091            TokenTypes.LITERAL_IF,
092            TokenTypes.LITERAL_ELSE,
093            TokenTypes.LITERAL_WHILE,
094            TokenTypes.LITERAL_DO,
095            TokenTypes.LITERAL_FOR,
096            TokenTypes.LITERAL_FINALLY,
097            TokenTypes.LITERAL_RETURN,
098            TokenTypes.LITERAL_YIELD,
099            TokenTypes.LITERAL_CATCH,
100            TokenTypes.DO_WHILE,
101            TokenTypes.ELLIPSIS,
102            TokenTypes.LITERAL_SWITCH,
103            TokenTypes.LITERAL_SYNCHRONIZED,
104            TokenTypes.LITERAL_TRY,
105            TokenTypes.LITERAL_CASE,
106            TokenTypes.LAMBDA,
107            TokenTypes.LITERAL_WHEN,
108            TokenTypes.ANNOTATIONS,
109            TokenTypes.SINGLE_LINE_COMMENT,
110        };
111    }
112
113    @Override
114    public int[] getRequiredTokens() {
115        return CommonUtil.EMPTY_INT_ARRAY;
116    }
117
118    @Override
119    public boolean isCommentNodesRequired() {
120        return true;
121    }
122
123    @Override
124    public void visitToken(DetailAST ast) {
125        final int type = ast.getType();
126        if (type == TokenTypes.TYPECAST) {
127            final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN);
128            final int[] line = getLineCodePoints(targetAST.getLineNo() - 1);
129            if (!isFollowedByWhitespace(targetAST, line)) {
130                log(targetAST, MSG_WS_TYPECAST);
131            }
132        }
133        else if (type == TokenTypes.ANNOTATIONS) {
134            if (ast.getFirstChild() != null) {
135                DetailAST targetAST = ast.getFirstChild().getLastChild();
136                if (targetAST.getType() == TokenTypes.DOT) {
137                    targetAST = targetAST.getLastChild();
138                }
139                final int[] line = getLineCodePoints(targetAST.getLineNo() - 1);
140                if (!isFollowedByWhitespace(targetAST, line)) {
141                    final Object[] message = {targetAST.getText()};
142                    log(targetAST, MSG_WS_NOT_FOLLOWED, message);
143                }
144            }
145        }
146        else if (type == TokenTypes.SINGLE_LINE_COMMENT) {
147            if (!isCommentFollowedByWhitespace(ast)) {
148                log(ast, MSG_WS_NOT_FOLLOWED, ast.getText());
149            }
150        }
151        else {
152            final int[] line = getLineCodePoints(ast.getLineNo() - 1);
153            if (!isFollowedByWhitespace(ast, line)) {
154                final Object[] message = {ast.getText()};
155                log(ast, MSG_WS_NOT_FOLLOWED, message);
156            }
157        }
158    }
159
160    /**
161     * Checks whether a single-line comment is followed by whitespace.
162     *
163     * @param ast AST token representing the single-line comment.
164     * @return {@code true} if the comment content is empty or is
165     *         followed by whitespace after any leading slashes.
166     */
167    private static boolean isCommentFollowedByWhitespace(DetailAST ast) {
168        final DetailAST commentContent = ast.findFirstToken(TokenTypes.COMMENT_CONTENT);
169        final String commentText = commentContent.getText();
170        int index = 0;
171        while (commentText.charAt(index) == '/') {
172            index++;
173        }
174        final int codePoint = commentText.codePointAt(index);
175        return Character.isWhitespace(codePoint);
176    }
177
178    /**
179     * Checks whether token is followed by a whitespace.
180     *
181     * @param targetAST Ast token.
182     * @param line Unicode code points array of line associated with the ast token.
183     * @return true if ast token is followed by a whitespace.
184     */
185    private static boolean isFollowedByWhitespace(DetailAST targetAST, int... line) {
186        final int after =
187            targetAST.getColumnNo() + targetAST.getText().length();
188        boolean followedByWhitespace = true;
189
190        if (after < line.length) {
191            final int codePoint = line[after];
192
193            followedByWhitespace = codePoint == ';'
194                || codePoint == ')'
195                || Character.isWhitespace(codePoint);
196        }
197        return followedByWhitespace;
198    }
199
200}