View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
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.grammars;
21  
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * GeneratedJavaTokenTypesTest.
27   * @noinspection ClassIndependentOfModule
28   */
29  public class GeneratedJavaTokenTypesTest {
30      /**
31       * <p>
32       * New tokens must be added onto the end of the list with new numbers, and
33       * old tokens must remain and keep their current numbering. Old token
34       * numberings are not allowed to change.
35       * </p>
36       *
37       * <p>
38       * The reason behind this is Java inlines static final field values directly
39       * into the compiled Java code. This loses all connections with the original
40       * class, GeneratedJavaTokenTypes, and so numbering updates are not picked
41       * up in user-created checks and causes conflicts.
42       * </p>
43       *
44       * <p>
45       * Issue: https://github.com/checkstyle/checkstyle/issues/505
46       * </p>
47       */
48      @Test
49      public void testTokenNumbering() {
50          final String message = "A token's number has changed. Please open"
51                  + " 'GeneratedJavaTokenTypesTest' and confirm which token is at fault.\n"
52                  + "Token numbers must not change or else they will create a conflict"
53                  + " with users.\n\n"
54                  + "See Issue: https://github.com/checkstyle/checkstyle/issues/505";
55  
56          // Read JavaDoc before changing
57          Assert.assertEquals(message, 1, GeneratedJavaTokenTypes.EOF);
58          Assert.assertEquals(message, 3, GeneratedJavaTokenTypes.NULL_TREE_LOOKAHEAD);
59          Assert.assertEquals(message, 4, GeneratedJavaTokenTypes.BLOCK);
60          Assert.assertEquals(message, 5, GeneratedJavaTokenTypes.MODIFIERS);
61          Assert.assertEquals(message, 6, GeneratedJavaTokenTypes.OBJBLOCK);
62          Assert.assertEquals(message, 7, GeneratedJavaTokenTypes.SLIST);
63          Assert.assertEquals(message, 8, GeneratedJavaTokenTypes.CTOR_DEF);
64          Assert.assertEquals(message, 9, GeneratedJavaTokenTypes.METHOD_DEF);
65          Assert.assertEquals(message, 10, GeneratedJavaTokenTypes.VARIABLE_DEF);
66          Assert.assertEquals(message, 11, GeneratedJavaTokenTypes.INSTANCE_INIT);
67          Assert.assertEquals(message, 12, GeneratedJavaTokenTypes.STATIC_INIT);
68          Assert.assertEquals(message, 13, GeneratedJavaTokenTypes.TYPE);
69          Assert.assertEquals(message, 14, GeneratedJavaTokenTypes.CLASS_DEF);
70          Assert.assertEquals(message, 15, GeneratedJavaTokenTypes.INTERFACE_DEF);
71          Assert.assertEquals(message, 16, GeneratedJavaTokenTypes.PACKAGE_DEF);
72          Assert.assertEquals(message, 17, GeneratedJavaTokenTypes.ARRAY_DECLARATOR);
73          Assert.assertEquals(message, 18, GeneratedJavaTokenTypes.EXTENDS_CLAUSE);
74          Assert.assertEquals(message, 19, GeneratedJavaTokenTypes.IMPLEMENTS_CLAUSE);
75          Assert.assertEquals(message, 20, GeneratedJavaTokenTypes.PARAMETERS);
76          Assert.assertEquals(message, 21, GeneratedJavaTokenTypes.PARAMETER_DEF);
77          Assert.assertEquals(message, 22, GeneratedJavaTokenTypes.LABELED_STAT);
78          Assert.assertEquals(message, 23, GeneratedJavaTokenTypes.TYPECAST);
79          Assert.assertEquals(message, 24, GeneratedJavaTokenTypes.INDEX_OP);
80          Assert.assertEquals(message, 25, GeneratedJavaTokenTypes.POST_INC);
81          Assert.assertEquals(message, 26, GeneratedJavaTokenTypes.POST_DEC);
82          Assert.assertEquals(message, 27, GeneratedJavaTokenTypes.METHOD_CALL);
83          Assert.assertEquals(message, 28, GeneratedJavaTokenTypes.EXPR);
84          Assert.assertEquals(message, 29, GeneratedJavaTokenTypes.ARRAY_INIT);
85          Assert.assertEquals(message, 30, GeneratedJavaTokenTypes.IMPORT);
86          Assert.assertEquals(message, 31, GeneratedJavaTokenTypes.UNARY_MINUS);
87          Assert.assertEquals(message, 32, GeneratedJavaTokenTypes.UNARY_PLUS);
88          Assert.assertEquals(message, 33, GeneratedJavaTokenTypes.CASE_GROUP);
89          Assert.assertEquals(message, 34, GeneratedJavaTokenTypes.ELIST);
90          Assert.assertEquals(message, 35, GeneratedJavaTokenTypes.FOR_INIT);
91          Assert.assertEquals(message, 36, GeneratedJavaTokenTypes.FOR_CONDITION);
92          Assert.assertEquals(message, 37, GeneratedJavaTokenTypes.FOR_ITERATOR);
93          Assert.assertEquals(message, 38, GeneratedJavaTokenTypes.EMPTY_STAT);
94          Assert.assertEquals(message, 39, GeneratedJavaTokenTypes.FINAL);
95          Assert.assertEquals(message, 40, GeneratedJavaTokenTypes.ABSTRACT);
96          Assert.assertEquals(message, 41, GeneratedJavaTokenTypes.STRICTFP);
97          Assert.assertEquals(message, 42, GeneratedJavaTokenTypes.SUPER_CTOR_CALL);
98          Assert.assertEquals(message, 43, GeneratedJavaTokenTypes.CTOR_CALL);
99          Assert.assertEquals(message, 44, GeneratedJavaTokenTypes.LITERAL_package);
100         Assert.assertEquals(message, 45, GeneratedJavaTokenTypes.SEMI);
101         Assert.assertEquals(message, 46, GeneratedJavaTokenTypes.LITERAL_import);
102         Assert.assertEquals(message, 47, GeneratedJavaTokenTypes.LBRACK);
103         Assert.assertEquals(message, 48, GeneratedJavaTokenTypes.RBRACK);
104         Assert.assertEquals(message, 49, GeneratedJavaTokenTypes.LITERAL_void);
105         Assert.assertEquals(message, 50, GeneratedJavaTokenTypes.LITERAL_boolean);
106         Assert.assertEquals(message, 51, GeneratedJavaTokenTypes.LITERAL_byte);
107         Assert.assertEquals(message, 52, GeneratedJavaTokenTypes.LITERAL_char);
108         Assert.assertEquals(message, 53, GeneratedJavaTokenTypes.LITERAL_short);
109         Assert.assertEquals(message, 54, GeneratedJavaTokenTypes.LITERAL_int);
110         Assert.assertEquals(message, 55, GeneratedJavaTokenTypes.LITERAL_float);
111         Assert.assertEquals(message, 56, GeneratedJavaTokenTypes.LITERAL_long);
112         Assert.assertEquals(message, 57, GeneratedJavaTokenTypes.LITERAL_double);
113         Assert.assertEquals(message, 58, GeneratedJavaTokenTypes.IDENT);
114         Assert.assertEquals(message, 59, GeneratedJavaTokenTypes.DOT);
115         Assert.assertEquals(message, 60, GeneratedJavaTokenTypes.STAR);
116         Assert.assertEquals(message, 61, GeneratedJavaTokenTypes.LITERAL_private);
117         Assert.assertEquals(message, 62, GeneratedJavaTokenTypes.LITERAL_public);
118         Assert.assertEquals(message, 63, GeneratedJavaTokenTypes.LITERAL_protected);
119         Assert.assertEquals(message, 64, GeneratedJavaTokenTypes.LITERAL_static);
120         Assert.assertEquals(message, 65, GeneratedJavaTokenTypes.LITERAL_transient);
121         Assert.assertEquals(message, 66, GeneratedJavaTokenTypes.LITERAL_native);
122         Assert.assertEquals(message, 67, GeneratedJavaTokenTypes.LITERAL_synchronized);
123         Assert.assertEquals(message, 68, GeneratedJavaTokenTypes.LITERAL_volatile);
124         Assert.assertEquals(message, 69, GeneratedJavaTokenTypes.LITERAL_class);
125         Assert.assertEquals(message, 70, GeneratedJavaTokenTypes.LITERAL_extends);
126         Assert.assertEquals(message, 71, GeneratedJavaTokenTypes.LITERAL_interface);
127         Assert.assertEquals(message, 72, GeneratedJavaTokenTypes.LCURLY);
128         Assert.assertEquals(message, 73, GeneratedJavaTokenTypes.RCURLY);
129         Assert.assertEquals(message, 74, GeneratedJavaTokenTypes.COMMA);
130         Assert.assertEquals(message, 75, GeneratedJavaTokenTypes.LITERAL_implements);
131         Assert.assertEquals(message, 76, GeneratedJavaTokenTypes.LPAREN);
132         Assert.assertEquals(message, 77, GeneratedJavaTokenTypes.RPAREN);
133         Assert.assertEquals(message, 78, GeneratedJavaTokenTypes.LITERAL_this);
134         Assert.assertEquals(message, 79, GeneratedJavaTokenTypes.LITERAL_super);
135         Assert.assertEquals(message, 80, GeneratedJavaTokenTypes.ASSIGN);
136         Assert.assertEquals(message, 81, GeneratedJavaTokenTypes.LITERAL_throws);
137         Assert.assertEquals(message, 82, GeneratedJavaTokenTypes.COLON);
138         Assert.assertEquals(message, 83, GeneratedJavaTokenTypes.LITERAL_if);
139         Assert.assertEquals(message, 84, GeneratedJavaTokenTypes.LITERAL_while);
140         Assert.assertEquals(message, 85, GeneratedJavaTokenTypes.LITERAL_do);
141         Assert.assertEquals(message, 86, GeneratedJavaTokenTypes.LITERAL_break);
142         Assert.assertEquals(message, 87, GeneratedJavaTokenTypes.LITERAL_continue);
143         Assert.assertEquals(message, 88, GeneratedJavaTokenTypes.LITERAL_return);
144         Assert.assertEquals(message, 89, GeneratedJavaTokenTypes.LITERAL_switch);
145         Assert.assertEquals(message, 90, GeneratedJavaTokenTypes.LITERAL_throw);
146         Assert.assertEquals(message, 91, GeneratedJavaTokenTypes.LITERAL_for);
147         Assert.assertEquals(message, 92, GeneratedJavaTokenTypes.LITERAL_else);
148         Assert.assertEquals(message, 93, GeneratedJavaTokenTypes.LITERAL_case);
149         Assert.assertEquals(message, 94, GeneratedJavaTokenTypes.LITERAL_default);
150         Assert.assertEquals(message, 95, GeneratedJavaTokenTypes.LITERAL_try);
151         Assert.assertEquals(message, 96, GeneratedJavaTokenTypes.LITERAL_catch);
152         Assert.assertEquals(message, 97, GeneratedJavaTokenTypes.LITERAL_finally);
153         Assert.assertEquals(message, 98, GeneratedJavaTokenTypes.PLUS_ASSIGN);
154         Assert.assertEquals(message, 99, GeneratedJavaTokenTypes.MINUS_ASSIGN);
155         Assert.assertEquals(message, 100, GeneratedJavaTokenTypes.STAR_ASSIGN);
156         Assert.assertEquals(message, 101, GeneratedJavaTokenTypes.DIV_ASSIGN);
157         Assert.assertEquals(message, 102, GeneratedJavaTokenTypes.MOD_ASSIGN);
158         Assert.assertEquals(message, 103, GeneratedJavaTokenTypes.SR_ASSIGN);
159         Assert.assertEquals(message, 104, GeneratedJavaTokenTypes.BSR_ASSIGN);
160         Assert.assertEquals(message, 105, GeneratedJavaTokenTypes.SL_ASSIGN);
161         Assert.assertEquals(message, 106, GeneratedJavaTokenTypes.BAND_ASSIGN);
162         Assert.assertEquals(message, 107, GeneratedJavaTokenTypes.BXOR_ASSIGN);
163         Assert.assertEquals(message, 108, GeneratedJavaTokenTypes.BOR_ASSIGN);
164         Assert.assertEquals(message, 109, GeneratedJavaTokenTypes.QUESTION);
165         Assert.assertEquals(message, 110, GeneratedJavaTokenTypes.LOR);
166         Assert.assertEquals(message, 111, GeneratedJavaTokenTypes.LAND);
167         Assert.assertEquals(message, 112, GeneratedJavaTokenTypes.BOR);
168         Assert.assertEquals(message, 113, GeneratedJavaTokenTypes.BXOR);
169         Assert.assertEquals(message, 114, GeneratedJavaTokenTypes.BAND);
170         Assert.assertEquals(message, 115, GeneratedJavaTokenTypes.NOT_EQUAL);
171         Assert.assertEquals(message, 116, GeneratedJavaTokenTypes.EQUAL);
172         Assert.assertEquals(message, 117, GeneratedJavaTokenTypes.LT);
173         Assert.assertEquals(message, 118, GeneratedJavaTokenTypes.GT);
174         Assert.assertEquals(message, 119, GeneratedJavaTokenTypes.LE);
175         Assert.assertEquals(message, 120, GeneratedJavaTokenTypes.GE);
176         Assert.assertEquals(message, 121, GeneratedJavaTokenTypes.LITERAL_instanceof);
177         Assert.assertEquals(message, 122, GeneratedJavaTokenTypes.SL);
178         Assert.assertEquals(message, 123, GeneratedJavaTokenTypes.SR);
179         Assert.assertEquals(message, 124, GeneratedJavaTokenTypes.BSR);
180         Assert.assertEquals(message, 125, GeneratedJavaTokenTypes.PLUS);
181         Assert.assertEquals(message, 126, GeneratedJavaTokenTypes.MINUS);
182         Assert.assertEquals(message, 127, GeneratedJavaTokenTypes.DIV);
183         Assert.assertEquals(message, 128, GeneratedJavaTokenTypes.MOD);
184         Assert.assertEquals(message, 129, GeneratedJavaTokenTypes.INC);
185         Assert.assertEquals(message, 130, GeneratedJavaTokenTypes.DEC);
186         Assert.assertEquals(message, 131, GeneratedJavaTokenTypes.BNOT);
187         Assert.assertEquals(message, 132, GeneratedJavaTokenTypes.LNOT);
188         Assert.assertEquals(message, 133, GeneratedJavaTokenTypes.LITERAL_true);
189         Assert.assertEquals(message, 134, GeneratedJavaTokenTypes.LITERAL_false);
190         Assert.assertEquals(message, 135, GeneratedJavaTokenTypes.LITERAL_null);
191         Assert.assertEquals(message, 136, GeneratedJavaTokenTypes.LITERAL_new);
192         Assert.assertEquals(message, 137, GeneratedJavaTokenTypes.NUM_INT);
193         Assert.assertEquals(message, 138, GeneratedJavaTokenTypes.CHAR_LITERAL);
194         Assert.assertEquals(message, 139, GeneratedJavaTokenTypes.STRING_LITERAL);
195         Assert.assertEquals(message, 140, GeneratedJavaTokenTypes.NUM_FLOAT);
196         Assert.assertEquals(message, 141, GeneratedJavaTokenTypes.NUM_LONG);
197         Assert.assertEquals(message, 142, GeneratedJavaTokenTypes.NUM_DOUBLE);
198         Assert.assertEquals(message, 143, GeneratedJavaTokenTypes.WS);
199         Assert.assertEquals(message, 144, GeneratedJavaTokenTypes.SINGLE_LINE_COMMENT);
200         Assert.assertEquals(message, 145, GeneratedJavaTokenTypes.BLOCK_COMMENT_BEGIN);
201         Assert.assertEquals(message, 146, GeneratedJavaTokenTypes.ESC);
202         Assert.assertEquals(message, 147, GeneratedJavaTokenTypes.HEX_DIGIT);
203         Assert.assertEquals(message, 148, GeneratedJavaTokenTypes.VOCAB);
204         Assert.assertEquals(message, 149, GeneratedJavaTokenTypes.EXPONENT);
205         Assert.assertEquals(message, 150, GeneratedJavaTokenTypes.FLOAT_SUFFIX);
206         Assert.assertEquals(message, 151, GeneratedJavaTokenTypes.ASSERT);
207         Assert.assertEquals(message, 152, GeneratedJavaTokenTypes.STATIC_IMPORT);
208         Assert.assertEquals(message, 153, GeneratedJavaTokenTypes.ENUM);
209         Assert.assertEquals(message, 154, GeneratedJavaTokenTypes.ENUM_DEF);
210         Assert.assertEquals(message, 155, GeneratedJavaTokenTypes.ENUM_CONSTANT_DEF);
211         Assert.assertEquals(message, 156, GeneratedJavaTokenTypes.FOR_EACH_CLAUSE);
212         Assert.assertEquals(message, 157, GeneratedJavaTokenTypes.ANNOTATION_DEF);
213         Assert.assertEquals(message, 158, GeneratedJavaTokenTypes.ANNOTATIONS);
214         Assert.assertEquals(message, 159, GeneratedJavaTokenTypes.ANNOTATION);
215         Assert.assertEquals(message, 160, GeneratedJavaTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR);
216         Assert.assertEquals(message, 161, GeneratedJavaTokenTypes.ANNOTATION_FIELD_DEF);
217         Assert.assertEquals(message, 162, GeneratedJavaTokenTypes.ANNOTATION_ARRAY_INIT);
218         Assert.assertEquals(message, 163, GeneratedJavaTokenTypes.TYPE_ARGUMENTS);
219         Assert.assertEquals(message, 164, GeneratedJavaTokenTypes.TYPE_ARGUMENT);
220         Assert.assertEquals(message, 165, GeneratedJavaTokenTypes.TYPE_PARAMETERS);
221         Assert.assertEquals(message, 166, GeneratedJavaTokenTypes.TYPE_PARAMETER);
222         Assert.assertEquals(message, 167, GeneratedJavaTokenTypes.WILDCARD_TYPE);
223         Assert.assertEquals(message, 168, GeneratedJavaTokenTypes.TYPE_UPPER_BOUNDS);
224         Assert.assertEquals(message, 169, GeneratedJavaTokenTypes.TYPE_LOWER_BOUNDS);
225         Assert.assertEquals(message, 170, GeneratedJavaTokenTypes.AT);
226         Assert.assertEquals(message, 171, GeneratedJavaTokenTypes.ELLIPSIS);
227         Assert.assertEquals(message, 172, GeneratedJavaTokenTypes.GENERIC_START);
228         Assert.assertEquals(message, 173, GeneratedJavaTokenTypes.GENERIC_END);
229         Assert.assertEquals(message, 174, GeneratedJavaTokenTypes.TYPE_EXTENSION_AND);
230         Assert.assertEquals(message, 175, GeneratedJavaTokenTypes.DO_WHILE);
231         Assert.assertEquals(message, 176, GeneratedJavaTokenTypes.RESOURCE_SPECIFICATION);
232         Assert.assertEquals(message, 177, GeneratedJavaTokenTypes.RESOURCES);
233         Assert.assertEquals(message, 178, GeneratedJavaTokenTypes.RESOURCE);
234         Assert.assertEquals(message, 179, GeneratedJavaTokenTypes.DOUBLE_COLON);
235         Assert.assertEquals(message, 180, GeneratedJavaTokenTypes.METHOD_REF);
236         Assert.assertEquals(message, 181, GeneratedJavaTokenTypes.LAMBDA);
237         Assert.assertEquals(message, 182, GeneratedJavaTokenTypes.BLOCK_COMMENT_END);
238         Assert.assertEquals(message, 183, GeneratedJavaTokenTypes.COMMENT_CONTENT);
239         Assert.assertEquals(message, 184, GeneratedJavaTokenTypes.SINGLE_LINE_COMMENT_CONTENT);
240         Assert.assertEquals(message, 185, GeneratedJavaTokenTypes.BLOCK_COMMENT_CONTENT);
241         Assert.assertEquals(message, 186, GeneratedJavaTokenTypes.STD_ESC);
242         Assert.assertEquals(message, 187, GeneratedJavaTokenTypes.BINARY_DIGIT);
243         Assert.assertEquals(message, 188, GeneratedJavaTokenTypes.ID_START);
244         Assert.assertEquals(message, 189, GeneratedJavaTokenTypes.ID_PART);
245         Assert.assertEquals(message, 190, GeneratedJavaTokenTypes.INT_LITERAL);
246         Assert.assertEquals(message, 191, GeneratedJavaTokenTypes.LONG_LITERAL);
247         Assert.assertEquals(message, 192, GeneratedJavaTokenTypes.FLOAT_LITERAL);
248         Assert.assertEquals(message, 193, GeneratedJavaTokenTypes.DOUBLE_LITERAL);
249         Assert.assertEquals(message, 194, GeneratedJavaTokenTypes.HEX_FLOAT_LITERAL);
250         Assert.assertEquals(message, 195, GeneratedJavaTokenTypes.HEX_DOUBLE_LITERAL);
251         Assert.assertEquals(message, 196, GeneratedJavaTokenTypes.SIGNED_INTEGER);
252         Assert.assertEquals(message, 197, GeneratedJavaTokenTypes.BINARY_EXPONENT);
253         // Read JavaDoc before changing
254         Assert.assertEquals("all tokens must be added to list in"
255                 + " 'GeneratedJavaTokenTypesTest' and verified"
256                 + " that their old numbering didn't change", 196,
257                 GeneratedJavaTokenTypes.class.getDeclaredFields().length);
258     }
259 }