Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EmptyForInitializerPadCheck |
|
| 3.0;3 |
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.DetailAST; | |
23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
24 | import com.puppycrawl.tools.checkstyle.api.Utils; | |
25 | import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; | |
26 | ||
27 | /** | |
28 | * <p>Checks the padding of an empty for initializer; that is whether a | |
29 | * space is required at an empty for initializer, or such spaces are | |
30 | * forbidden. No check occurs if there is a line wrap at the initializer, as in | |
31 | * </p> | |
32 | * <pre class="body"> | |
33 | for ( | |
34 | ; i < j; i++, j--) | |
35 | </pre> | |
36 | * <p> | |
37 | * The policy to verify is specified using the {@link PadOption} class and | |
38 | * defaults to {@link PadOption#NOSPACE}. | |
39 | * </p> | |
40 | * <p> | |
41 | * An example of how to configure the check is: | |
42 | * </p> | |
43 | * <pre> | |
44 | * <module name="EmptyForInitializerPad"/> | |
45 | * </pre> | |
46 | * | |
47 | * @author lkuehne | |
48 | * @version 1.0 | |
49 | */ | |
50 | public class EmptyForInitializerPadCheck | |
51 | extends AbstractOptionCheck<PadOption> | |
52 | { | |
53 | /** | |
54 | * Sets the paren pad otion to nospace. | |
55 | */ | |
56 | public EmptyForInitializerPadCheck() | |
57 | { | |
58 | 2 | super(PadOption.NOSPACE, PadOption.class); |
59 | 2 | } |
60 | ||
61 | @Override | |
62 | public int[] getDefaultTokens() | |
63 | { | |
64 | 2 | return new int[] {TokenTypes.FOR_INIT, |
65 | }; | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void visitToken(DetailAST aAST) | |
70 | { | |
71 | 22 | if (aAST.getChildCount() == 0) { |
72 | //empty for initializer. test pad before semi. | |
73 | 4 | final DetailAST semi = aAST.getNextSibling(); |
74 | 4 | final int semiLineIdx = semi.getLineNo() - 1; |
75 | 4 | final String line = getLines()[semiLineIdx]; |
76 | 4 | final int before = semi.getColumnNo() - 1; |
77 | //don't check if semi at beginning of line | |
78 | 4 | if (!Utils.whitespaceBefore(before, line)) { |
79 | 4 | final PadOption option = getAbstractOption(); |
80 | 4 | if ((PadOption.NOSPACE == option) |
81 | && (Character.isWhitespace(line.charAt(before)))) | |
82 | { | |
83 | 1 | log(semi.getLineNo(), before, "ws.preceded", ";"); |
84 | } | |
85 | 3 | else if ((PadOption.SPACE == option) |
86 | && !Character.isWhitespace(line.charAt(before))) | |
87 | { | |
88 | 1 | log(semi.getLineNo(), before, "ws.notPreceded", ";"); |
89 | } | |
90 | } | |
91 | } | |
92 | 22 | } |
93 | } |