public class TypeInfo<T> extends Object
T. Java doesn't yet provide a way to
represent generic types, so this class does. Forces clients to create a
subclass of this class which enables retrieval the type information even at
runtime.
For example, to create a type literal for List<String>, you can
create an empty anonymous inner class:
TypeInfo<List<String>> list = new TypeInfo<List<String>>() {};
Along with modeling generic types, this class can resolve type parameters.
For example, to figure out what type keySet() returns on a Map<Integer, String>, use this code:
TypeInfo<Map<Integer, String>> mapType
= new TypeInfo<Map<Integer, String>>() {};
TypeInfo<?> keySetType
= mapType.getReturnType(Map.class.getMethod("keySet"));
System.out.println(keySetType); // prints "Set<Integer>"| Modifier | Constructor and Description |
|---|---|
protected |
TypeInfo()
Constructs a new type literal.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o) |
static <T> TypeInfo<T> |
forClass(Class<T> type)
Create new type info for given
Type |
static <T> TypeInfo<T> |
forClass(Class<T> type,
AnnotatedNode annotatedNode)
Create new type info for given
Type |
static TypeInfo<?> |
forType(Type type)
Create new type info for given
Type |
static TypeInfo<?> |
forType(Type type,
AnnotatedNode annotatedNode)
Create new type info for given
Type |
AnnotatedNode |
getAnnotatedNode() |
TypeInfo<?> |
getComponentType() |
List<TypeInfo<?>> |
getExceptionTypes(Member methodOrConstructor)
Returns the resolved generic exception types thrown by
constructor. |
TypeInfo<?> |
getFieldType(Field field)
Returns the resolved generic type of
field. |
TypeInfo[] |
getInterfaces() |
List<TypeInfo<?>> |
getParameterTypes(Member methodOrConstructor)
Returns the resolved generic parameter types of
methodOrConstructor. |
Class<T> |
getRawType()
Returns the raw (non-generic) type for this type.
|
TypeInfo<?> |
getReturnType(Method method)
Returns the resolved generic return type of
method. |
TypeInfo<?> |
getSuperType() |
TypeInfo<?> |
getSupertype(Class<?> supertype)
Returns the generic form of
supertype. |
Type |
getType()
Gets underlying
Type instance. |
int |
hashCode() |
boolean |
hasParameters() |
boolean |
isArray() |
protected TypeInfo |
resolveForClass(Class _type) |
TypeInfo |
resolveParameter(int index) |
String |
toString() |
protected TypeInfo()
Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
public AnnotatedNode getAnnotatedNode()
public final Class<T> getRawType()
public final Type getType()
Type instance.public TypeInfo<?> getComponentType()
public boolean isArray()
public TypeInfo<?> getSuperType()
public TypeInfo[] getInterfaces()
public boolean hasParameters()
public TypeInfo resolveParameter(int index)
public static TypeInfo<?> forType(Type type, AnnotatedNode annotatedNode)
Typepublic static <T> TypeInfo<T> forClass(Class<T> type, AnnotatedNode annotatedNode)
Typepublic TypeInfo<?> getSupertype(Class<?> supertype)
supertype. For example, if this is ArrayList<String>, this returns Iterable<String> given the input Iterable.class.supertype - a superclass of, or interface implemented by, this.public TypeInfo<?> getFieldType(Field field)
field.field - a field defined by this or any superclass.public List<TypeInfo<?>> getParameterTypes(Member methodOrConstructor)
methodOrConstructor.methodOrConstructor - a method or constructor defined by this or any supertype.public List<TypeInfo<?>> getExceptionTypes(Member methodOrConstructor)
constructor.methodOrConstructor - a method or constructor defined by this or any supertype.Copyright © 2016. All rights reserved.