Class GenericMetadataSupport
- java.lang.Object
-
- org.mockito.internal.util.reflection.GenericMetadataSupport
-
- Direct Known Subclasses:
GenericMetadataSupport.FromClassGenericMetadataSupport,GenericMetadataSupport.FromParameterizedTypeGenericMetadataSupport,GenericMetadataSupport.GenericArrayReturnType,GenericMetadataSupport.NotGenericReturnTypeSupport,GenericMetadataSupport.ParameterizedReturnType,GenericMetadataSupport.TypeVariableReturnType
public abstract class GenericMetadataSupport extends java.lang.ObjectThis class can retrieve generic meta-data that the compiler stores on classes and accessible members.The main idea of this code is to create a Map that will help to resolve return types. In order to actually work with nested generics, this map will have to be passed along new instances as a type context.
Hence :
- A new instance representing the metadata is created using the
inferFrom(Type)method from a realClassor from aParameterizedType, other types are not yet supported. - Then from this metadata, we can extract meta-data for a generic return type of a method, using
resolveGenericReturnType(Method).
For now this code support the following kind of generic declarations :
interface GenericsNest<K extends Comparable<K> & Cloneable> extends Map<K, Set<Number>> { Set<Number> remove(Object key); // override with fixed ParameterizedType List<? super Integer> returning_wildcard_with_class_lower_bound(); List<? super K> returning_wildcard_with_typeVar_lower_bound(); List<? extends K> returning_wildcard_with_typeVar_upper_bound(); K returningK(); <O extends K> List<O> paramType_with_type_params(); <S, T extends S> T two_type_params(); <O extends K> O typeVar_with_type_params(); Number returningNonGeneric(); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceGenericMetadataSupport.BoundedTypeType representing bounds of a typeprivate static classGenericMetadataSupport.FromClassGenericMetadataSupportGeneric metadata implementation forClass.private static classGenericMetadataSupport.FromParameterizedTypeGenericMetadataSupportGeneric metadata implementation for "standalone"ParameterizedType.private static classGenericMetadataSupport.GenericArrayReturnTypeprivate static classGenericMetadataSupport.NotGenericReturnTypeSupportNon-Generic metadata forClassreturned viaMethod.getGenericReturnType().private static classGenericMetadataSupport.ParameterizedReturnTypeGeneric metadata specific toParameterizedTypereturned viaMethod.getGenericReturnType().static classGenericMetadataSupport.TypeVarBoundedTypeType representing bounds of a type variable, allows to keep all bounds information.private static classGenericMetadataSupport.TypeVariableReturnTypeGeneric metadata forTypeVariablereturned viaMethod.getGenericReturnType().static classGenericMetadataSupport.WildCardBoundedTypeType representing bounds of a wildcard, allows to keep all bounds information.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>contextualActualTypeParametersRepresents actual type variables resolved for current class.
-
Constructor Summary
Constructors Constructor Description GenericMetadataSupport()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>actualTypeArguments()private GenericMetadataSupport.BoundedTypeboundsOf(java.lang.reflect.TypeVariable<?> typeParameter)private GenericMetadataSupport.BoundedTypeboundsOf(java.lang.reflect.WildcardType wildCard)protected java.lang.Class<?>extractRawTypeOf(java.lang.reflect.Type type)java.util.List<java.lang.reflect.Type>extraInterfaces()protected java.lang.reflect.TypegetActualTypeArgumentFor(java.lang.reflect.TypeVariable<?> typeParameter)booleanhasRawExtraInterfaces()static GenericMetadataSupportinferFrom(java.lang.reflect.Type type)Create an new instance ofGenericMetadataSupportinferred from aType.java.lang.Class<?>[]rawExtraInterfaces()abstract java.lang.Class<?>rawType()protected voidregisterAllTypeVariables(java.lang.reflect.Type classType)Registers the type variables for the given type and all of its superclasses and superinterfaces.protected voidregisterTypeParametersOn(java.lang.reflect.TypeVariable<?>[] typeParameters)private voidregisterTypeVariableIfNotPresent(java.lang.reflect.TypeVariable<?> typeVariable)protected voidregisterTypeVariablesOn(java.lang.reflect.Type classType)GenericMetadataSupportresolveGenericReturnType(java.lang.reflect.Method method)Resolve current method generic return type to aGenericMetadataSupport.private GenericMetadataSupportresolveGenericType(java.lang.reflect.Type type, java.lang.reflect.Method method)
-
-
-
Method Detail
-
registerAllTypeVariables
protected void registerAllTypeVariables(java.lang.reflect.Type classType)
Registers the type variables for the given type and all of its superclasses and superinterfaces.
-
extractRawTypeOf
protected java.lang.Class<?> extractRawTypeOf(java.lang.reflect.Type type)
-
registerTypeVariablesOn
protected void registerTypeVariablesOn(java.lang.reflect.Type classType)
-
registerTypeParametersOn
protected void registerTypeParametersOn(java.lang.reflect.TypeVariable<?>[] typeParameters)
-
registerTypeVariableIfNotPresent
private void registerTypeVariableIfNotPresent(java.lang.reflect.TypeVariable<?> typeVariable)
-
boundsOf
private GenericMetadataSupport.BoundedType boundsOf(java.lang.reflect.TypeVariable<?> typeParameter)
- Parameters:
typeParameter- The TypeVariable parameter- Returns:
- A
GenericMetadataSupport.BoundedTypefor easy bound information, if first bound is a TypeVariable then retrieve BoundedType of this TypeVariable
-
boundsOf
private GenericMetadataSupport.BoundedType boundsOf(java.lang.reflect.WildcardType wildCard)
- Parameters:
wildCard- The WildCard type- Returns:
- A
GenericMetadataSupport.BoundedTypefor easy bound information, if first bound is a TypeVariable then retrieve BoundedType of this TypeVariable
-
rawType
public abstract java.lang.Class<?> rawType()
- Returns:
- Raw type of the current instance.
-
extraInterfaces
public java.util.List<java.lang.reflect.Type> extraInterfaces()
- Returns:
- Returns extra interfaces if relevant, otherwise empty List.
-
rawExtraInterfaces
public java.lang.Class<?>[] rawExtraInterfaces()
- Returns:
- Returns an array with the raw types of
extraInterfaces()if relevant.
-
hasRawExtraInterfaces
public boolean hasRawExtraInterfaces()
- Returns:
- Returns true if metadata knows about extra-interfaces
extraInterfaces()if relevant.
-
actualTypeArguments
public java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> actualTypeArguments()
- Returns:
- Actual type arguments matching the type variables of the raw type represented by this
GenericMetadataSupportinstance.
-
getActualTypeArgumentFor
protected java.lang.reflect.Type getActualTypeArgumentFor(java.lang.reflect.TypeVariable<?> typeParameter)
-
resolveGenericReturnType
public GenericMetadataSupport resolveGenericReturnType(java.lang.reflect.Method method)
Resolve current method generic return type to aGenericMetadataSupport.- Parameters:
method- Method to resolve the return type.- Returns:
GenericMetadataSupportrepresenting this generic return type.
-
resolveGenericType
private GenericMetadataSupport resolveGenericType(java.lang.reflect.Type type, java.lang.reflect.Method method)
-
inferFrom
public static GenericMetadataSupport inferFrom(java.lang.reflect.Type type)
Create an new instance ofGenericMetadataSupportinferred from aType.At the moment
typecan only be aClassor aParameterizedType, otherwise it'll throw aMockitoException.- Parameters:
type- The class from which theGenericMetadataSupportshould be built.- Returns:
- The new
GenericMetadataSupport. - Throws:
MockitoException- Raised if type is not aClassor aParameterizedType.
-
-