KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > IllegalTokenCheck


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
package com.puppycrawl.tools.checkstyle.checks.coding;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.Check;
25 import com.puppycrawl.tools.checkstyle.api.DetailAST;
26 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27
28 /**
29  * <p>
30  * Checks for illegal tokens.
31  * </p>
32  * <p>
33  * Rational: Certain language features are often lead to hard to
34  * maintain code or are non-obvious to novice developers. Others
35  * may be discouraged in certain frameworks, such as not having
36  * native methods in EJB components.
37  * </p>
38  * <p>
39  * An example of how to configure the check is:
40  * </p>
41  * <pre>
42  * &lt;module name="IllegalToken"/&gt;
43  * </pre>
44  * <p> An example of how to configure the check to forbid
45  * a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is:
46  * </p>
47  * <pre>
48  * &lt;module name="IllegalToken"&gt;
49  * &lt;property name="tokens" value="LITERAL_NATIVE"/&gt;
50  * &lt;/module&gt;
51  * </pre>
52  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
53  * @author Rick Giles
54  */

55 public class IllegalTokenCheck
56     extends Check
57 {
58     /**
59      * {@inheritDoc}
60      */

61     public int[] getDefaultTokens()
62     {
63         return new int[] {
64             TokenTypes.LITERAL_SWITCH,
65             TokenTypes.POST_INC,
66             TokenTypes.POST_DEC,
67         };
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public int[] getAcceptableTokens()
74     {
75         // Any tokens set by property 'tokens' are acceptable
76
int[] tokensToCopy = getDefaultTokens();
77         final Set JavaDoc tokenNames = getTokenNames();
78         if (!tokenNames.isEmpty()) {
79             tokensToCopy = new int[tokenNames.size()];
80             int i = 0;
81             final Iterator JavaDoc it = tokenNames.iterator();
82             while (it.hasNext()) {
83                 final String JavaDoc name = (String JavaDoc) it.next();
84                 tokensToCopy[i] = TokenTypes.getTokenId(name);
85                 i++;
86             }
87         }
88         final int[] copy = new int[tokensToCopy.length];
89         System.arraycopy(tokensToCopy, 0, copy, 0, tokensToCopy.length);
90         return copy;
91     }
92
93     /**
94      * {@inheritDoc}
95      */

96     public void visitToken(DetailAST aAST)
97     {
98         log(
99             aAST.getLineNo(),
100             aAST.getColumnNo(),
101             "illegal.token",
102             aAST.getText());
103     }
104
105 }
106
Popular Tags