Class GenericTypeReflector
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class(package private) static classA key representing aCaptureType. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TypeaddWildcardParameters(Class<?> clazz) Returns a type representing the class, with all type parameters the unbound wildcard ("?").static AnnotatedTypeRecursively wraps aTypeinto anAnnotatedTypeusing the annotations found on the erasure classes.private static AnnotatedTypeprivate static AnnotatedTypeannotate(Type type, boolean expandGenerics, Map<GenericTypeReflector.CaptureCacheKey, AnnotatedType> cache) This is the method underlying bothannotate(Type)andannotate(Type, Annotation[]).static AnnotatedTypeannotate(Type type, Annotation[] annotations) Recursively wraps aTypeinto anAnnotatedTypeusing the annotations found on the erasure classes, plus adding the provided annotations to the top levelTypeonly.static Typeprivate static voidbuildUpperBoundClassAndInterfaces(Type type, Set<Class<?>> result) Helper method for getUpperBoundClassAndInterfaces, adding the result to the given set.static AnnotatedParameterizedTypeApplies capture conversion to the given type.static AnnotatedTypecapture(AnnotatedType type) Applies capture conversion to the given type.private static booleanstatic booleanequals(AnnotatedType t1, AnnotatedType t2) Checks whether the two provided types are of the same structure and annotations on all levels.static Class<?> Returns the erasure of the given type.private static AnnotatedParameterizedTypeexpandClassGenerics(Class<?> type) private static AnnotatedTypeexpandGenerics(AnnotatedType type) private static voidextractVariables(AnnotatedParameterizedType resolvedTyped, AnnotatedParameterizedType unresolvedType, Class<?> declaringClass, VarMap variables) static AnnotatedTypeIf type is an array type, returns the annotated type of the component of the array.static TypegetArrayComponentType(Type type) If type is an array type, returns the type of the component of the array.private static AnnotatedType[]getArrayExactDirectSuperTypes(AnnotatedType arrayType) private static AnnotatedType[]Returns the direct supertypes of the given type.static AnnotatedTypegetExactFieldType(Field f, AnnotatedType declaringType) Resolves the exact type of the given field in the given type.static TypegetExactFieldType(Field f, Type type) Resolves the exact type of the given field in the given type.static AnnotatedType[]getExactParameterTypes(Executable exe, AnnotatedType declaringType) Resolves the exact annotated parameter types of the given method/constructor in the given type.static Type[]getExactParameterTypes(Executable exe, Type declaringType) Resolves the exact parameter types of the given method/constructor in the given type.static AnnotatedTypegetExactReturnType(Method m, AnnotatedType declaringType) Resolves the exact return type of the given method in the given type.static TypegetExactReturnType(Method m, Type declaringType) Resolves the exact return type of the given method in the given type.static AnnotatedTypegetExactSubType(AnnotatedType superType, Class<?> searchSubClass) The equivalent ofgetExactSubType(Type, Class)but works withAnnotatedTypesstatic TypegetExactSubType(Type superType, Class<?> searchSubClass) Finds the most specific subtype ofsuperTypewhose erasure issearchSubClass.static AnnotatedTypegetExactSuperType(AnnotatedType subType, Class<?> searchSuperClass) The equivalent ofgetExactSuperType(Type, Class)but works withAnnotatedTypesstatic TypegetExactSuperType(Type subType, Class<?> searchSuperClass) Finds the most specific supertype ofsubTypewhose erasure issearchSuperClass.static AnnotatedTypegetFieldType(Field f, AnnotatedType declaringType) Resolves the type of the given field in the given type.private static AnnotatedTypegetFieldType(Field f, AnnotatedType declaringType, VarMap.MappingMode mappingMode) static TypegetFieldType(Field f, Type type) Resolves the type of the given field in the given type.static AnnotatedType[]getParameterTypes(Executable exe, AnnotatedType declaringType) private static AnnotatedType[]getParameterTypes(Executable exe, AnnotatedType declaringType, VarMap.MappingMode mappingMode) static Type[]getParameterTypes(Executable exe, Type declaringType) static AnnotatedTypegetReturnType(Method m, AnnotatedType declaringType) Resolves the return type of the given method in the given type.private static AnnotatedTypegetReturnType(Method m, AnnotatedType declaringType, VarMap.MappingMode mappingMode) static TypegetReturnType(Method m, Type declaringType) Resolves the return type of the given method in the given type.static StringgetTypeName(Type type) Returns the display name of a Type.static AnnotatedTypegetTypeParameter(AnnotatedType type, TypeVariable<? extends Class<?>> variable) Gets the type parameter for a given type that is the value for a given type variable.static TypegetTypeParameter(Type type, TypeVariable<? extends Class<?>> variable) Returns list of classes and interfaces that are supertypes of the given type.static inthashCode(AnnotatedType... types) (package private) static inthashCode(Stream<Annotation> annotations) private static booleanisArraySupertype(Type arraySuperType, Type subType) static booleanstatic booleanisFullyBound(Type type) static booleanisMissingTypeParameters(Type type) Checks if the given type is a class that is supposed to have type parameters, but doesn't.static booleanisSuperType(Type superType, Type subType) Checks if the capture of subType is a subtype of superTypeprivate static <I,O> O[] mapArray(I[] array, IntFunction<O[]> resultCtor, Function<I, O> mapper) private static AnnotatedTypemapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams) Maps type parameters in a type to their values.private static AnnotatedTypemapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode) static Annotation[]merge(Annotation[]... annotations) Merges an arbitrary number of annotations arrays, and removes duplicates.static <T extends AnnotatedType>
TmergeAnnotations(T t1, T t2) static AnnotatedTypereduceBounded(AnnotatedType type) Recursively traverses the structure of the given type, reducing all bounded types (AnnotatedTypeVariable,AnnotatedWildcardTypeandAnnotatedCaptureType) to their first bound.static <T extends AnnotatedType>
TreplaceAnnotations(T original, Annotation[] annotations) Creates a newAnnotatedTypeof the same type as the original, but with its annotations replaced with the provided ones.private static AnnotatedParameterizedTypereplaceParameters(AnnotatedParameterizedType type, Annotation[] annotations, AnnotatedType[] typeParameters, AnnotatedType ownerType) static AnnotatedParameterizedTypereplaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters) Creates a newAnnotatedParameterizedTypeof the same raw class as the providedtypeby with all of its type parameters replaced bytypeParameters.static AnnotatedParameterizedTypereplaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters, AnnotatedType ownerType) static AnnotatedTyperesolveExactType(AnnotatedType unresolved, AnnotatedType typeAndParams) static TyperesolveExactType(Type unresolved, Type typeAndParams) static AnnotatedTyperesolveType(AnnotatedType unresolved, AnnotatedType typeAndParams) private static AnnotatedTyperesolveType(AnnotatedType unresolved, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode) static TyperesolveType(Type unresolved, Type typeAndParams) static <T extends AnnotatedType>
TtoCanonical(T type) Returns anAnnotatedTypefunctionally identical to the given one, but in a canonical form that implementsequalsandhashCode.private static <T extends AnnotatedType>
TtoCanonical(T type, Function<Type, Type> leafTransformer) This is the method underlyingtoCanonical(AnnotatedType).static <T extends AnnotatedType>
TtoCanonicalBoxed(T type) Returns anAnnotatedTypefunctionally identical to the given one, but in a canonical form that implementsequalsandhashCodeand has all the primitives replaced by their boxed form.static AnnotatedTypetransform(AnnotatedType type, TypeVisitor visitor) Recursively applies a transformation implemented by the providedTypeVisitorto the structure of the given type.(package private) static booleantypeArraysEqual(AnnotatedType[] t1, AnnotatedType[] t2) static <T extends AnnotatedType>
TupdateAnnotations(T original, Annotation[] annotations) Creates a newAnnotatedTypeof the same structure as the original, but with its annotations replaced with the provided ones.
-
Field Details
-
UNBOUND_WILDCARD
-
BOX_TYPES
-
-
Constructor Details
-
GenericTypeReflector
public GenericTypeReflector()
-
-
Method Details
-
erase
-
box
-
isBoxType
-
isFullyBound
-
mapTypeParameters
private static AnnotatedType mapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams) Maps type parameters in a type to their values.- Parameters:
toMapType- Type possibly containing type argumentstypeAndParams- must be either ParameterizedType, or (in case there are no type arguments, or it's a raw type) Class- Returns:
- toMapType, but with type parameters from typeAndParams replaced.
-
mapTypeParameters
private static AnnotatedType mapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode) -
resolveExactType
-
resolveExactType
-
resolveType
-
resolveType
-
resolveType
private static AnnotatedType resolveType(AnnotatedType unresolved, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode) -
isMissingTypeParameters
Checks if the given type is a class that is supposed to have type parameters, but doesn't. In other words, if it's a really raw type. -
addWildcardParameters
Returns a type representing the class, with all type parameters the unbound wildcard ("?"). For example,addWildcardParameters(Map.class)returns a type representingMap<?,?>.- Returns:
- If clazz is a class or interface without type parameters, clazz itself is returned.
- If clazz is a class or interface with type parameters, an instance of ParameterizedType is returned.
- if clazz is an array type, an array type is returned with unbound wildcard parameters added in the the component type.
-
getExactSuperType
The equivalent ofgetExactSuperType(Type, Class)but works withAnnotatedTypes- Parameters:
subType- The type whose supertype is to be searched forsearchSuperClass- The class of the supertype to search for- Returns:
- The annotated type representing
searchSuperClasswith type parameters fromsubType
-
getExactSuperType
Finds the most specific supertype ofsubTypewhose erasure issearchSuperClass. In other words, returns a type representing the classsearchSuperClassplus its exact type parameters insubType.- Returns an instance of
ParameterizedTypeifsearchSuperClassis a real class or interface andsubTypehas parameters for it - Returns an instance of
GenericArrayTypeifsearchSuperClassis an array type, andsubTypehas type parameters for it - Returns an instance of
ClassifsubTypeis a raw type, or has no type parameters forsearchSuperClass - Returns null if
searchSuperClassis not a superclass ofsubType.
For example, with
class StringList implements List<String>,getExactSuperType(StringList.class, Collection.class)returns aParameterizedTyperepresentingCollection<String>.- Parameters:
subType- The type whose supertype is to be searched forsearchSuperClass- The class of the supertype to search for- Returns:
- The type representing
searchSuperClasswith type parameters fromsubType
- Returns an instance of
-
getExactSubType
The equivalent ofgetExactSubType(Type, Class)but works withAnnotatedTypes- Parameters:
superType- The type whose subtype is to be searched forsearchSubClass- The class of the subtype to search for- Returns:
- The annotated type representing
searchSubClasswith type parameters fromsuperType
-
getExactSubType
Finds the most specific subtype ofsuperTypewhose erasure issearchSubClass. In other words, returns a type representing the classsearchSubClassplus its exact type parameters insuperType, if they are possible to resolve.- Returns an instance of
AnnotatedParameterizedTypeifsearchSubClassis a real class or interface andsuperTypehas parameters for it - Returns an instance of
AnnotatedArrayTypeifsearchSubClassis an array type, andsuperTypehas type parameters for it - Returns an instance of
AnnotatedTypeifsuperTypeis a raw type, or has no type parameters forsearchSubClass - Returns null if
searchSubClassis not a subclass ofsuperType.
For example, with
getExactSubType(new TypeToken<List<String>>(){}.getAnnotatedType(), ArrayList.class)returns aAnnotatedParameterizedTyperepresentingArrayList<String>. - Returns an instance of
-
getTypeParameter
public static AnnotatedType getTypeParameter(AnnotatedType type, TypeVariable<? extends Class<?>> variable) Gets the type parameter for a given type that is the value for a given type variable. For example, withclass StringList implements List<String>,getTypeParameter(StringList.class, Collection.class.getTypeParameters()[0])returnsString.- Parameters:
type- The type to inspect.variable- The type variable to find the value for.- Returns:
- The type parameter for the given variable. Or null if type is not a subtype of the type that declares the variable, or if the variable isn't known (because of raw types).
-
getTypeParameter
-
isSuperType
-
isArraySupertype
-
getArrayComponentType
If type is an array type, returns the annotated type of the component of the array. Otherwise, returns null. -
getArrayComponentType
-
contains
-
extractVariables
private static void extractVariables(AnnotatedParameterizedType resolvedTyped, AnnotatedParameterizedType unresolvedType, Class<?> declaringClass, VarMap variables) -
getExactDirectSuperTypes
Returns the direct supertypes of the given type. Resolves type parameters. -
getArrayExactDirectSuperTypes
-
getExactReturnType
Resolves the exact return type of the given method in the given type. This may be different fromm.getAnnotatedReturnType()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in the return type, ordeclaringTypeis a raw type. -
getExactReturnType
Resolves the exact return type of the given method in the given type. This may be different fromm.getGenericReturnType()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in the return type, ordeclaringTypeis a raw type. -
getReturnType
Resolves the return type of the given method in the given type. Any unresolvable variables will be kept (in contrast togetExactReturnType(Method, AnnotatedType)). This may be different fromm.getAnnotatedReturnType()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in the return type, ordeclaringTypeis a raw type. -
getReturnType
Resolves the return type of the given method in the given type. Any unresolvable variables will be kept (in contrast togetExactReturnType(Method, Type)). This may be different fromm.getGenericReturnType()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in the return type, ordeclaringTypeis a raw type. -
getReturnType
private static AnnotatedType getReturnType(Method m, AnnotatedType declaringType, VarMap.MappingMode mappingMode) -
getExactFieldType
Resolves the exact type of the given field in the given type. This may be different fromf.getAnnotatedType()when the field was declared in a superclass, ordeclaringTypehas a type parameter that is used in the type of the field, ordeclaringTypeis a raw type. -
getExactFieldType
Resolves the exact type of the given field in the given type. This may be different fromf.getGenericType()when the field was declared in a superclass, ordeclaringTypehas a type parameter that is used in the type of the field, ordeclaringTypeis a raw type. -
getFieldType
Resolves the type of the given field in the given type. Any unresolvable variables will be kept (in contrast togetExactFieldType(Field, AnnotatedType)). This may be different fromf.getAnnotatedType()when the field was declared in a superclass, ordeclaringTypehas a type parameter that is used in the type of the field, ordeclaringTypeis a raw type. -
getFieldType
Resolves the type of the given field in the given type. Any unresolvable variables will be kept (in contrast togetExactFieldType(Field, Type)). This may be different fromf.getGenericType()when the field was declared in a superclass, ordeclaringTypehas a type parameter that is used in the type of the field, ordeclaringTypeis a raw type. -
getFieldType
private static AnnotatedType getFieldType(Field f, AnnotatedType declaringType, VarMap.MappingMode mappingMode) -
getExactParameterTypes
Resolves the exact annotated parameter types of the given method/constructor in the given type. This may be different fromexe.getAnnotatedParameterTypes()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in one of the parameters, ordeclaringTypeis a raw type. -
getExactParameterTypes
Resolves the exact parameter types of the given method/constructor in the given type. This may be different fromexe.getParameterTypes()when the method was declared in a superclass, ordeclaringTypehas a type parameter that is used in one of the parameters, ordeclaringTypeis a raw type. -
getParameterTypes
-
getParameterTypes
-
getParameterTypes
private static AnnotatedType[] getParameterTypes(Executable exe, AnnotatedType declaringType, VarMap.MappingMode mappingMode) -
capture
Applies capture conversion to the given type. -
capture
Applies capture conversion to the given type.- See Also:
-
getTypeName
-
getUpperBoundClassAndInterfaces
Returns list of classes and interfaces that are supertypes of the given type. For example given this class:class {@literal Foo<A extends Number & Iterable<A>, B extends A>}
calling this method on type parametersB(Foo.class.getTypeParameters()[1]) returns a list containingNumberandIterable.This is mostly useful if you get a type from one of the other methods in
GenericTypeReflector, but you don't want to deal with all the different sorts of types, and you are only really interested in concrete classes and interfaces.- Returns:
- A List of classes, each of them a supertype of the given type. If the given type is a class or interface itself, returns a List with just the given type. The list contains no duplicates, and is ordered in the order the upper bounds are defined on the type.
-
annotate
-
annotate
Recursively wraps aTypeinto anAnnotatedTypeusing the annotations found on the erasure classes.- Parameters:
type- Type to annotate- Returns:
- Type whose structure has been recursively annotated
-
annotate
Recursively wraps aTypeinto anAnnotatedTypeusing the annotations found on the erasure classes, plus adding the provided annotations to the top levelTypeonly.- Parameters:
type- Type to annotate- Returns:
- Type whose structure has been recursively annotated, plus the provided annotation added
at the top level
See
annotate(Type)
-
annotate
private static AnnotatedType annotate(Type type, boolean expandGenerics, Map<GenericTypeReflector.CaptureCacheKey, AnnotatedType> cache) This is the method underlying bothannotate(Type)andannotate(Type, Annotation[]). It goes recursively through the structure of the providedTypewrapping all type parameters, bounds etc. encountered intoAnnotatedTypes using annotations found directly on the corresponding erasure class, with a special treatment forCaptureTypewhich can have infinitely recursive structure by having itself as its upper bound.- Parameters:
type- The type to annotatecache- The cache for already encounteredCaptureTypes. Necessary becauseCaptureTypes can have infinitely recursive structure.- Returns:
- Type whose structure has been recursively annotated
See
annotate(Type)See
annotate(Type, Annotation[])See
GenericTypeReflector.CaptureCacheKeySee
CaptureType
-
replaceAnnotations
Creates a newAnnotatedTypeof the same type as the original, but with its annotations replaced with the provided ones.- Parameters:
original- The type whose structure is to be copiedannotations- Annotations to use instead of the ones found on theoriginal- Returns:
- A type of the same structure as the original but with replaced annotations
-
updateAnnotations
Creates a newAnnotatedTypeof the same structure as the original, but with its annotations replaced with the provided ones.- Parameters:
original- The type whose structure is to be copiedannotations- Extra annotations to be added on top of the ones found on theoriginal- Returns:
- A type of the same structure as the original but with replaced annotations
-
mergeAnnotations
-
replaceParameters
public static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters) Creates a newAnnotatedParameterizedTypeof the same raw class as the providedtypeby with all of its type parameters replaced bytypeParameters.- Parameters:
type- The original parameterized type from which the raw class is to be takentypeParameters- The new type parameters to use- Returns:
- The new parameterized type
-
replaceParameters
public static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters, AnnotatedType ownerType) -
replaceParameters
private static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, Annotation[] annotations, AnnotatedType[] typeParameters, AnnotatedType ownerType) -
toCanonical
Returns anAnnotatedTypefunctionally identical to the given one, but in a canonical form that implementsequalsandhashCode.- Parameters:
type- The type to turn into the canonical form- Returns:
- A type functionally equivalent to the given one, but in the canonical form
-
toCanonicalBoxed
Returns anAnnotatedTypefunctionally identical to the given one, but in a canonical form that implementsequalsandhashCodeand has all the primitives replaced by their boxed form.- Parameters:
type- The type to turn into the canonical form- Returns:
- A type functionally equivalent to the given one, but in the canonical form
-
toCanonical
This is the method underlyingtoCanonical(AnnotatedType). It recursively traverses the structure of the providedAnnotatedTypeturning all type parameters, bounds etc. into their canonical forms, with a special treatment forAnnotatedCaptureTypewhich can have infinitely recursive structure by having itself as its upper bound.- Parameters:
type- The type to annotateleafTransformer- The transformer function to apply to leaf types (e.g. to box primitives)- Returns:
- Type whose structure has been recursively annotated
See
toCanonical(AnnotatedType)See
GenericTypeReflector.AnnotatedCaptureCacheKeySee
CaptureType
-
expandGenerics
-
transform
Recursively applies a transformation implemented by the providedTypeVisitorto the structure of the given type.- Parameters:
type- The type to transformvisitor- Visitor implementing the transformation- Returns:
- Type produced by recursively applying the transformation to the given type
-
reduceBounded
Recursively traverses the structure of the given type, reducing all bounded types (AnnotatedTypeVariable,AnnotatedWildcardTypeandAnnotatedCaptureType) to their first bound.- Parameters:
type- The type to transform- Returns:
- Type produced by recursively reducing bounded types within the structure of the given type
-
expandClassGenerics
-
merge
Merges an arbitrary number of annotations arrays, and removes duplicates.- Parameters:
annotations- Annotation arrays to merge and deduplicate- Returns:
- An array containing all annotations from the given arrays, without duplicates
-
typeArraysEqual
-
hashCode
-
hashCode
-
equals
Checks whether the two provided types are of the same structure and annotations on all levels.- Parameters:
t1- The first type to be comparedt2- The second type to be compared- Returns:
- True if both types have the same structure and annotations on all levels
-
buildUpperBoundClassAndInterfaces
-
mapArray
-