public class GenericsUtils extends Object
Constructor and Description |
---|
GenericsUtils() |
Modifier and Type | Method and Description |
---|---|
static Class |
asClass(Type actualType)
Get the class represented by the reflected type.
|
static Type |
extractActualType(Type containingType,
Field field)
Analyzes the method in the context of containingClass and returns the Class that is represented by
the method's generic return type.
|
static Type |
extractActualType(Type containingType,
Method method)
Analyzes the method in the context of containingClass and returns the Class that is represented by
the method's generic return type.
|
static Class |
extractGenericFieldType(Class containingClass,
Field field)
Analyzes the field in the context of containingClass and returns the Class that is represented by
the field's generic type.
|
static Class<?> |
extractGenericReturnType(Class<?> containingClass,
Method method)
Analyzes the method in the context of containingClass and returns the Class that is represented by
the method's generic return type.
|
static boolean |
isAssignableFrom(Type suspectedSuperType,
Type suspectedSubType)
Determines if the suspected super type is assignable from the suspected sub type.
|
static Type |
resolve(Type type,
Type containingType)
Resolves the type parameter based on the context of the containingType.
|
static String |
toString(Type type)
Convert the type into a string.
|
public GenericsUtils()
public static Class<?> extractGenericReturnType(Class<?> containingClass, Method method)
extractActualType(java.lang.reflect.Type, java.lang.reflect.Method)
.containingClass
- class which either contains or inherited the methodmethod
- method from which to extract the return typeextractActualType(java.lang.reflect.Type, java.lang.reflect.Method)
,
resolve(java.lang.reflect.Type,java.lang.reflect.Type)
,
asClass(java.lang.reflect.Type)
public static Class extractGenericFieldType(Class containingClass, Field field)
getTypeVariableIndex(java.lang.reflect.TypeVariable)
.containingClass
- class which either contains or inherited the fieldfield
- field from which to extract the typeextractActualType(java.lang.reflect.Type, java.lang.reflect.Field)
,
resolve(java.lang.reflect.Type,java.lang.reflect.Type)
,
asClass(java.lang.reflect.Type)
public static Type extractActualType(Type containingType, Method method)
containingType
- Type which is/represents the class that either contains or inherited the methodmethod
- method from which to extract the generic return typeresolve(java.lang.reflect.Type,java.lang.reflect.Type)
public static Type extractActualType(Type containingType, Field field)
containingType
- Type which is/represents the class that either contains or inherited the fieldfield
- field from which to extract the generic return typeresolve(java.lang.reflect.Type,java.lang.reflect.Type)
public static Type resolve(Type type, Type containingType)
TypeVariable
will be unwrapped to the type argument resolved form the class
hierarchy. This may be something other than a simple Class if the type argument is a ParameterizedType for
instance (e.g. List<E>; List<Map<Long, String>>
, E would be returned as a ParameterizedType with the raw
type Map and type arguments Long and String.type
- the generic type (ParameterizedType, GenericArrayType, WildcardType, TypeVariable) to be resolvedcontainingType
- the type which hispublic static boolean isAssignableFrom(Type suspectedSuperType, Type suspectedSubType)
suspectedSuperType
- e.g. GenericDAO<Pet, String>
suspectedSubType
- e.g. PetDAO extends GenericDAO<Pet,String>
public static Class asClass(Type actualType)
TypeVariable
the first bound is returned. If your type variable extends multiple interfaces that information
is lost.
WildcardType
the first lower bound is returned. If the wildcard is defined with upper bounds
then Object
is returned.actualType
- a Class, ParameterizedType, GenericArrayType${project.version} - Copyright © 2003-2015 The Apache Software Foundation.