Class GenericType<T>

  • Type Parameters:
    T - the generic type parameter
    All Implemented Interfaces:
    Type

    public class GenericType<T>
    extends Object
    implements Type
    Represents a full type including generics declaration, to avoid information loss due to type erasure. Supports in-line instantiation of objects that represent generic types with actual type parameters. An object that represents any parameterized type may be obtained by sub-classing GenericType. Alternatively, an object representing a concrete parameterized type can be created using a create(Type) and manually specifying the type() actual (parameterized) type}.

    For example:

      GenericType<List<String>> stringListType = new GenericType<List<String>>() {};
     

    Or:

      public class MyGenericType extends GenericType<List<String>> { ... }
    
      ...
    
      MyGenericType stringListType = new MyGenericType();
     

    Note that due to the Java type erasure limitations the parameterized type information must be specified on a subclass, not just during the instance creation. For example, the following case would throw an IllegalArgumentException:

      public class MyGenericType<T> extends GenericType<T> { ... }
    
      ...
    
      // The type is only specified on instance, not in a sub-class
      MyGenericType<List<String>> stringListType =
              new MyGenericType<List<String>>();
     
    • Constructor Detail

      • GenericType

        protected GenericType()
                       throws IllegalArgumentException
        Constructs a new generic type, deriving the generic type and class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.
        Throws:
        IllegalArgumentException - in case the generic type parameter value is not provided by any of the subclasses.
    • Method Detail

      • create

        public static <N> GenericType<N> create​(Type genericType)
                                         throws IllegalArgumentException
        Constructs a new generic type, using the provided generic type information and deriving the class.
        Type Parameters:
        N - generic type of the returned GenericType
        Parameters:
        genericType - the generic type
        Returns:
        new type wrapping the provided type
        Throws:
        IllegalArgumentException - if genericType is null or not an instance of Class or ParameterizedType whose raw type is an instance of Class.
      • create

        public static <N> GenericType<N> create​(Class<N> clazz)
        Constructs a new generic type instance representing the given class.
        Type Parameters:
        N - generic type of the returned GenericType
        Parameters:
        clazz - the class to represent
        Returns:
        new type wrapping the provided class
      • create

        public static <N> GenericType<N> create​(N object)
        Constructs a new generic type instance representing the class of the given object.
        Type Parameters:
        N - generic type of the returned GenericType
        Parameters:
        object - the object to derive the class of
        Returns:
        new type wrapping the class of the provided object
      • type

        public Type type()
        The type represented by this generic type instance.

        For new GenericType<List<String>>(){}, this would return a ParameterizedType for java.util.List<java.lang.String>.

        Returns:
        the actual type represented by this generic type instance.
      • rawType

        public Class<?> rawType()
        Returns the object representing the class or interface that declared the type represented by this generic type instance.

        For new GenericType<List<String>>(){}, this would return an interface java.util.List.

        Returns:
        the class or interface that declared the type represented by this generic type instance.
      • isClass

        public boolean isClass()
        Whether this generic type represents a simple class with no generic information.
        Returns:
        true if this is a class, false if this is a generic type (e.g. returns true for List, false for List<String>
      • cast

        public T cast​(Object object)
               throws ClassCastException
        Casts the parameter to the type of this generic type. This is a utility method to use in stream processing etc.
        Parameters:
        object - instance to cast
        Returns:
        typed instance
        Throws:
        ClassCastException - in case the object is not of the expected type
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object