KickJava   Java API By Example, From Geeks To Geeks.

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


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.checks.AbstractOptionCheck;
25
26 /**
27  * <p>Checks the padding of an empty for iterator; that is whether a
28  * space is required at an empty for iterator, or such spaces are
29  * forbidden. No check occurs if there is a line wrap at the iterator, as in
30  * </p>
31  * <pre class="body">
32 for (Iterator foo = very.long.line.iterator();
33       foo.hasNext();
34      )
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  * &lt;module name="EmptyForIteratorPad"/&gt;
45  * </pre>
46  *
47  * @author Rick Giles
48  * @version 1.0
49  */

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

56     public EmptyForIteratorPadCheck()
57     {
58         super(PadOption.NOSPACE);
59     }
60
61     /** {@inheritDoc} */
62     public int[] getDefaultTokens()
63     {
64         return new int[] {TokenTypes.FOR_ITERATOR,
65         };
66     }
67
68     /** {@inheritDoc} */
69     public void visitToken(DetailAST aAST)
70     {
71         if (aAST.getChildCount() == 0) {
72             //empty for iterator. test pad after semi.
73
final DetailAST semi = aAST.getPreviousSibling();
74             final String JavaDoc line = getLines()[semi.getLineNo() - 1];
75             final int after = semi.getColumnNo() + 1;
76             //don't check if at end of line
77
if (after < line.length()) {
78                 if ((PadOption.NOSPACE == getAbstractOption())
79                     && (Character.isWhitespace(line.charAt(after))))
80                 {
81                     log(semi.getLineNo(), after, "ws.followed", ";");
82                 }
83                 else if ((PadOption.SPACE == getAbstractOption())
84                          && !Character.isWhitespace(line.charAt(after)))
85                 {
86                     log(semi.getLineNo(), after, "ws.notFollowed", ";");
87                 }
88             }
89         }
90     }
91 }
92
Popular Tags