KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 after a token.
29  * More specifically, it checks that it is not followed by whitespace,
30  * or (if linebreaks are allowed) all characters on the line after are
31  * whitespace. To forbid linebreaks afer a token, set property
32  * allowLineBreaks to false.
33  * </p>
34   * <p> By default the check will check the following operators:
35  * {@link TokenTypes#ARRAY_INIT ARRAY_INIT},
36  * {@link TokenTypes#BNOT BNOT},
37  * {@link TokenTypes#DEC DEC},
38  * {@link TokenTypes#DOT DOT},
39  * {@link TokenTypes#INC INC},
40  * {@link TokenTypes#LNOT LNOT},
41  * {@link TokenTypes#UNARY_MINUS UNARY_MINUS},
42  * {@link TokenTypes#UNARY_PLUS UNARY_PLUS}. It also supports the operator
43  * {@link TokenTypes#TYPECAST TYPECAST}.
44  * </p>
45  * <p>
46  * An example of how to configure the check is:
47  * </p>
48  * <pre>
49  * &lt;module name="NoWhitespaceAfter"/&gt;
50  * </pre>
51  * <p> An example of how to configure the check to forbid linebreaks after
52  * a {@link TokenTypes#DOT DOT} token is:
53  * </p>
54  * <pre>
55  * &lt;module name="NoWhitespaceAfter"&gt;
56  * &lt;property name="tokens" value="DOT"/&gt;
57  * &lt;property name="allowLineBreaks" value="false"/&gt;
58  * &lt;/module&gt;
59  * </pre>
60  * @author Rick Giles
61  * @author lkuehne
62  * @version 1.0
63  */

64 public class NoWhitespaceAfterCheck extends Check
65 {
66     /** Whether whitespace is allowed if the AST is at a linebreak */
67     private boolean mAllowLineBreaks = true;
68
69     /** {@inheritDoc} */
70     public int[] getDefaultTokens()
71     {
72         return new int[] {
73             TokenTypes.ARRAY_INIT,
74             TokenTypes.INC,
75             TokenTypes.DEC,
76             TokenTypes.UNARY_MINUS,
77             TokenTypes.UNARY_PLUS,
78             TokenTypes.BNOT,
79             TokenTypes.LNOT,
80             TokenTypes.DOT,
81         };
82     }
83
84     /** {@inheritDoc} */
85     public int[] getAcceptableTokens()
86     {
87         return new int[] {
88             TokenTypes.ARRAY_INIT,
89             TokenTypes.INC,
90             TokenTypes.DEC,
91             TokenTypes.UNARY_MINUS,
92             TokenTypes.UNARY_PLUS,
93             TokenTypes.BNOT,
94             TokenTypes.LNOT,
95             TokenTypes.DOT,
96             TokenTypes.TYPECAST,
97         };
98     }
99
100     /** {@inheritDoc} */
101     public void visitToken(DetailAST aAST)
102     {
103         DetailAST targetAST = aAST;
104         if (targetAST.getType() == TokenTypes.TYPECAST) {
105             targetAST = targetAST.findFirstToken(TokenTypes.RPAREN);
106         }
107         final String JavaDoc line = getLines()[aAST.getLineNo() - 1];
108         final int after =
109             targetAST.getColumnNo() + targetAST.getText().length();
110
111         if ((after >= line.length())
112             || Character.isWhitespace(line.charAt(after)))
113         {
114             boolean flag = !mAllowLineBreaks;
115             for (int i = after + 1; !flag && (i < line.length()); i++) {
116                 if (!Character.isWhitespace(line.charAt(i))) {
117                     flag = true;
118                 }
119             }
120             if (flag) {
121                 log(targetAST.getLineNo(), after,
122                     "ws.followed", targetAST.getText());
123             }
124         }
125     }
126
127     /**
128      * Control whether whitespace is flagged at linebreaks.
129      * @param aAllowLineBreaks whether whitespace should be
130      * flagged at linebreaks.
131      */

132     public void setAllowLineBreaks(boolean aAllowLineBreaks)
133     {
134         mAllowLineBreaks = aAllowLineBreaks;
135     }
136 }
137
Popular Tags