- java.lang.Object
-
- org.github.gestalt.config.reflect.TypeCapture<T>
-
- Type Parameters:
T- type to capture
public class TypeCapture<T> extends java.lang.Objecta 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.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypeCapture()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.protectedTypeCapture(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.protectedTypeCapture(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.booleanequals(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.StringgetName()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.TypegetSuperclassTypeParameter(java.lang.Class<?> subclass)Get the Super class for a type.inthashCode()booleanhasParameter()If this class has a generic parameter.booleanisArray()If this type is a array.booleanisAssignableFrom(java.lang.reflect.Type classType)If this class is assignable from a type.booleanisEnum()If this type is an Enum.booleanisInterface()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.StringtoString()
-
-
-
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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-