KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > whitespace > EmptyForInitializerPadCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 import com.puppycrawl.tools.checkstyle.checks.AbstractOption;
27
28 /**
29  * <p>Checks the padding of an empty for initializer; that is whether a
30  * space is required at an empty for initializer, or such spaces are
31  * forbidden. No check occurs if there is a line wrap at the initializer, as in
32  * </p>
33  * <pre class="body">
34 for (
35       ; i &lt; j; i++, j--)
36    </pre>
37  * <p>
38  * The policy to verify is specified using the {@link PadOption} class and
39  * defaults to {@link PadOption#NOSPACE}.
40  * </p>
41  * <p>
42  * An example of how to configure the check is:
43  * </p>
44  * <pre>
45  * &lt;module name="EmptyForInitializerPad"/&gt;
46  * </pre>
47  *
48  * @author lkuehne
49  * @version 1.0
50  */

51 public class EmptyForInitializerPadCheck
52     extends AbstractOptionCheck
53 {
54     /**
55      * Sets the paren pad otion to nospace.
56      */

57     public EmptyForInitializerPadCheck()
58     {
59         super(PadOption.NOSPACE);
60     }
61
62     /** {@inheritDoc} */
63     public int[] getDefaultTokens()
64     {
65         return new int[] {TokenTypes.FOR_INIT,
66         };
67     }
68
69     /** {@inheritDoc} */
70     public void visitToken(DetailAST aAST)
71     {
72         if (aAST.getChildCount() == 0) {
73             //empty for initializer. test pad before semi.
74
final DetailAST semi = (DetailAST) aAST.getNextSibling();
75             final int semiLineIdx = semi.getLineNo() - 1;
76             final String JavaDoc line = getLines()[semiLineIdx];
77             final int before = semi.getColumnNo() - 1;
78             //don't check if semi at beginning of line
79
if (!Utils.whitespaceBefore(before, line)) {
80                 final AbstractOption abstractOption = getAbstractOption();
81                 if ((PadOption.NOSPACE == abstractOption)
82                     && (Character.isWhitespace(line.charAt(before))))
83                 {
84                     log(semi.getLineNo(), before, "ws.preceded", ";");
85                 }
86                 else if ((PadOption.SPACE == abstractOption)
87                          && !Character.isWhitespace(line.charAt(before)))
88                 {
89                     log(semi.getLineNo(), before, "ws.notPreceded", ";");
90                 }
91             }
92         }
93     }
94 }
95
Popular Tags