1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.utils;
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Modifier;
24 import java.util.Arrays;
25 import java.util.BitSet;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.ResourceBundle;
30 import java.util.Set;
31 import java.util.function.Consumer;
32 import java.util.function.Predicate;
33 import java.util.stream.Collectors;
34 import java.util.stream.IntStream;
35
36 import com.puppycrawl.tools.checkstyle.api.DetailAST;
37 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38
39
40
41
42
43 public final class TokenUtil {
44
45
46 private static final Map<String, Integer> TOKEN_NAME_TO_VALUE;
47
48 private static final Map<Integer, String> TOKEN_VALUE_TO_NAME;
49
50
51 private static final int[] TOKEN_IDS;
52
53
54 private static final String TOKEN_ID_EXCEPTION_FORMAT = "unknown TokenTypes id '%s'";
55
56
57 private static final String TOKEN_NAME_EXCEPTION_FORMAT = "unknown TokenTypes value '%s'";
58
59
60 static {
61 TOKEN_NAME_TO_VALUE = nameToValueMapFromPublicIntFields(TokenTypes.class);
62 TOKEN_VALUE_TO_NAME = invertMap(TOKEN_NAME_TO_VALUE);
63 TOKEN_IDS = TOKEN_NAME_TO_VALUE.values().stream().mapToInt(Integer::intValue).toArray();
64 }
65
66
67 private TokenUtil() {
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public static int getIntFromField(Field field, Object object) {
82 try {
83 return field.getInt(object);
84 }
85 catch (final IllegalAccessException exception) {
86 throw new IllegalStateException(exception);
87 }
88 }
89
90
91
92
93
94
95
96
97 public static Map<String, Integer> nameToValueMapFromPublicIntFields(Class<?> cls) {
98 return Arrays.stream(cls.getDeclaredFields())
99 .filter(fld -> Modifier.isPublic(fld.getModifiers()) && fld.getType() == Integer.TYPE)
100 .collect(Collectors.toUnmodifiableMap(
101 Field::getName, fld -> getIntFromField(fld, null))
102 );
103 }
104
105
106
107
108
109
110
111 public static Map<Integer, String> invertMap(Map<String, Integer> map) {
112 return map.entrySet().stream()
113 .collect(Collectors.toUnmodifiableMap(Map.Entry::getValue, Map.Entry::getKey));
114 }
115
116
117
118
119
120
121 public static int getTokenTypesTotalNumber() {
122 return TOKEN_IDS.length;
123 }
124
125
126
127
128
129
130 public static int[] getAllTokenIds() {
131 final int[] safeCopy = new int[TOKEN_IDS.length];
132 System.arraycopy(TOKEN_IDS, 0, safeCopy, 0, TOKEN_IDS.length);
133 return safeCopy;
134 }
135
136
137
138
139
140
141
142
143 public static String getTokenName(int id) {
144 final String name = TOKEN_VALUE_TO_NAME.get(id);
145 if (name == null) {
146 throw new IllegalArgumentException(
147 String.format(Locale.ROOT, TOKEN_ID_EXCEPTION_FORMAT, id));
148 }
149 return name;
150 }
151
152
153
154
155
156
157
158
159 public static int getTokenId(String name) {
160 final Integer id = TOKEN_NAME_TO_VALUE.get(name);
161 if (id == null) {
162 throw new IllegalArgumentException(
163 String.format(Locale.ROOT, TOKEN_NAME_EXCEPTION_FORMAT, name));
164 }
165 return id;
166 }
167
168
169
170
171
172
173
174
175 public static String getShortDescription(String name) {
176 if (!TOKEN_NAME_TO_VALUE.containsKey(name)) {
177 throw new IllegalArgumentException(
178 String.format(Locale.ROOT, TOKEN_NAME_EXCEPTION_FORMAT, name));
179 }
180
181 final String tokenTypes =
182 "com.puppycrawl.tools.checkstyle.api.tokentypes";
183 final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
184 return bundle.getString(name);
185 }
186
187
188
189
190
191
192
193
194
195 public static boolean isCommentType(int type) {
196 return type == TokenTypes.SINGLE_LINE_COMMENT
197 || type == TokenTypes.BLOCK_COMMENT_BEGIN
198 || type == TokenTypes.BLOCK_COMMENT_END
199 || type == TokenTypes.COMMENT_CONTENT;
200 }
201
202
203
204
205
206
207
208
209
210 public static boolean isCommentType(String type) {
211 return isCommentType(getTokenId(type));
212 }
213
214
215
216
217
218
219
220
221
222 public static Optional<DetailAST> findFirstTokenByPredicate(DetailAST root,
223 Predicate<DetailAST> predicate) {
224 Optional<DetailAST> result = Optional.empty();
225 for (DetailAST ast = root.getFirstChild(); ast != null; ast = ast.getNextSibling()) {
226 if (predicate.test(ast)) {
227 result = Optional.of(ast);
228 break;
229 }
230 }
231 return result;
232 }
233
234
235
236
237
238
239
240
241
242 public static void forEachChild(DetailAST root, int type, Consumer<DetailAST> action) {
243 for (DetailAST ast = root.getFirstChild(); ast != null; ast = ast.getNextSibling()) {
244 if (ast.getType() == type) {
245 action.accept(ast);
246 }
247 }
248 }
249
250
251
252
253
254
255
256
257
258 public static boolean areOnSameLine(DetailAST ast1, DetailAST ast2) {
259 return ast1.getLineNo() == ast2.getLineNo();
260 }
261
262
263
264
265
266
267
268
269
270 public static boolean isTypeDeclaration(int type) {
271 return type == TokenTypes.CLASS_DEF
272 || type == TokenTypes.INTERFACE_DEF
273 || type == TokenTypes.ANNOTATION_DEF
274 || type == TokenTypes.ENUM_DEF
275 || type == TokenTypes.RECORD_DEF;
276 }
277
278
279
280
281
282
283
284
285
286 public static boolean isOfType(int type, int... types) {
287 boolean matching = false;
288 for (int tokenType : types) {
289 if (tokenType == type) {
290 matching = true;
291 break;
292 }
293 }
294 return matching;
295 }
296
297
298
299
300
301
302
303
304
305 public static boolean isOfType(int type, Set<Integer> types) {
306 return types.contains(type);
307 }
308
309
310
311
312
313
314
315
316
317 public static boolean isOfType(DetailAST ast, int... types) {
318 return ast != null && isOfType(ast.getType(), types);
319 }
320
321
322
323
324
325
326
327
328 public static boolean isRootNode(DetailAST ast) {
329 return ast.getType() == TokenTypes.COMPILATION_UNIT;
330 }
331
332
333
334
335
336
337
338 public static boolean isBooleanLiteralType(final int tokenType) {
339 final boolean isTrue = tokenType == TokenTypes.LITERAL_TRUE;
340 final boolean isFalse = tokenType == TokenTypes.LITERAL_FALSE;
341 return isTrue || isFalse;
342 }
343
344
345
346
347
348
349
350 public static BitSet asBitSet(int... tokens) {
351 return IntStream.of(tokens)
352 .collect(BitSet::new, BitSet::set, BitSet::or);
353 }
354
355
356
357
358
359
360
361 public static BitSet asBitSet(String... tokens) {
362 return Arrays.stream(tokens)
363 .map(String::trim)
364 .filter(Predicate.not(String::isEmpty))
365 .mapToInt(TokenUtil::getTokenId)
366 .collect(BitSet::new, BitSet::set, BitSet::or);
367 }
368
369 }