Class TokenUtil
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.utils.TokenUtil
-
-
Field Summary
Fields Modifier and Type Field Description private static String
TOKEN_ID_EXCEPTION_FORMAT
Format for exception message when getting token by given id.private static int[]
TOKEN_IDS
Array of all token IDs.private static String
TOKEN_NAME_EXCEPTION_FORMAT
Format for exception message when getting token by given name.private static Map<String,Integer>
TOKEN_NAME_TO_VALUE
Maps from a token name to value.private static Map<Integer,String>
TOKEN_VALUE_TO_NAME
Maps from a token value to name.
-
Constructor Summary
Constructors Modifier Constructor Description private
TokenUtil()
Stop instances being created.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areOnSameLine(DetailAST ast1, DetailAST ast2)
Determines if two ASTs are on the same line.static BitSet
asBitSet(int... tokens)
Creates a newBitSet
from array of tokens.static BitSet
asBitSet(String... tokens)
Creates a newBitSet
from array of tokens.static Optional<DetailAST>
findFirstTokenByPredicate(DetailAST root, Predicate<DetailAST> predicate)
static void
forEachChild(DetailAST root, int type, Consumer<DetailAST> action)
Performs an action for each child ofDetailAST
root node which matches the given token type.static int[]
getAllTokenIds()
Get all token IDs that are available in TokenTypes.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.static String
getShortDescription(String name)
Returns the short description of a token for a given name.static int
getTokenId(String name)
Returns the ID of a token for a given name.static String
getTokenName(int id)
Returns the name of a token for a given ID.static int
getTokenTypesTotalNumber()
Get total number of TokenTypes.static Map<Integer,String>
invertMap(Map<String,Integer> map)
Inverts a given map by exchanging each entry's key and value.static boolean
isBooleanLiteralType(int tokenType)
Checks if a token type is a literal true or false.static boolean
isCommentType(int type)
Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).static boolean
isCommentType(String type)
Is argument comment-related type name (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).static boolean
isOfType(int type, int... types)
Determines if the token type belongs to the given types.static boolean
isOfType(int type, Set<Integer> types)
Determines if the token type belongs to the given types.static boolean
isOfType(DetailAST ast, int... types)
Determines if the AST belongs to the given types.static boolean
isRootNode(DetailAST ast)
Determines if given AST is a root node, i.e. if the type of the token we are checking isCOMPILATION_UNIT
.static boolean
isTypeDeclaration(int type)
Is type declaration token type (CLASS_DEF, INTERFACE_DEF, ANNOTATION_DEF, ENUM_DEF, RECORD_DEF).static Map<String,Integer>
nameToValueMapFromPublicIntFields(Class<?> cls)
Creates a map of 'field name' to 'field value' from allpublic
int
fields of a class.
-
-
-
Field Detail
-
TOKEN_NAME_TO_VALUE
private static final Map<String,Integer> TOKEN_NAME_TO_VALUE
Maps from a token name to value.
-
TOKEN_VALUE_TO_NAME
private static final Map<Integer,String> TOKEN_VALUE_TO_NAME
Maps from a token value to name.
-
TOKEN_IDS
private static final int[] TOKEN_IDS
Array of all token IDs.
-
TOKEN_ID_EXCEPTION_FORMAT
private static final String TOKEN_ID_EXCEPTION_FORMAT
Format for exception message when getting token by given id.- See Also:
- Constant Field Values
-
TOKEN_NAME_EXCEPTION_FORMAT
private static final String TOKEN_NAME_EXCEPTION_FORMAT
Format for exception message when getting token by given name.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TokenUtil
private TokenUtil()
Stop instances being created.
-
-
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 allpublic
int
fields of a class.- Parameters:
cls
- source class- Returns:
- unmodifiable name to value map
-
invertMap
public static Map<Integer,String> invertMap(Map<String,Integer> map)
Inverts a given map by exchanging each entry's key and value.- Parameters:
map
- source map- Returns:
- inverted map
-
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
- Throws:
IllegalArgumentException
- when id is not valid
-
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
- Throws:
IllegalArgumentException
- when id is null
-
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
- Throws:
IllegalArgumentException
- when name is unknown
-
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)
-
forEachChild
public static void forEachChild(DetailAST root, int type, Consumer<DetailAST> action)
Performs an action for each child ofDetailAST
root node which matches the given token type.- Parameters:
root
- root node.type
- token type to match.action
- action to perform on the nodes.
-
areOnSameLine
public static boolean areOnSameLine(DetailAST ast1, DetailAST ast2)
Determines if two ASTs are on the same line.- Parameters:
ast1
- the first ASTast2
- the second AST- Returns:
- true if they are on the same line.
-
isTypeDeclaration
public static boolean isTypeDeclaration(int type)
Is type declaration token type (CLASS_DEF, INTERFACE_DEF, ANNOTATION_DEF, ENUM_DEF, RECORD_DEF).- Parameters:
type
- token type.- Returns:
- true if type is type declaration token type.
-
isOfType
public static boolean isOfType(int type, int... types)
Determines if the token type belongs to the given types.- Parameters:
type
- the Token Type to checktypes
- the acceptable types- Returns:
- true if type matches one of the given types.
-
isOfType
public static boolean isOfType(int type, Set<Integer> types)
Determines if the token type belongs to the given types.- Parameters:
type
- the Token Type to checktypes
- the acceptable types- Returns:
- true if type matches one of the given types.
-
isOfType
public static boolean isOfType(DetailAST ast, int... types)
Determines if the AST belongs to the given types.- Parameters:
ast
- the AST node to checktypes
- the acceptable types- Returns:
- true if type matches one of the given types.
-
isRootNode
public static boolean isRootNode(DetailAST ast)
Determines if given AST is a root node, i.e. if the type of the token we are checking isCOMPILATION_UNIT
.- Parameters:
ast
- AST to check- Returns:
- true if AST is a root node
-
isBooleanLiteralType
public static boolean isBooleanLiteralType(int tokenType)
Checks if a token type is a literal true or false.- Parameters:
tokenType
- the TokenType- Returns:
- true if tokenType is LITERAL_TRUE or LITERAL_FALSE
-
asBitSet
public static BitSet asBitSet(int... tokens)
Creates a newBitSet
from array of tokens.- Parameters:
tokens
- to initialize the BitSet- Returns:
- tokens as BitSet
-
-