Class TypeUtil


  • public class TypeUtil
    extends java.lang.Object
    TYPE Utilities. Provides various static utility methods for manipulating types and their string representations.
    Since:
    Jetty 4.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CR  
      static int LF  
      static java.lang.Class<?>[] NO_ARGS  
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> asList​(T[] a)
      Array to List.
      static <T> java.util.Iterator<T> concat​(java.util.Iterator<T> i1, java.util.Iterator<T> i2)  
      static byte convertHexDigit​(byte c)  
      static int convertHexDigit​(char c)  
      static int convertHexDigit​(int c)  
      static void dump​(java.lang.Class<?> c)  
      static void dump​(java.lang.ClassLoader cl)  
      static byte[] fromHexString​(java.lang.String s)  
      static java.lang.Class<?> fromName​(java.lang.String name)
      Class from a canonical name for a type.
      static java.net.URI getClassLoaderLocation​(java.lang.Class<?> clazz)  
      static java.net.URI getClassLoaderLocation​(java.lang.Class<?> clazz, java.lang.ClassLoader loader)  
      static java.net.URI getCodeSourceLocation​(java.lang.Class<?> clazz)  
      static java.net.URI getLocationOfClass​(java.lang.Class<?> clazz)
      Attempt to find the Location of a loaded Class.
      static java.net.URI getModuleLocation​(java.lang.Class<?> clazz)  
      static java.net.URI getSystemClassLoaderLocation​(java.lang.Class<?> clazz)  
      static boolean isFalse​(java.lang.Object o)  
      static boolean isTrue​(java.lang.Object o)  
      static byte[] parseBytes​(java.lang.String s, int base)  
      static int parseInt​(byte[] b, int offset, int length, int base)
      Parse an int from a byte array of ascii characters.
      static int parseInt​(java.lang.String s, int offset, int length, int base)
      Parse an int from a substring.
      static <T> java.util.stream.Stream<java.util.ServiceLoader.Provider<T>> serviceProviderStream​(java.util.ServiceLoader<T> serviceLoader)
      Utility to create a stream which provides the same functionality as ServiceLoader.stream().
      static <T> java.util.stream.Stream<T> serviceStream​(java.util.ServiceLoader<T> serviceLoader)
      Utility method to provide a stream of the service type from a ServiceLoader.
      static java.lang.String toClassReference​(java.lang.Class<?> clazz)
      Return the Classpath / Classloader reference for the provided class file.
      static java.lang.String toClassReference​(java.lang.String className)
      Return the Classpath / Classloader reference for the provided class file.
      static void toHex​(byte b, java.lang.Appendable buf)  
      static void toHex​(int value, java.lang.Appendable buf)  
      static void toHex​(long value, java.lang.Appendable buf)  
      static java.lang.String toHexString​(byte b)  
      static java.lang.String toHexString​(byte[] b)  
      static java.lang.String toHexString​(byte[] b, int offset, int length)  
      static java.lang.String toName​(java.lang.Class<?> type)
      Canonical name for a type.
      static java.lang.String toString​(byte[] bytes, int base)  
      static java.lang.Object valueOf​(java.lang.Class<?> type, java.lang.String value)
      Convert String value to instance.
      static java.lang.Object valueOf​(java.lang.String type, java.lang.String value)
      Convert String value to instance.
      • Methods inherited from class java.lang.Object

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

      • TypeUtil

        public TypeUtil()
    • Method Detail

      • asList

        public static <T> java.util.List<T> asList​(T[] a)
        Array to List.

        Works like Arrays.asList(Object...), but handles null arrays.

        Type Parameters:
        T - the array and list entry type
        Parameters:
        a - the array to convert to a list
        Returns:
        a list backed by the array.
      • fromName

        public static java.lang.Class<?> fromName​(java.lang.String name)
        Class from a canonical name for a type.
        Parameters:
        name - A class or type name.
        Returns:
        A class , which may be a primitive TYPE field..
      • toName

        public static java.lang.String toName​(java.lang.Class<?> type)
        Canonical name for a type.
        Parameters:
        type - A class , which may be a primitive TYPE field.
        Returns:
        Canonical name.
      • toClassReference

        public static java.lang.String toClassReference​(java.lang.Class<?> clazz)
        Return the Classpath / Classloader reference for the provided class file.

        Convenience method for the code

         String ref = myObject.getClass().getName().replace('.','/') + ".class";
         
        Parameters:
        clazz - the class to reference
        Returns:
        the classpath reference syntax for the class file
      • toClassReference

        public static java.lang.String toClassReference​(java.lang.String className)
        Return the Classpath / Classloader reference for the provided class file.

        Convenience method for the code

         String ref = myClassName.replace('.','/') + ".class";
         
        Parameters:
        className - the class to reference
        Returns:
        the classpath reference syntax for the class file
      • valueOf

        public static java.lang.Object valueOf​(java.lang.Class<?> type,
                                               java.lang.String value)
        Convert String value to instance.
        Parameters:
        type - The class of the instance, which may be a primitive TYPE field.
        value - The value as a string.
        Returns:
        The value as an Object.
      • valueOf

        public static java.lang.Object valueOf​(java.lang.String type,
                                               java.lang.String value)
        Convert String value to instance.
        Parameters:
        type - classname or type (eg int)
        value - The value as a string.
        Returns:
        The value as an Object.
      • parseInt

        public static int parseInt​(java.lang.String s,
                                   int offset,
                                   int length,
                                   int base)
                            throws java.lang.NumberFormatException
        Parse an int from a substring. Negative numbers are not handled.
        Parameters:
        s - String
        offset - Offset within string
        length - Length of integer or -1 for remainder of string
        base - base of the integer
        Returns:
        the parsed integer
        Throws:
        java.lang.NumberFormatException - if the string cannot be parsed
      • parseInt

        public static int parseInt​(byte[] b,
                                   int offset,
                                   int length,
                                   int base)
                            throws java.lang.NumberFormatException
        Parse an int from a byte array of ascii characters. Negative numbers are not handled.
        Parameters:
        b - byte array
        offset - Offset within string
        length - Length of integer or -1 for remainder of string
        base - base of the integer
        Returns:
        the parsed integer
        Throws:
        java.lang.NumberFormatException - if the array cannot be parsed into an integer
      • parseBytes

        public static byte[] parseBytes​(java.lang.String s,
                                        int base)
      • toString

        public static java.lang.String toString​(byte[] bytes,
                                                int base)
      • convertHexDigit

        public static byte convertHexDigit​(byte c)
        Parameters:
        c - An ASCII encoded character 0-9 a-f A-F
        Returns:
        The byte value of the character 0-16.
      • convertHexDigit

        public static int convertHexDigit​(char c)
        Parameters:
        c - An ASCII encoded character 0-9 a-f A-F
        Returns:
        The byte value of the character 0-16.
      • convertHexDigit

        public static int convertHexDigit​(int c)
        Parameters:
        c - An ASCII encoded character 0-9 a-f A-F
        Returns:
        The byte value of the character 0-16.
      • toHex

        public static void toHex​(byte b,
                                 java.lang.Appendable buf)
      • toHex

        public static void toHex​(int value,
                                 java.lang.Appendable buf)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • toHex

        public static void toHex​(long value,
                                 java.lang.Appendable buf)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • toHexString

        public static java.lang.String toHexString​(byte b)
      • toHexString

        public static java.lang.String toHexString​(byte[] b)
      • toHexString

        public static java.lang.String toHexString​(byte[] b,
                                                   int offset,
                                                   int length)
      • fromHexString

        public static byte[] fromHexString​(java.lang.String s)
      • dump

        public static void dump​(java.lang.Class<?> c)
      • dump

        public static void dump​(java.lang.ClassLoader cl)
      • isTrue

        public static boolean isTrue​(java.lang.Object o)
        Parameters:
        o - Object to test for true
        Returns:
        True if passed object is not null and is either a Boolean with value true or evaluates to a string that evaluates to true.
      • isFalse

        public static boolean isFalse​(java.lang.Object o)
        Parameters:
        o - Object to test for false
        Returns:
        True if passed object is not null and is either a Boolean with value false or evaluates to a string that evaluates to false.
      • getLocationOfClass

        public static java.net.URI getLocationOfClass​(java.lang.Class<?> clazz)
        Attempt to find the Location of a loaded Class.

        This can be null for primitives, void, and in-memory classes.

        Parameters:
        clazz - the loaded class to find a location for.
        Returns:
        the location as a URI (this is a URI pointing to a holder of the class: a directory, a jar file, a jrt:// resource, etc), or null of no location available.
      • getSystemClassLoaderLocation

        public static java.net.URI getSystemClassLoaderLocation​(java.lang.Class<?> clazz)
      • getClassLoaderLocation

        public static java.net.URI getClassLoaderLocation​(java.lang.Class<?> clazz)
      • getClassLoaderLocation

        public static java.net.URI getClassLoaderLocation​(java.lang.Class<?> clazz,
                                                          java.lang.ClassLoader loader)
      • getCodeSourceLocation

        public static java.net.URI getCodeSourceLocation​(java.lang.Class<?> clazz)
      • getModuleLocation

        public static java.net.URI getModuleLocation​(java.lang.Class<?> clazz)
      • concat

        public static <T> java.util.Iterator<T> concat​(java.util.Iterator<T> i1,
                                                       java.util.Iterator<T> i2)
      • serviceStream

        public static <T> java.util.stream.Stream<T> serviceStream​(java.util.ServiceLoader<T> serviceLoader)
        Utility method to provide a stream of the service type from a ServiceLoader. Log warnings will be given for any ServiceConfigurationErrors which occur when loading or instantiating the services.
        Type Parameters:
        T - the type of the service to load.
        Parameters:
        serviceLoader - the ServiceLoader instance to use.
        Returns:
        a stream of the service type which will not throw ServiceConfigurationError.
      • serviceProviderStream

        public static <T> java.util.stream.Stream<java.util.ServiceLoader.Provider<T>> serviceProviderStream​(java.util.ServiceLoader<T> serviceLoader)
        Utility to create a stream which provides the same functionality as ServiceLoader.stream(). However this also guards the case in which Iterator.hasNext() throws. Any exceptions from the underlying iterator will be cached until the ServiceLoader.Provider.get() is called.
        Type Parameters:
        T - the type of the service to load.
        Parameters:
        serviceLoader - the ServiceLoader instance to use.
        Returns:
        A stream that lazily loads providers for this loader's service