View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.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   * Contains utility methods for tokens.
41   *
42   */
43  public final class TokenUtil {
44  
45      /** Maps from a token name to value. */
46      private static final Map<String, Integer> TOKEN_NAME_TO_VALUE;
47      /** Maps from a token value to name. */
48      private static final Map<Integer, String> TOKEN_VALUE_TO_NAME;
49  
50      /** Array of all token IDs. */
51      private static final int[] TOKEN_IDS;
52  
53      /** Format for exception message when getting token by given id. */
54      private static final String TOKEN_ID_EXCEPTION_FORMAT = "unknown TokenTypes id '%s'";
55  
56      /** Format for exception message when getting token by given name. */
57      private static final String TOKEN_NAME_EXCEPTION_FORMAT = "unknown TokenTypes value '%s'";
58  
59      // initialise the constants
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      /** Stop instances being created. **/
67      private TokenUtil() {
68      }
69  
70      /**
71       * Gets the IDENT token from the given AST node.
72       *
73       * <p>This method must only be used for AST types where the IDENT token
74       * is guaranteed to NEVER be null. The known types are:
75       * <ul>
76       * <li>{@link TokenTypes#METHOD_DEF}</li>
77       * <li>{@link TokenTypes#CLASS_DEF}</li>
78       * <li>{@link TokenTypes#VARIABLE_DEF}</li>
79       * <li>{@link TokenTypes#PARAMETER_DEF}, <b>but not receiver parameter</b>.</li>
80       * <li>{@link TokenTypes#INTERFACE_DEF}</li>
81       * <li>{@link TokenTypes#ENUM_DEF}</li>
82       * <li>{@link TokenTypes#ANNOTATION_DEF}</li>
83       * <li>{@link TokenTypes#RECORD_DEF}</li>
84       * <li>{@link TokenTypes#LITERAL_CATCH}</li>
85       * </ul>
86       *
87       * @param ast the AST node.
88       * @return the IDENT token (not null).
89       */
90      public static DetailAST getIdent(DetailAST ast) {
91          return NullUtil.notNull(ast.findFirstToken(TokenTypes.IDENT));
92      }
93  
94      /**
95       * Gets the value of a static or instance field of type int or of another primitive type
96       * convertible to type int via a widening conversion. Does not throw any checked exceptions.
97       *
98       * @param field from which the int should be extracted
99       * @param object to extract the int value from
100      * @return the value of the field converted to type int
101      * @throws IllegalStateException if this Field object is enforcing Java language access control
102      *         and the underlying field is inaccessible
103      * @see Field#getInt(Object)
104      */
105     public static int getIntFromField(Field field, Object object) {
106         try {
107             return field.getInt(object);
108         }
109         catch (final IllegalAccessException exception) {
110             throw new IllegalStateException(exception);
111         }
112     }
113 
114     /**
115      * Creates a map of 'field name' to 'field value' from all {@code public} {@code int} fields
116      * of a class.
117      *
118      * @param cls source class
119      * @return unmodifiable name to value map
120      */
121     public static Map<String, Integer> nameToValueMapFromPublicIntFields(Class<?> cls) {
122         return Arrays.stream(cls.getDeclaredFields())
123             .filter(fld -> Modifier.isPublic(fld.getModifiers()) && fld.getType() == Integer.TYPE)
124             .collect(Collectors.toUnmodifiableMap(
125                 Field::getName, fld -> getIntFromField(fld, null))
126             );
127     }
128 
129     /**
130      * Inverts a given map by exchanging each entry's key and value.
131      *
132      * @param map source map
133      * @return inverted map
134      */
135     public static Map<Integer, String> invertMap(Map<String, Integer> map) {
136         return map.entrySet().stream()
137             .collect(Collectors.toUnmodifiableMap(Map.Entry::getValue, Map.Entry::getKey));
138     }
139 
140     /**
141      * Get total number of TokenTypes.
142      *
143      * @return total number of TokenTypes.
144      */
145     public static int getTokenTypesTotalNumber() {
146         return TOKEN_IDS.length;
147     }
148 
149     /**
150      * Get all token IDs that are available in TokenTypes.
151      *
152      * @return array of token IDs
153      */
154     public static int[] getAllTokenIds() {
155         final int[] safeCopy = new int[TOKEN_IDS.length];
156         System.arraycopy(TOKEN_IDS, 0, safeCopy, 0, TOKEN_IDS.length);
157         return safeCopy;
158     }
159 
160     /**
161      * Returns the name of a token for a given ID.
162      *
163      * @param id the ID of the token name to get
164      * @return a token name
165      * @throws IllegalArgumentException when id is not valid
166      */
167     public static String getTokenName(int id) {
168         final String name = TOKEN_VALUE_TO_NAME.get(id);
169         if (name == null) {
170             throw new IllegalArgumentException(
171                 String.format(Locale.ROOT, TOKEN_ID_EXCEPTION_FORMAT, id));
172         }
173         return name;
174     }
175 
176     /**
177      * Returns the ID of a token for a given name.
178      *
179      * @param name the name of the token ID to get
180      * @return a token ID
181      * @throws IllegalArgumentException when id is null
182      */
183     public static int getTokenId(String name) {
184         final Integer id = TOKEN_NAME_TO_VALUE.get(name);
185         if (id == null) {
186             throw new IllegalArgumentException(
187                 String.format(Locale.ROOT, TOKEN_NAME_EXCEPTION_FORMAT, name));
188         }
189         return id;
190     }
191 
192     /**
193      * Returns the short description of a token for a given name.
194      *
195      * @param name the name of the token ID to get
196      * @return a short description
197      * @throws IllegalArgumentException when name is unknown
198      */
199     public static String getShortDescription(String name) {
200         if (!TOKEN_NAME_TO_VALUE.containsKey(name)) {
201             throw new IllegalArgumentException(
202                 String.format(Locale.ROOT, TOKEN_NAME_EXCEPTION_FORMAT, name));
203         }
204 
205         final String tokenTypes =
206             "com.puppycrawl.tools.checkstyle.api.tokentypes";
207         final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
208         return bundle.getString(name);
209     }
210 
211     /**
212      * Is argument comment-related type (SINGLE_LINE_COMMENT,
213      * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
214      *
215      * @param type
216      *        token type.
217      * @return true if type is comment-related type.
218      */
219     public static boolean isCommentType(int type) {
220         return type == TokenTypes.SINGLE_LINE_COMMENT
221                 || type == TokenTypes.BLOCK_COMMENT_BEGIN
222                 || type == TokenTypes.BLOCK_COMMENT_END
223                 || type == TokenTypes.COMMENT_CONTENT;
224     }
225 
226     /**
227      * Is argument comment-related type name (SINGLE_LINE_COMMENT,
228      * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
229      *
230      * @param type
231      *        token type name.
232      * @return true if type is comment-related type name.
233      */
234     public static boolean isCommentType(String type) {
235         return isCommentType(getTokenId(type));
236     }
237 
238     /**
239      * Finds the first {@link Optional} child token of {@link DetailAST} root node
240      * which matches the given predicate.
241      *
242      * @param root root node.
243      * @param predicate predicate.
244      * @return {@link Optional} of {@link DetailAST} node which matches the predicate.
245      */
246     public static Optional<DetailAST> findFirstTokenByPredicate(DetailAST root,
247                                                                 Predicate<DetailAST> predicate) {
248         Optional<DetailAST> result = Optional.empty();
249         for (DetailAST ast = root.getFirstChild(); ast != null; ast = ast.getNextSibling()) {
250             if (predicate.test(ast)) {
251                 result = Optional.of(ast);
252                 break;
253             }
254         }
255         return result;
256     }
257 
258     /**
259      * Performs an action for each child of {@link DetailAST} root node
260      * which matches the given token type.
261      *
262      * @param root root node.
263      * @param type token type to match.
264      * @param action action to perform on the nodes.
265      */
266     public static void forEachChild(DetailAST root, int type, Consumer<DetailAST> action) {
267         for (DetailAST ast = root.getFirstChild(); ast != null; ast = ast.getNextSibling()) {
268             if (ast.getType() == type) {
269                 action.accept(ast);
270             }
271         }
272     }
273 
274     /**
275      * Determines if two ASTs are on the same line.
276      *
277      * @param ast1   the first AST
278      * @param ast2   the second AST
279      *
280      * @return true if they are on the same line.
281      */
282     public static boolean areOnSameLine(DetailAST ast1, DetailAST ast2) {
283         return ast1.getLineNo() == ast2.getLineNo();
284     }
285 
286     /**
287      * Is type declaration token type (CLASS_DEF, INTERFACE_DEF,
288      * ANNOTATION_DEF, ENUM_DEF, RECORD_DEF).
289      *
290      * @param type
291      *        token type.
292      * @return true if type is type declaration token type.
293      */
294     public static boolean isTypeDeclaration(int type) {
295         return type == TokenTypes.CLASS_DEF
296                 || type == TokenTypes.INTERFACE_DEF
297                 || type == TokenTypes.ANNOTATION_DEF
298                 || type == TokenTypes.ENUM_DEF
299                 || type == TokenTypes.RECORD_DEF;
300     }
301 
302     /**
303      * Determines if the token type belongs to the given types.
304      *
305      * @param type the Token Type to check
306      * @param types the acceptable types
307      *
308      * @return true if type matches one of the given types.
309      */
310     public static boolean isOfType(int type, int... types) {
311         boolean matching = false;
312         for (int tokenType : types) {
313             if (tokenType == type) {
314                 matching = true;
315                 break;
316             }
317         }
318         return matching;
319     }
320 
321     /**
322      * Determines if the token type belongs to the given types.
323      *
324      * @param type the Token Type to check
325      * @param types the acceptable types
326      *
327      * @return true if type matches one of the given types.
328      */
329     public static boolean isOfType(int type, Set<Integer> types) {
330         return types.contains(type);
331     }
332 
333     /**
334      * Determines if the AST belongs to the given types.
335      *
336      * @param ast the AST node to check
337      * @param types the acceptable types
338      *
339      * @return true if type matches one of the given types.
340      */
341     public static boolean isOfType(DetailAST ast, int... types) {
342         return ast != null && isOfType(ast.getType(), types);
343     }
344 
345     /**
346      * Determines if given AST is a root node, i.e. if the type
347      * of the token we are checking is {@code COMPILATION_UNIT}.
348      *
349      * @param ast AST to check
350      * @return true if AST is a root node
351      */
352     public static boolean isRootNode(DetailAST ast) {
353         return ast.getType() == TokenTypes.COMPILATION_UNIT;
354     }
355 
356     /**
357      * Checks if a token type is a literal true or false.
358      *
359      * @param tokenType the TokenType
360      * @return true if tokenType is LITERAL_TRUE or LITERAL_FALSE
361      */
362     public static boolean isBooleanLiteralType(final int tokenType) {
363         final boolean isTrue = tokenType == TokenTypes.LITERAL_TRUE;
364         final boolean isFalse = tokenType == TokenTypes.LITERAL_FALSE;
365         return isTrue || isFalse;
366     }
367 
368     /**
369      * Creates a new {@code BitSet} from array of tokens.
370      *
371      * @param tokens to initialize the BitSet
372      * @return tokens as BitSet
373      */
374     public static BitSet asBitSet(int... tokens) {
375         return IntStream.of(tokens)
376                 .collect(BitSet::new, BitSet::set, BitSet::or);
377     }
378 
379     /**
380      * Creates a new {@code BitSet} from array of tokens.
381      *
382      * @param tokens to initialize the BitSet
383      * @return tokens as BitSet
384      */
385     public static BitSet asBitSet(String... tokens) {
386         return Arrays.stream(tokens)
387                 .map(String::trim)
388                 .filter(Predicate.not(String::isEmpty))
389                 .mapToInt(TokenUtil::getTokenId)
390                 .collect(BitSet::new, BitSet::set, BitSet::or);
391     }
392 
393 }