Class Types


  • public class Types
    extends java.lang.Object
    Support functions for types, classes and interfaces.
    • Constructor Summary

      Constructors 
      Constructor Description
      Types()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsTypeVariables​(java.lang.reflect.Type type)
      Checks if any type parameters for the given Type are bound to variables.
      static java.util.Set<java.lang.reflect.Type> getGenericSuperTypes​(java.lang.reflect.Type type)
      Given a Type, returns a set of all types extended or implemented.
      static java.util.Set<java.lang.Class<?>> getSuperTypes​(java.lang.Class<?> cls)
      Given a Class, returns a set of all classes and interfaces extended or implemented.
      static java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> getTypeArguments​(java.lang.reflect.Type type, java.lang.Class<?> toClass)
      Gets the type arguments of a class/interface based on a subtype.
      static java.lang.reflect.Type getTypeParameter​(java.lang.reflect.Type type, java.lang.Class<?> cls, java.lang.reflect.TypeVariable<?> typeVariable)
      Gets the Type of a generic parameter, identified by given by the TypeVariable, of the given Class when resolved against the given Type.
      static java.lang.reflect.Type[] getUpperBounds​(java.lang.reflect.WildcardType type)
      Returns the upper bounds of the given WildcardType.
      static boolean isAssignable​(java.lang.reflect.Type type, java.lang.reflect.Type toType)
      Checks if the subject type may be implicitly cast to the target type following Java generics rules.
      static java.lang.reflect.ParameterizedType parameterize​(java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)
      Create a new ParameterizedType with the given type arguments.
      static <T> java.lang.Class<T> raw​(java.lang.reflect.Type type)
      Gets the raw type of a Java Type.
      static java.lang.reflect.Type resolveVariables​(java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeArguments, java.lang.reflect.Type type)
      Get a type representing type with variable assignments resolved.
      static java.lang.reflect.WildcardType wildcardExtends​(java.lang.reflect.Type... upperBounds)
      Creates a new WildcardType with the given upper bounds.
      • Methods inherited from class java.lang.Object

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

      • Types

        public Types()
    • Method Detail

      • getSuperTypes

        public static java.util.Set<java.lang.Class<?>> getSuperTypes​(java.lang.Class<?> cls)
        Given a Class, returns a set of all classes and interfaces extended or implemented.
        Parameters:
        cls - a Class, cannot be null
        Returns:
        a set of all classes and interfaces extended or implemented, never null, never contains nulls and never empty
      • getGenericSuperTypes

        public static java.util.Set<java.lang.reflect.Type> getGenericSuperTypes​(java.lang.reflect.Type type)
        Given a Type, returns a set of all types extended or implemented.
        Parameters:
        type - a Type, cannot be null
        Returns:
        a set of all types extended or implemented, never null, never contains nulls and never empty
      • parameterize

        public static java.lang.reflect.ParameterizedType parameterize​(java.lang.Class<?> raw,
                                                                       java.lang.reflect.Type... typeArguments)
        Create a new ParameterizedType with the given type arguments.
        Parameters:
        raw - a Class to parameterize, cannot be null
        typeArguments - an array of type arguments
        Returns:
        a ParameterizedType, never null
      • wildcardExtends

        public static java.lang.reflect.WildcardType wildcardExtends​(java.lang.reflect.Type... upperBounds)
        Creates a new WildcardType with the given upper bounds. Note that the Java specification does not allow wildcard types with multiple upper bounds to be created in source code, however you can create them programmatically in order to for example filter by types that must implement two interfaces.
        Parameters:
        upperBounds - an array of Types, cannot be null or contain nulls
        Returns:
        a WildcardType, never null
      • raw

        public static <T> java.lang.Class<T> raw​(java.lang.reflect.Type type)
        Gets the raw type of a Java Type. This works for classes and parameterized types. Returns null if no raw type can be derived.
        Type Parameters:
        T - the expected type
        Parameters:
        type - a Type, cannot be null
        Returns:
        a Class representing the raw type of the given type, can be null
      • containsTypeVariables

        public static boolean containsTypeVariables​(java.lang.reflect.Type type)
        Checks if any type parameters for the given Type are bound to variables.
        Parameters:
        type - a Type, cannot be null
        Returns:
        true if any parameters are bound to variables, otherwise false
      • getTypeParameter

        public static java.lang.reflect.Type getTypeParameter​(java.lang.reflect.Type type,
                                                              java.lang.Class<?> cls,
                                                              java.lang.reflect.TypeVariable<?> typeVariable)
        Gets the Type of a generic parameter, identified by given by the TypeVariable, of the given Class when resolved against the given Type.
        Parameters:
        type - a Type from which to determine the parameters of the given Class, cannot be null
        cls - a Class to determine a type parameter for, cannot be null
        typeVariable - a TypeVariable of the given Class to extract, cannot be null
        Returns:
        a Type, can be null if the TypeVariable was not associated with the given Class
      • getTypeArguments

        public static java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> getTypeArguments​(java.lang.reflect.Type type,
                                                                                                                     java.lang.Class<?> toClass)
        Gets the type arguments of a class/interface based on a subtype.

        This method returns null if type is not assignable to toClass. It returns an empty map if none of the classes or interfaces in its inheritance hierarchy specify any type arguments.

        Parameters:
        type - a type from which to determine the type parameters of toClass, cannot be null
        toClass - a class whose type parameters are to be determined based on the subtype type, cannot be null
        Returns:
        a map of the type assignments for the type variables in each type in the inheritance hierarchy from type to toClass inclusive or null
      • resolveVariables

        public static java.lang.reflect.Type resolveVariables​(java.util.Map<java.lang.reflect.TypeVariable<?>,​java.lang.reflect.Type> typeArguments,
                                                              java.lang.reflect.Type type)
        Get a type representing type with variable assignments resolved.
        Parameters:
        typeArguments - as from getTypeArguments(Type, Class), cannot be null
        type - a type to unroll variable assignments for, cannot be null
        Returns:
        a Type or null when not all variables can be resolved
      • isAssignable

        public static boolean isAssignable​(java.lang.reflect.Type type,
                                           java.lang.reflect.Type toType)
        Checks if the subject type may be implicitly cast to the target type following Java generics rules. If both types are Class objects, the method returns the result of ClassUtils.isAssignable(Class, Class).
        Parameters:
        type - the subject type to be assigned to the target type, cannot be null
        toType - the target type, cannot be null
        Returns:
        true if type is assignable to toType
      • getUpperBounds

        public static java.lang.reflect.Type[] getUpperBounds​(java.lang.reflect.WildcardType type)
        Returns the upper bounds of the given WildcardType.
        Parameters:
        type - a WildcardType, cannot be null
        Returns:
        a non-empty array containing the upper bounds of the wildcard type, never null