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