Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NoWhitespaceBeforeCheck |
|
| 3.75;3.75 |
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 | ||
20 | package com.puppycrawl.tools.checkstyle.checks.whitespace; | |
21 | ||
22 | import com.puppycrawl.tools.checkstyle.api.Check; | |
23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
25 | ||
26 | /** | |
27 | * <p> | |
28 | * Checks that there is no whitespace before a token. | |
29 | * More specifically, it checks that it is not preceded with whitespace, | |
30 | * or (if linebreaks are allowed) all characters on the line before are | |
31 | * whitespace. To allow linebreaks before a token, set property | |
32 | * allowLineBreaks to true. | |
33 | * </p> | |
34 | * <p> By default the check will check the following operators: | |
35 | * {@link TokenTypes#SEMI SEMI}, | |
36 | * {@link TokenTypes#POST_DEC POST_DEC}, | |
37 | * {@link TokenTypes#POST_INC POST_INC}. | |
38 | * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration | |
39 | * of this check. | |
40 | * </p> | |
41 | * | |
42 | * <p> | |
43 | * An example of how to configure the check is: | |
44 | * </p> | |
45 | * <pre> | |
46 | * <module name="NoWhitespaceBefore"/> | |
47 | * </pre> | |
48 | * <p> An example of how to configure the check to allow linebreaks before | |
49 | * a {@link TokenTypes#DOT DOT} token is: | |
50 | * </p> | |
51 | * <pre> | |
52 | * <module name="NoWhitespaceBefore"> | |
53 | * <property name="tokens" value="DOT"/> | |
54 | * <property name="allowLineBreaks" value="true"/> | |
55 | * </module> | |
56 | * </pre> | |
57 | * @author Rick Giles | |
58 | * @author lkuehne | |
59 | * @version 1.0 | |
60 | */ | |
61 | 3 | public class NoWhitespaceBeforeCheck |
62 | extends Check | |
63 | { | |
64 | /** Whether whitespace is allowed if the AST is at a linebreak */ | |
65 | private boolean mAllowLineBreaks; | |
66 | ||
67 | @Override | |
68 | public int[] getDefaultTokens() | |
69 | { | |
70 | 1 | return new int[] { |
71 | TokenTypes.SEMI, | |
72 | TokenTypes.POST_INC, | |
73 | TokenTypes.POST_DEC, | |
74 | }; | |
75 | } | |
76 | ||
77 | @Override | |
78 | public int[] getAcceptableTokens() | |
79 | { | |
80 | 2 | return new int[] { |
81 | TokenTypes.SEMI, | |
82 | TokenTypes.POST_INC, | |
83 | TokenTypes.POST_DEC, | |
84 | TokenTypes.DOT, | |
85 | }; | |
86 | } | |
87 | ||
88 | @Override | |
89 | public void visitToken(DetailAST aAST) | |
90 | { | |
91 | 91 | final String[] lines = getLines(); |
92 | 91 | final String line = lines[aAST.getLineNo() - 1]; |
93 | 91 | final int before = aAST.getColumnNo() - 1; |
94 | ||
95 | 91 | if ((before < 0) || Character.isWhitespace(line.charAt(before))) { |
96 | ||
97 | // empty FOR initializer? | |
98 | 19 | if (aAST.getType() == TokenTypes.SEMI) { |
99 | 7 | final DetailAST sibling = aAST.getPreviousSibling(); |
100 | 7 | if ((sibling != null) |
101 | && (sibling.getType() == TokenTypes.FOR_INIT) | |
102 | && (sibling.getChildCount() == 0)) | |
103 | { | |
104 | 1 | return; |
105 | } | |
106 | } | |
107 | ||
108 | 18 | boolean flag = !mAllowLineBreaks; |
109 | // verify all characters before '.' are whitespace | |
110 | 47 | for (int i = 0; !flag && (i < before); i++) { |
111 | 29 | if (!Character.isWhitespace(line.charAt(i))) { |
112 | 3 | flag = true; |
113 | } | |
114 | } | |
115 | 18 | if (flag) { |
116 | 16 | log(aAST.getLineNo(), before, "ws.preceded", aAST.getText()); |
117 | } | |
118 | } | |
119 | 90 | } |
120 | ||
121 | /** | |
122 | * Control whether whitespace is flagged at linebreaks. | |
123 | * @param aAllowLineBreaks whether whitespace should be | |
124 | * flagged at linebreaks. | |
125 | */ | |
126 | public void setAllowLineBreaks(boolean aAllowLineBreaks) | |
127 | { | |
128 | 1 | mAllowLineBreaks = aAllowLineBreaks; |
129 | 1 | } |
130 | } |