Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WhitespaceAfterCheck |
|
| 7.0;7 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 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 | 6 | public class WhitespaceAfterCheck |
56 | extends Check | |
57 | { | |
58 | @Override | |
59 | public int[] getDefaultTokens() | |
60 | { | |
61 | 6 | return new int[] { |
62 | TokenTypes.COMMA, | |
63 | TokenTypes.SEMI, | |
64 | TokenTypes.TYPECAST, | |
65 | }; | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void visitToken(DetailAST aAST) | |
70 | { | |
71 | final Object[] message; | |
72 | final DetailAST targetAST; | |
73 | 202 | if (aAST.getType() == TokenTypes.TYPECAST) { |
74 | 5 | targetAST = aAST.findFirstToken(TokenTypes.RPAREN); |
75 | // TODO: i18n | |
76 | 5 | message = new Object[]{"cast"}; |
77 | } | |
78 | else { | |
79 | 197 | targetAST = aAST; |
80 | 197 | message = new Object[]{aAST.getText()}; |
81 | } | |
82 | 202 | final String line = getLines()[targetAST.getLineNo() - 1]; |
83 | 202 | final int after = |
84 | targetAST.getColumnNo() + targetAST.getText().length(); | |
85 | ||
86 | 202 | if (after < line.length()) { |
87 | ||
88 | 81 | final char charAfter = line.charAt(after); |
89 | 81 | if ((targetAST.getType() == TokenTypes.SEMI) |
90 | && ((charAfter == ';') || (charAfter == ')'))) | |
91 | { | |
92 | 4 | return; |
93 | } | |
94 | 77 | if (!Character.isWhitespace(charAfter)) { |
95 | //empty FOR_ITERATOR? | |
96 | 11 | if (targetAST.getType() == TokenTypes.SEMI) { |
97 | 5 | final DetailAST sibling = |
98 | targetAST.getNextSibling(); | |
99 | 5 | if ((sibling != null) |
100 | && (sibling.getType() == TokenTypes.FOR_ITERATOR) | |
101 | && (sibling.getChildCount() == 0)) | |
102 | { | |
103 | 0 | return; |
104 | } | |
105 | } | |
106 | 11 | log(targetAST.getLineNo(), |
107 | targetAST.getColumnNo() + targetAST.getText().length(), | |
108 | "ws.notFollowed", | |
109 | message); | |
110 | } | |
111 | } | |
112 | 198 | } |
113 | } |