View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27  
28  /**
29   * <div>
30   * Checks that a token is followed by whitespace, with the exception that it
31   * does not check for whitespace after the semicolon of an empty for iterator.
32   * Use Check
33   * <a href="https://checkstyle.org/checks/whitespace/emptyforiteratorpad.html">
34   * EmptyForIteratorPad</a> to validate empty for iterators.
35   * </div>
36   *
37   * @since 3.0
38   */
39  @StatelessCheck
40  public class WhitespaceAfterCheck extends AbstractCheck {
41  
42      /**
43       * A key is pointing to the warning message text in "messages.properties" file.
44       */
45      public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties" file.
49       */
50      public static final String MSG_WS_TYPECAST = "ws.typeCast";
51  
52      /**
53       * Creates a new {@code WhitespaceAfterCheck} instance.
54       */
55      public WhitespaceAfterCheck() {
56          // no code by default
57      }
58  
59      @Override
60      public int[] getDefaultTokens() {
61          return new int[] {
62              TokenTypes.COMMA,
63              TokenTypes.SEMI,
64              TokenTypes.TYPECAST,
65              TokenTypes.LITERAL_IF,
66              TokenTypes.LITERAL_ELSE,
67              TokenTypes.LITERAL_WHILE,
68              TokenTypes.LITERAL_DO,
69              TokenTypes.LITERAL_FOR,
70              TokenTypes.LITERAL_FINALLY,
71              TokenTypes.LITERAL_RETURN,
72              TokenTypes.LITERAL_YIELD,
73              TokenTypes.LITERAL_CATCH,
74              TokenTypes.DO_WHILE,
75              TokenTypes.ELLIPSIS,
76              TokenTypes.LITERAL_SWITCH,
77              TokenTypes.LITERAL_SYNCHRONIZED,
78              TokenTypes.LITERAL_TRY,
79              TokenTypes.LITERAL_CASE,
80              TokenTypes.LAMBDA,
81              TokenTypes.LITERAL_WHEN,
82          };
83      }
84  
85      @Override
86      public int[] getAcceptableTokens() {
87          return new int[] {
88              TokenTypes.COMMA,
89              TokenTypes.SEMI,
90              TokenTypes.TYPECAST,
91              TokenTypes.LITERAL_IF,
92              TokenTypes.LITERAL_ELSE,
93              TokenTypes.LITERAL_WHILE,
94              TokenTypes.LITERAL_DO,
95              TokenTypes.LITERAL_FOR,
96              TokenTypes.LITERAL_FINALLY,
97              TokenTypes.LITERAL_RETURN,
98              TokenTypes.LITERAL_YIELD,
99              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 }