Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IllegalTokenCheck |
|
| 1.6666666666666667;1.667 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 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 com.puppycrawl.tools.checkstyle.api.Check; | |
22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
24 | import java.util.Set; | |
25 | ||
26 | /** | |
27 | * <p> | |
28 | * Checks for illegal tokens. | |
29 | * </p> | |
30 | * <p> | |
31 | * Rational: Certain language features are often lead to hard to | |
32 | * maintain code or are non-obvious to novice developers. Others | |
33 | * may be discouraged in certain frameworks, such as not having | |
34 | * native methods in EJB components. | |
35 | * </p> | |
36 | * <p> | |
37 | * An example of how to configure the check is: | |
38 | * </p> | |
39 | * <pre> | |
40 | * <module name="IllegalToken"/> | |
41 | * </pre> | |
42 | * <p> An example of how to configure the check to forbid | |
43 | * a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is: | |
44 | * </p> | |
45 | * <pre> | |
46 | * <module name="IllegalToken"> | |
47 | * <property name="tokens" value="LITERAL_NATIVE"/> | |
48 | * </module> | |
49 | * </pre> | |
50 | * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> | |
51 | * @author Rick Giles | |
52 | */ | |
53 | 2 | public class IllegalTokenCheck |
54 | extends Check | |
55 | { | |
56 | @Override | |
57 | public int[] getDefaultTokens() | |
58 | { | |
59 | 2 | return new int[] { |
60 | TokenTypes.LITERAL_SWITCH, | |
61 | TokenTypes.POST_INC, | |
62 | TokenTypes.POST_DEC, | |
63 | }; | |
64 | } | |
65 | ||
66 | @Override | |
67 | public int[] getAcceptableTokens() | |
68 | { | |
69 | // Any tokens set by property 'tokens' are acceptable | |
70 | 1 | int[] tokensToCopy = getDefaultTokens(); |
71 | 1 | final Set<String> tokenNames = getTokenNames(); |
72 | 1 | if (!tokenNames.isEmpty()) { |
73 | 1 | tokensToCopy = new int[tokenNames.size()]; |
74 | 1 | int i = 0; |
75 | 1 | for (String name : tokenNames) { |
76 | 1 | tokensToCopy[i] = TokenTypes.getTokenId(name); |
77 | 1 | i++; |
78 | } | |
79 | } | |
80 | 1 | final int[] copy = new int[tokensToCopy.length]; |
81 | 1 | System.arraycopy(tokensToCopy, 0, copy, 0, tokensToCopy.length); |
82 | 1 | return copy; |
83 | } | |
84 | ||
85 | @Override | |
86 | public void visitToken(DetailAST aAST) | |
87 | { | |
88 | 4 | log( |
89 | aAST.getLineNo(), | |
90 | aAST.getColumnNo(), | |
91 | "illegal.token", | |
92 | aAST.getText()); | |
93 | 4 | } |
94 | ||
95 | } |