Source code: com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 Oliver Burn
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 /** @see com.puppycrawl.tools.checkstyle.api.Check */
59 public int[] getDefaultTokens()
60 {
61 return new int[] {
62 TokenTypes.COMMA,
63 TokenTypes.SEMI,
64 TokenTypes.TYPECAST,
65 };
66 }
67
68 /** @see com.puppycrawl.tools.checkstyle.api.Check */
69 public void visitToken(DetailAST aAST)
70 {
71 final String[] lines = getLines();
72 final Object[] message;
73 final DetailAST targetAST;
74 if (aAST.getType() == TokenTypes.TYPECAST) {
75 targetAST = aAST.findFirstToken(TokenTypes.RPAREN);
76 // TODO: i18n
77 message = new Object[]{"cast"};
78 }
79 else {
80 targetAST = aAST;
81 message = new Object[]{aAST.getText()};
82 }
83 final String line = lines[targetAST.getLineNo() - 1];
84 final int after =
85 targetAST.getColumnNo() + targetAST.getText().length();
86
87 if (after < line.length()) {
88
89 final char charAfter = line.charAt(after);
90 if ((targetAST.getType() == TokenTypes.SEMI)
91 && ((charAfter == ';') || (charAfter == ')')))
92 {
93 return;
94 }
95 if (!Character.isWhitespace(charAfter)) {
96 //empty FOR_ITERATOR?
97 if (targetAST.getType() == TokenTypes.SEMI) {
98 final DetailAST sibling =
99 (DetailAST) targetAST.getNextSibling();
100 if ((sibling != null)
101 && (sibling.getType() == TokenTypes.FOR_ITERATOR)
102 && (sibling.getChildCount() == 0))
103 {
104 return;
105 }
106 }
107 log(targetAST.getLineNo(),
108 targetAST.getColumnNo() + targetAST.getText().length(),
109 "ws.notFollowed",
110 message);
111 }
112 }
113 }
114 }