Class TypeCapture<T>

  • Type Parameters:
    T - type to capture

    public class TypeCapture<T>
    extends java.lang.Object
    a java doesn't have reified generics so if we are trying to get the type of a list at runtime we are unable to. This class is modeled on the TypeToken from Guava, TypeLiteral from guice and jackson.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int hashCode
      hashcode for this TypeCapture.
      protected java.lang.Class<?> rawType
      Raw type for this TypeCapture.
      protected java.lang.reflect.Type type
      type for this TypeCapture.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected TypeCapture()
      The default constructor is used in the form new TypeCapture<List<String>>() { }; This will capture the type of List<string> and save it, allowing us to know at runtime the type of a list.
      protected TypeCapture​(java.lang.Class<T> klass)
      Create a TypeCapture using a generic type, this will suffer from type erasure, so only use if for non generic classes.
      protected TypeCapture​(java.lang.reflect.Type klass)
      Create a TypeCapture using a Type.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Class<?> buildRawType​(java.lang.reflect.Type type)
      Lifted from Guice TypeLiteral.
      boolean equals​(java.lang.Object o)  
      java.lang.annotation.Annotation[] getAnnotations()
      Get the annotations on this type.
      <A extends java.lang.annotation.Annotation>
      A[]
      getAnnotationsByType​(java.lang.Class<A> annotationClass)
      Get the annotations by type.
      java.lang.Class<?> getComponentType()
      Returns the Class representing the component type of an array.
      TypeCapture<?> getFirstParameterType()
      Get the TypeCapture of the first generic parameter or null if there is none.
      java.lang.String getName()
      Get the name of the class.
      java.util.List<TypeCapture<?>> getParameterTypes()
      Get all generic parameter types.
      java.lang.Class<?> getRawType()
      Get the raw type of this class.
      TypeCapture<?> getSecondParameterType()
      Get the TypeCapture of the second generic parameter or null if there is none.
      protected java.lang.reflect.Type getSuperclassTypeParameter​(java.lang.Class<?> subclass)
      Get the Super class for a type.
      int hashCode()  
      boolean hasParameter()
      If this class has a generic parameter.
      boolean isArray()
      If this type is a array.
      boolean isAssignableFrom​(java.lang.reflect.Type classType)
      If this class is assignable from a type.
      boolean isEnum()
      If this type is an Enum.
      boolean isInterface()
      If this type is an interface.
      static <T> TypeCapture<T> of​(java.lang.Class<T> klass)
      When we only have a class, we can get the type from the class.
      static <T> TypeCapture<T> of​(java.lang.reflect.Type klass)
      When we only have a class, we can get the type from the class.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • rawType

        protected final java.lang.Class<?> rawType
        Raw type for this TypeCapture.
      • type

        protected final java.lang.reflect.Type type
        type for this TypeCapture.
      • hashCode

        protected final int hashCode
        hashcode for this TypeCapture.
    • Constructor Detail

      • TypeCapture

        protected TypeCapture()
        The default constructor is used in the form new TypeCapture<List<String>>() { }; This will capture the type of List<string> and save it, allowing us to know at runtime the type of a list.
      • TypeCapture

        protected TypeCapture​(java.lang.Class<T> klass)
        Create a TypeCapture using a generic type, this will suffer from type erasure, so only use if for non generic classes.
        Parameters:
        klass - class to capture
      • TypeCapture

        protected TypeCapture​(java.lang.reflect.Type klass)
        Create a TypeCapture using a Type.
        Parameters:
        klass - java Type
    • Method Detail

      • of

        public static <T> TypeCapture<T> of​(java.lang.Class<T> klass)
        When we only have a class, we can get the type from the class. This should not be used with classes that have generic type parameters.
        Type Parameters:
        T - type of capture
        Parameters:
        klass - to get type of.
        Returns:
        the TypeCapture
      • of

        public static <T> TypeCapture<T> of​(java.lang.reflect.Type klass)
        When we only have a class, we can get the type from the class. This should not be used with classes that have generic type parameters.
        Type Parameters:
        T - type of capture
        Parameters:
        klass - type to capture.
        Returns:
        the TypeCapture
      • hasParameter

        public boolean hasParameter()
        If this class has a generic parameter.
        Returns:
        If this class has a generic parameter
      • getFirstParameterType

        public TypeCapture<?> getFirstParameterType()
        Get the TypeCapture of the first generic parameter or null if there is none.
        Returns:
        the TypeCapture of the first generic parameter or null if there is none.
      • getSecondParameterType

        public TypeCapture<?> getSecondParameterType()
        Get the TypeCapture of the second generic parameter or null if there is none.
        Returns:
        the TypeCapture of the second generic parameter or null if there is none.
      • getParameterTypes

        public java.util.List<TypeCapture<?>> getParameterTypes()
        Get all generic parameter types.
        Returns:
        list of all generic parameter types.
      • getComponentType

        public java.lang.Class<?> getComponentType()
        Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.
        Returns:
        component type
      • getRawType

        public java.lang.Class<?> getRawType()
        Get the raw type of this class.
        Returns:
        the raw type of this class.
      • getName

        public java.lang.String getName()
        Get the name of the class.
        Returns:
        name of the class
      • isAssignableFrom

        public boolean isAssignableFrom​(java.lang.reflect.Type classType)
        If this class is assignable from a type.
        Parameters:
        classType - type to check if we are asignable
        Returns:
        If this class is assignable from a type.
      • isArray

        public boolean isArray()
        If this type is a array.
        Returns:
        If this type is a array
      • isEnum

        public boolean isEnum()
        If this type is an Enum.
        Returns:
        If this type is an Enum
      • isInterface

        public boolean isInterface()
        If this type is an interface.
        Returns:
        If this type is an interface
      • getAnnotations

        public java.lang.annotation.Annotation[] getAnnotations()
        Get the annotations on this type.
        Returns:
        annotations on this type
      • getAnnotationsByType

        public <A extends java.lang.annotation.Annotation> A[] getAnnotationsByType​(java.lang.Class<A> annotationClass)
        Get the annotations by type.
        Type Parameters:
        A - the type of the annotation
        Parameters:
        annotationClass - annotation class
        Returns:
        the annotation
      • getSuperclassTypeParameter

        protected java.lang.reflect.Type getSuperclassTypeParameter​(java.lang.Class<?> subclass)
        Get the Super class for a type.
        Parameters:
        subclass - class we are looking for the super class of
        Returns:
        Super class for a type
      • buildRawType

        protected java.lang.Class<?> buildRawType​(java.lang.reflect.Type type)
        Lifted from Guice TypeLiteral. ...
        Parameters:
        type - java type
        Returns:
        class of the raw type
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object