Class TokenUtil
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.utils.TokenUtil
-
public final class TokenUtil extends Object
Contains utility methods for tokens.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<DetailAST>findFirstTokenByPredicate(DetailAST root, Predicate<DetailAST> predicate)static voidforEachChild(DetailAST root, int type, Consumer<DetailAST> action)Performs an action for each child ofDetailASTroot node which matches the given token type.static int[]getAllTokenIds()Get all token IDs that are available in TokenTypes.static intgetIntFromField(Field field, Object object)Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.static StringgetShortDescription(String name)Returns the short description of a token for a given name.static intgetTokenId(String name)Returns the ID of a token for a given name.static StringgetTokenName(int id)Returns the name of a token for a given ID.static intgetTokenTypesTotalNumber()Get total number of TokenTypes.static booleanisCommentType(int type)Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).static booleanisCommentType(String type)Is argument comment-related type name (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).static Map<String,Integer>nameToValueMapFromPublicIntFields(Class<?> cls)Creates a map of 'field name' to 'field value' from allpublicintfields of a class.static String[]valueToNameArrayFromNameToValueMap(Map<String,Integer> map)Creates an array of map keys for quick value-to-name lookup for the map.
-
-
-
Method Detail
-
getIntFromField
public static int getIntFromField(Field field, Object object)
Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion. Does not throw any checked exceptions.- Parameters:
field- from which the int should be extractedobject- to extract the int value from- Returns:
- the value of the field converted to type int
- Throws:
IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible- See Also:
Field.getInt(Object)
-
nameToValueMapFromPublicIntFields
public static Map<String,Integer> nameToValueMapFromPublicIntFields(Class<?> cls)
Creates a map of 'field name' to 'field value' from allpublicintfields of a class.- Parameters:
cls- source class- Returns:
- unmodifiable name to value map
-
valueToNameArrayFromNameToValueMap
public static String[] valueToNameArrayFromNameToValueMap(Map<String,Integer> map)
Creates an array of map keys for quick value-to-name lookup for the map.- Parameters:
map- source map- Returns:
- array of map keys
-
getTokenTypesTotalNumber
public static int getTokenTypesTotalNumber()
Get total number of TokenTypes.- Returns:
- total number of TokenTypes.
-
getAllTokenIds
public static int[] getAllTokenIds()
Get all token IDs that are available in TokenTypes.- Returns:
- array of token IDs
-
getTokenName
public static String getTokenName(int id)
Returns the name of a token for a given ID.- Parameters:
id- the ID of the token name to get- Returns:
- a token name
-
getTokenId
public static int getTokenId(String name)
Returns the ID of a token for a given name.- Parameters:
name- the name of the token ID to get- Returns:
- a token ID
-
getShortDescription
public static String getShortDescription(String name)
Returns the short description of a token for a given name.- Parameters:
name- the name of the token ID to get- Returns:
- a short description
-
isCommentType
public static boolean isCommentType(int type)
Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).- Parameters:
type- token type.- Returns:
- true if type is comment-related type.
-
isCommentType
public static boolean isCommentType(String type)
Is argument comment-related type name (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).- Parameters:
type- token type name.- Returns:
- true if type is comment-related type name.
-
findFirstTokenByPredicate
public static Optional<DetailAST> findFirstTokenByPredicate(DetailAST root, Predicate<DetailAST> predicate)
-
-