Class TokenUtil


  • public final class TokenUtil
    extends java.lang.Object
    Contains utility methods for tokens.
    • 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 java.util.Optional<DetailAST> findFirstTokenByPredicate​(DetailAST root, java.util.function.Predicate<DetailAST> predicate)
      Finds the first Optional child token of DetailAST root node which matches the given predicate.
      static void forEachChild​(DetailAST root, int type, java.util.function.Consumer<DetailAST> action)
      Performs an action for each child of DetailAST root node which matches the given token type.
      static int[] getAllTokenIds()
      Get all token IDs that are available in TokenTypes.
      static int getIntFromField​(java.lang.reflect.Field field, java.lang.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 java.lang.String getShortDescription​(java.lang.String name)
      Returns the short description of a token for a given name.
      static int getTokenId​(java.lang.String name)
      Returns the ID of a token for a given name.
      static java.lang.String getTokenName​(int id)
      Returns the name of a token for a given ID.
      static int getTokenTypesTotalNumber()
      Get total number of TokenTypes.
      static boolean isCommentType​(int type)
      Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
      static boolean isCommentType​(java.lang.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​(DetailAST ast, int... types)
      Determines if the AST belongs to the given types.
      static boolean isTypeDeclaration​(int type)
      Is type declaration token type (CLASS_DEF, INTERFACE_DEF, ANNOTATION_DEF, ENUM_DEF, RECORD_DEF).
      static java.util.Map<java.lang.String,​java.lang.Integer> nameToValueMapFromPublicIntFields​(java.lang.Class<?> cls)
      Creates a map of 'field name' to 'field value' from all public int fields of a class.
      static java.lang.String[] valueToNameArrayFromNameToValueMap​(java.util.Map<java.lang.String,​java.lang.Integer> map)
      Creates an array of map keys for quick value-to-name lookup for the map.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getIntFromField

        public static int getIntFromField​(java.lang.reflect.Field field,
                                          java.lang.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 extracted
        object - to extract the int value from
        Returns:
        the value of the field converted to type int
        Throws:
        java.lang.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 java.util.Map<java.lang.String,​java.lang.Integer> nameToValueMapFromPublicIntFields​(java.lang.Class<?> cls)
        Creates a map of 'field name' to 'field value' from all public int fields of a class.
        Parameters:
        cls - source class
        Returns:
        unmodifiable name to value map
      • valueToNameArrayFromNameToValueMap

        public static java.lang.String[] valueToNameArrayFromNameToValueMap​(java.util.Map<java.lang.String,​java.lang.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 java.lang.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:
        java.lang.IllegalArgumentException - when id is not valid
      • getTokenId

        public static int getTokenId​(java.lang.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:
        java.lang.IllegalArgumentException - when id is null
      • getShortDescription

        public static java.lang.String getShortDescription​(java.lang.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:
        java.lang.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​(java.lang.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 java.util.Optional<DetailAST> findFirstTokenByPredicate​(DetailAST root,
                                                                              java.util.function.Predicate<DetailAST> predicate)
        Finds the first Optional child token of DetailAST root node which matches the given predicate.
        Parameters:
        root - root node.
        predicate - predicate.
        Returns:
        Optional of DetailAST node which matches the predicate.
      • forEachChild

        public static void forEachChild​(DetailAST root,
                                        int type,
                                        java.util.function.Consumer<DetailAST> action)
        Performs an action for each child of DetailAST 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 AST
        ast2 - 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 check
        types - 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 check
        types - the acceptable types
        Returns:
        true if type matches one of the given types.