1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2015 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 package com.puppycrawl.tools.checkstyle.checks.whitespace;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26 * <p>
27 * Checks that a token is followed by whitespace, with the exception that it
28 * does not check for whitespace after the semicolon of an empty for iterator.
29 * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
30 * empty for iterators.
31 * </p>
32 * <p> By default the check will check the following tokens:
33 * {@link TokenTypes#COMMA COMMA},
34 * {@link TokenTypes#SEMI SEMI},
35 * {@link TokenTypes#TYPECAST TYPECAST}.
36 * </p>
37 * <p>
38 * An example of how to configure the check is:
39 * </p>
40 * <pre>
41 * <module name="WhitespaceAfter"/>
42 * </pre>
43 * <p> An example of how to configure the check for whitespace only after
44 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
45 * </p>
46 * <pre>
47 * <module name="WhitespaceAfter">
48 * <property name="tokens" value="COMMA, SEMI"/>
49 * </module>
50 * </pre>
51 * @author Oliver Burn
52 * @author Rick Giles
53 * @version 1.0
54 */
55 public class WhitespaceAfterCheck
56 extends Check
57 {
58
59 /**
60 * A key is pointing to the warning message text in "messages.properties"
61 * file.
62 */
63 public static final String WS_NOT_FOLLOWED = "ws.notFollowed";
64
65 @Override
66 public int[] getDefaultTokens()
67 {
68 return new int[] {
69 TokenTypes.COMMA,
70 TokenTypes.SEMI,
71 TokenTypes.TYPECAST,
72 };
73 }
74
75 @Override
76 public int[] getAcceptableTokens()
77 {
78 return new int[] {
79 TokenTypes.COMMA,
80 TokenTypes.SEMI,
81 TokenTypes.TYPECAST,
82 };
83 }
84
85 @Override
86 public void visitToken(DetailAST ast)
87 {
88 final Object[] message;
89 final DetailAST targetAST;
90 if (ast.getType() == TokenTypes.TYPECAST) {
91 targetAST = ast.findFirstToken(TokenTypes.RPAREN);
92 // TODO: i18n
93 message = new Object[]{"cast"};
94 }
95 else {
96 targetAST = ast;
97 message = new Object[]{ast.getText()};
98 }
99 final String line = getLine(targetAST.getLineNo() - 1);
100 final int after =
101 targetAST.getColumnNo() + targetAST.getText().length();
102
103 if (after < line.length()) {
104
105 final char charAfter = line.charAt(after);
106 if ((targetAST.getType() == TokenTypes.SEMI)
107 && ((charAfter == ';') || (charAfter == ')')))
108 {
109 return;
110 }
111 if (!Character.isWhitespace(charAfter)) {
112 //empty FOR_ITERATOR?
113 if (targetAST.getType() == TokenTypes.SEMI) {
114 final DetailAST sibling =
115 targetAST.getNextSibling();
116 if ((sibling != null)
117 && (sibling.getType() == TokenTypes.FOR_ITERATOR)
118 && (sibling.getChildCount() == 0))
119 {
120 return;
121 }
122 }
123 log(targetAST.getLineNo(),
124 targetAST.getColumnNo() + targetAST.getText().length(),
125 WS_NOT_FOLLOWED,
126 message);
127 }
128 }
129 }
130 }