Class TypeUtil

java.lang.Object
org.eclipse.jetty.util.TypeUtil

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

  • Method Details

    • asList

      public static <T> 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.
    • listIteratorAtEnd

      public static <T> ListIterator<T> listIteratorAtEnd(List<T> list)

      Returns a ListIterator positioned at the last item in a list.

      Type Parameters:
      T - the element type
      Parameters:
      list - the list
      Returns:
      A ListIterator positioned at the last item of the list.
    • fromName

      public static Class<?> fromName(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..
    • toShortName

      public static String toShortName(Class<?> type)
    • toClassReference

      public static String toClassReference(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 String toClassReference(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 Object valueOf(Class<?> type, 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 Object valueOf(String type, 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(String s, int offset, int length, int base) throws 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:
      NumberFormatException - if the string cannot be parsed
    • parseInt

      public static int parseInt(byte[] b, int offset, int length, int base) throws 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:
      NumberFormatException - if the array cannot be parsed into an integer
    • toString

      public static 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.
    • isHex

      public static boolean isHex(String str, int offset, int len)
    • toHex

      public static void toHex(byte b, Appendable buf)
    • toHex

      public static void toHex(int value, Appendable buf) throws IOException
      Throws:
      IOException
    • toHex

      public static void toHex(long value, Appendable buf) throws IOException
      Throws:
      IOException
    • dump

      public static void dump(Class<?> c)
    • dump

      public static void dump(ClassLoader cl)
    • isTrue

      public static boolean isTrue(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(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 URI getLocationOfClass(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 URI getSystemClassLoaderLocation(Class<?> clazz)
    • getClassLoaderLocation

      public static URI getClassLoaderLocation(Class<?> clazz)
    • getClassLoaderLocation

      public static URI getClassLoaderLocation(Class<?> clazz, ClassLoader loader)
    • getCodeSourceLocation

      public static URI getCodeSourceLocation(Class<?> clazz)
    • getModuleLocation

      public static URI getModuleLocation(Class<?> clazz)
    • concat

      public static <T> Iterator<T> concat(Iterator<T> i1, Iterator<T> i2)
    • serviceStream

      public static <T> Stream<T> serviceStream(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> Stream<ServiceLoader.Provider<T>> serviceProviderStream(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
    • truePredicate

      public static <T> Predicate<T> truePredicate()
      A Predicate that is always true, with optimized and/or/not methods.
      Type Parameters:
      T - The type of the predicate test
      Returns:
      true
    • falsePredicate

      public static <T> Predicate<T> falsePredicate()
      A Predicate that is always false, with optimized and/or/not methods.
      Type Parameters:
      T - The type of the predicate test
      Returns:
      true
    • ceilToNextPowerOfTwo

      public static int ceilToNextPowerOfTwo(int value)
      Get the next highest power of two
      Parameters:
      value - An integer
      Returns:
      a power of two that is greater than or equal to value
    • isDeclaredMethodOn

      public static boolean isDeclaredMethodOn(Object object, String methodName, Class<?>... args)
      Test is a method has been declared on the class of an instance
      Parameters:
      object - The object to check
      methodName - The method name
      args - The arguments to the method
      Returns:
      true iff Class.getDeclaredMethod(String, Class[]) can be called on the Class of the object, without throwing NoSuchMethodException.