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
This 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 realClass
or 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> invalid input: '&' 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();
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Type representing bounds of a typeprivate static class
Generic metadata implementation forClass
.private static class
Generic metadata implementation for "standalone"ParameterizedType
.private static class
private static class
Non-Generic metadata forClass
returned viaMethod.getGenericReturnType()
.private static class
Generic metadata specific toParameterizedType
returned viaMethod.getGenericReturnType()
.static class
Type representing bounds of a type variable, allows to keep all bounds information.private static class
Generic metadata forTypeVariable
returned viaMethod.getGenericReturnType()
.static class
Type representing bounds of a wildcard, allows to keep all bounds information. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Map
<TypeVariable<?>, Type> Represents actual type variables resolved for current class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionMap
<TypeVariable<?>, Type> boundsOf
(TypeVariable<?> typeParameter) boundsOf
(WildcardType wildCard) protected Class
<?> extractRawTypeOf
(Type type) protected Type
getActualTypeArgumentFor
(TypeVariable<?> typeParameter) boolean
static GenericMetadataSupport
Create an new instance ofGenericMetadataSupport
inferred from aType
.Class<?>[]
abstract Class
<?> rawType()
protected void
registerAllTypeVariables
(Type classType) Registers the type variables for the given type and all of its superclasses and superinterfaces.protected void
registerTypeParametersOn
(TypeVariable<?>[] typeParameters) private void
registerTypeVariableIfNotPresent
(TypeVariable<?> typeVariable) protected void
registerTypeVariablesOn
(Type classType) resolveGenericReturnType
(Method method) Resolve current method generic return type to aGenericMetadataSupport
.private GenericMetadataSupport
resolveGenericType
(Type type, Method method)
-
Field Details
-
contextualActualTypeParameters
Represents actual type variables resolved for current class.
-
-
Constructor Details
-
GenericMetadataSupport
public GenericMetadataSupport()
-
-
Method Details
-
registerAllTypeVariables
Registers the type variables for the given type and all of its superclasses and superinterfaces. -
extractRawTypeOf
-
registerTypeVariablesOn
-
registerTypeParametersOn
-
registerTypeVariableIfNotPresent
-
boundsOf
- Parameters:
typeParameter
- The TypeVariable parameter- Returns:
- A
GenericMetadataSupport.BoundedType
for easy bound information, if first bound is a TypeVariable then retrieve BoundedType of this TypeVariable
-
boundsOf
- Parameters:
wildCard
- The WildCard type- Returns:
- A
GenericMetadataSupport.BoundedType
for easy bound information, if first bound is a TypeVariable then retrieve BoundedType of this TypeVariable
-
rawType
- Returns:
- Raw type of the current instance.
-
extraInterfaces
- Returns:
- Returns extra interfaces if relevant, otherwise empty List.
-
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
- Returns:
- Actual type arguments matching the type variables of the raw type represented by this
GenericMetadataSupport
instance.
-
getActualTypeArgumentFor
-
resolveGenericReturnType
Resolve current method generic return type to aGenericMetadataSupport
.- Parameters:
method
- Method to resolve the return type.- Returns:
GenericMetadataSupport
representing this generic return type.
-
resolveGenericType
-
inferFrom
Create an new instance ofGenericMetadataSupport
inferred from aType
.At the moment
type
can only be aClass
or aParameterizedType
, otherwise it'll throw aMockitoException
.- Parameters:
type
- The class from which theGenericMetadataSupport
should be built.- Returns:
- The new
GenericMetadataSupport
. - Throws:
MockitoException
- Raised if type is not aClass
or aParameterizedType
.
-