Source code: com/puppycrawl/tools/checkstyle/checks/EmptyForIteratorPadCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 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;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25 /**
26 * <p>Checks the padding of an empty for iterator; that is whether a
27 * space is required at an empty for iterator, or such spaces are
28 * forbidden.
29 * The policy to verify is specified using the {@link PadOption} class and
30 * defaults to {@link PadOption#NOSPACE}.
31 * </p>
32 * <p>
33 * An example of how to configure the check is:
34 * </p>
35 * <pre>
36 * <module name="EmptyForIteratorPad"/>
37 * </pre>
38 * <p>
39 * @author Rick Giles
40 * @version 1.0
41 */
42 public class EmptyForIteratorPadCheck
43 extends AbstractOptionCheck
44 {
45 /**
46 * Sets the paren pad otion to nospace.
47 */
48 public EmptyForIteratorPadCheck()
49 {
50 super(PadOption.NOSPACE);
51 }
52
53 /** @see com.puppycrawl.tools.checkstyle.api.Check */
54 public int[] getDefaultTokens()
55 {
56 return new int[] {TokenTypes.FOR_ITERATOR,
57 };
58 }
59
60 /** @see com.puppycrawl.tools.checkstyle.api.Check */
61 public void visitToken(DetailAST aAST)
62 {
63 if (aAST.getChildCount() == 0) {
64 final String line = getLines()[aAST.getLineNo() - 1];
65 final int after = aAST.getColumnNo() - 1;
66 if (after < line.length()) {
67 if ((PadOption.NOSPACE == getAbstractOption())
68 && (Character.isWhitespace(line.charAt(after))))
69 {
70 log(aAST.getLineNo(), after, "ws.followed", ";");
71 }
72 else if ((PadOption.SPACE == getAbstractOption())
73 && !Character.isWhitespace(line.charAt(after))
74 && (line.charAt(after) != ')'))
75 {
76 log(aAST.getLineNo(), after, "ws.notFollowed", ";");
77 }
78 }
79 }
80 }
81 }