Class Types


  • public final class Types
    extends java.lang.Object
    Utility methods for working with generic types.

    Most of these utilities are designed to go along with GeAnTyRef.

    Since:
    4.0.0
    See Also:
    for other tools to work with types, for methods to construct types
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.lang.reflect.Method> allDeclaredMethods​(java.lang.Class<?> clazz)
      Get all declared methods in the provided class and its superclasses and superinterfaces, up to but not including Object.
      static java.util.stream.Stream<java.lang.reflect.Type> allSuperTypes​(java.lang.reflect.Type type)
      Get all supertypes of this object with type parameters.
      static java.util.stream.Stream<java.lang.reflect.Type> allSuperTypesAndInterfaces​(java.lang.reflect.Type type)
      Get all supertypes and interfaces of the provided type.
      static java.lang.reflect.Type box​(java.lang.reflect.Type input)
      Box the input type if it is an unboxed primitive Class.
      static java.lang.reflect.AnnotatedElement combinedAnnotations​(java.lang.reflect.AnnotatedElement... elements)
      Get an element containing the annotations of all the provided elements.
      static @Nullable java.lang.Object defaultValue​(java.lang.Class<?> type)
      Get the default value for a type.
      static boolean isArray​(java.lang.reflect.Type input)
      Get if the provided type is an array type.
      static boolean isBoxedPrimitive​(java.lang.reflect.Type input)
      Get whether or not the provided input type is a boxed primitive type.
      static <T> io.leangen.geantyref.TypeToken<java.util.List<T>> makeList​(java.lang.Class<T> elementType)
      Given an element type, create a new list type.
      static <T> io.leangen.geantyref.TypeToken<java.util.List<T>> makeListType​(io.leangen.geantyref.TypeToken<T> elementType)
      Given an element type, create a new list type.
      static <K,​V>
      io.leangen.geantyref.TypeToken<java.util.Map<K,​V>>
      makeMap​(io.leangen.geantyref.TypeToken<K> key, io.leangen.geantyref.TypeToken<V> value)
      Given an element type, create a new Map type.
      static <K,​V>
      io.leangen.geantyref.TypeToken<java.util.Map<K,​V>>
      makeMap​(io.leangen.geantyref.TypeToken<K> key, java.lang.Class<V> value)
      Given an element type, create a new Map type.
      static <K,​V>
      io.leangen.geantyref.TypeToken<java.util.Map<K,​V>>
      makeMap​(java.lang.Class<K> key, io.leangen.geantyref.TypeToken<V> value)
      Given an element type, create a new Map type.
      static <K,​V>
      io.leangen.geantyref.TypeToken<java.util.Map<K,​V>>
      makeMap​(java.lang.Class<K> key, java.lang.Class<V> value)
      Given an element type, create a new Map type.
      static java.lang.reflect.Type requireCompleteParameters​(java.lang.reflect.Type input)
      Throw an exception if the passed type is raw (missing parameters)..
      static java.lang.reflect.Type unbox​(java.lang.reflect.Type input)
      Unbox the input type if it is a boxed primitive.
      • Methods inherited from class java.lang.Object

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

      • isArray

        public static boolean isArray​(java.lang.reflect.Type input)
        Get if the provided type is an array type.

        Being an array type means that the provided type has a component type.

        Parameters:
        input - input type
        Returns:
        whether the type is an array
        Since:
        4.0.0
      • isBoxedPrimitive

        public static boolean isBoxedPrimitive​(java.lang.reflect.Type input)
        Get whether or not the provided input type is a boxed primitive type.

        This check will not match unboxed primitives.

        Parameters:
        input - type to check
        Returns:
        if type is a boxed primitive
        Since:
        4.0.0
      • unbox

        public static java.lang.reflect.Type unbox​(java.lang.reflect.Type input)
        Unbox the input type if it is a boxed primitive.
        Parameters:
        input - input type
        Returns:
        the unboxed version of the input type, or the input type if it was already non-primitive
        Since:
        4.0.0
      • defaultValue

        public static @Nullable java.lang.Object defaultValue​(java.lang.Class<?> type)
        Get the default value for a type.

        For all reference types, this is null. For all primitive types, this is equivalent to their defined default value.

        Parameters:
        type - the type to get a default value for
        Returns:
        the default value, or null for reference types
        Since:
        4.1.0
      • box

        public static java.lang.reflect.Type box​(java.lang.reflect.Type input)
        Box the input type if it is an unboxed primitive Class.
        Parameters:
        input - input type
        Returns:
        the unboxed version of the input type, or the input type if it was already a primitive, or had no primitive equivalent
        Since:
        4.0.0
      • makeList

        public static <T> io.leangen.geantyref.TypeToken<java.util.List<T>> makeList​(java.lang.Class<T> elementType)
        Given an element type, create a new list type.

        The provided element type must not be a raw type

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        T - type of element
        Parameters:
        elementType - class representing the element type
        Returns:
        new list type token
        Since:
        4.2.0
      • makeListType

        public static <T> io.leangen.geantyref.TypeToken<java.util.List<T>> makeListType​(io.leangen.geantyref.TypeToken<T> elementType)
        Given an element type, create a new list type.

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        T - type of element
        Parameters:
        elementType - type token representing the element type
        Returns:
        new list type token
        Since:
        4.0.0
      • makeMap

        public static <K,​V> io.leangen.geantyref.TypeToken<java.util.Map<K,​V>> makeMap​(java.lang.Class<K> key,
                                                                                                   java.lang.Class<V> value)
        Given an element type, create a new Map type.

        The provided key and value types must not be a raw type

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        K - type of key
        V - type of value
        Parameters:
        key - type of the map's key
        value - type of the map's value
        Returns:
        new Map type token
        Since:
        4.2.0
      • makeMap

        public static <K,​V> io.leangen.geantyref.TypeToken<java.util.Map<K,​V>> makeMap​(io.leangen.geantyref.TypeToken<K> key,
                                                                                                   io.leangen.geantyref.TypeToken<V> value)
        Given an element type, create a new Map type.

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        K - type of key
        V - type of value
        Parameters:
        key - type of the map's key
        value - type of the map's value
        Returns:
        new Map type token
        Since:
        4.2.0
      • makeMap

        public static <K,​V> io.leangen.geantyref.TypeToken<java.util.Map<K,​V>> makeMap​(java.lang.Class<K> key,
                                                                                                   io.leangen.geantyref.TypeToken<V> value)
        Given an element type, create a new Map type.

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        K - type of key
        V - type of value
        Parameters:
        key - type of the map's key
        value - type of the map's value
        Returns:
        new Map type token
        Since:
        4.2.0
      • makeMap

        public static <K,​V> io.leangen.geantyref.TypeToken<java.util.Map<K,​V>> makeMap​(io.leangen.geantyref.TypeToken<K> key,
                                                                                                   java.lang.Class<V> value)
        Given an element type, create a new Map type.

        This has an outcome similar to constructing a TypeToken directly, but avoids generating an extra anonymous class.

        Type Parameters:
        K - type of key
        V - type of value
        Parameters:
        key - type of the map's key
        value - type of the map's value
        Returns:
        new Map type token
        Since:
        4.2.0
      • combinedAnnotations

        public static java.lang.reflect.AnnotatedElement combinedAnnotations​(java.lang.reflect.AnnotatedElement... elements)
        Get an element containing the annotations of all the provided elements.

        If multiple elements have the same annotation, only the first one with an applicable type is returned.

        Parameters:
        elements - elements to combine
        Returns:
        new union element
        Since:
        4.0.0
      • requireCompleteParameters

        public static java.lang.reflect.Type requireCompleteParameters​(java.lang.reflect.Type input)
        Throw an exception if the passed type is raw (missing parameters)..
        Parameters:
        input - input type
        Returns:
        type, passed through
        Since:
        4.0.0
      • allSuperTypes

        public static java.util.stream.Stream<java.lang.reflect.Type> allSuperTypes​(java.lang.reflect.Type type)
        Get all supertypes of this object with type parameters.

        The iteration order is undefined. The returned stream will include the base type plus superclasses, but not superinterfaces.

        Parameters:
        type - base type
        Returns:
        stream of supertypes
        Since:
        4.0.0
      • allSuperTypesAndInterfaces

        public static java.util.stream.Stream<java.lang.reflect.Type> allSuperTypesAndInterfaces​(java.lang.reflect.Type type)
        Get all supertypes and interfaces of the provided type.

        The iteration order is undefined. The returned stream will include the base type plus superclasses and superinterfaces.

        Parameters:
        type - base type
        Returns:
        stream of supertypes
        Since:
        4.0.0
      • allDeclaredMethods

        public static java.util.List<java.lang.reflect.Method> allDeclaredMethods​(java.lang.Class<?> clazz)
        Get all declared methods in the provided class and its superclasses and superinterfaces, up to but not including Object.

        Overridden methods will be skipped when encountered in parent types.

        Parameters:
        clazz - the class to visit
        Returns:
        a list of methods that may not be modifiable
        Since:
        4.2.0