- java.lang.Object
-
- io.leangen.geantyref.GenericTypeReflector
-
public class GenericTypeReflector extends java.lang.Object
Utility class for doing reflection on types.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GenericTypeReflector.AnnotatedCaptureCacheKey
(package private) static class
GenericTypeReflector.CaptureCacheKey
A key representing aCaptureType
.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>
BOX_TYPES
private static java.lang.reflect.WildcardType
UNBOUND_WILDCARD
-
Constructor Summary
Constructors Constructor Description GenericTypeReflector()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.reflect.Type
addWildcardParameters(java.lang.Class<?> clazz)
Returns a type representing the class, with all type parameters the unbound wildcard ("?").static java.lang.reflect.AnnotatedType
annotate(java.lang.reflect.Type type)
Recursively wraps aType
into anAnnotatedType
using the annotations found on the erasure classes.private static java.lang.reflect.AnnotatedType
annotate(java.lang.reflect.Type type, boolean expandGenerics)
private static java.lang.reflect.AnnotatedType
annotate(java.lang.reflect.Type type, boolean expandGenerics, java.util.Map<GenericTypeReflector.CaptureCacheKey,java.lang.reflect.AnnotatedType> cache)
This is the method underlying bothannotate(Type)
andannotate(Type, Annotation[])
.static java.lang.reflect.AnnotatedType
annotate(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
Recursively wraps aType
into anAnnotatedType
using the annotations found on the erasure classes, plus adding the provided annotations to the top levelType
only.static java.lang.reflect.Type
box(java.lang.reflect.Type type)
private static void
buildUpperBoundClassAndInterfaces(java.lang.reflect.Type type, java.util.Set<java.lang.Class<?>> result)
Helper method for getUpperBoundClassAndInterfaces, adding the result to the given set.static java.lang.reflect.AnnotatedParameterizedType
capture(java.lang.reflect.AnnotatedParameterizedType type)
Applies capture conversion to the given type.static java.lang.reflect.AnnotatedType
capture(java.lang.reflect.AnnotatedType type)
Applies capture conversion to the given type.private static boolean
contains(java.lang.reflect.Type containingType, java.lang.reflect.Type containedType)
static boolean
equals(java.lang.reflect.AnnotatedType t1, java.lang.reflect.AnnotatedType t2)
Checks whether the two provided types are of the same structure and annotations on all levels.static java.lang.Class<?>
erase(java.lang.reflect.Type type)
Returns the erasure of the given type.private static java.lang.reflect.AnnotatedParameterizedType
expandClassGenerics(java.lang.Class<?> type)
private static java.lang.reflect.AnnotatedType
expandGenerics(java.lang.reflect.AnnotatedType type)
private static void
extractVariables(java.lang.reflect.AnnotatedParameterizedType resolvedTyped, java.lang.reflect.AnnotatedParameterizedType unresolvedType, java.lang.Class<?> declaringClass, VarMap variables)
static java.lang.reflect.AnnotatedType
getArrayComponentType(java.lang.reflect.AnnotatedType type)
If type is an array type, returns the annotated type of the component of the array.static java.lang.reflect.Type
getArrayComponentType(java.lang.reflect.Type type)
If type is an array type, returns the type of the component of the array.private static java.lang.reflect.AnnotatedType[]
getArrayExactDirectSuperTypes(java.lang.reflect.AnnotatedType arrayType)
private static java.lang.reflect.AnnotatedType[]
getExactDirectSuperTypes(java.lang.reflect.AnnotatedType type)
Returns the direct supertypes of the given type.static java.lang.reflect.AnnotatedType
getExactFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType)
Resolves the exact type of the given field in the given type.static java.lang.reflect.Type
getExactFieldType(java.lang.reflect.Field f, java.lang.reflect.Type type)
Resolves the exact type of the given field in the given type.static java.lang.reflect.AnnotatedType[]
getExactParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType)
Resolves the exact annotated parameter types of the given method/constructor in the given type.static java.lang.reflect.Type[]
getExactParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.Type declaringType)
Resolves the exact parameter types of the given method/constructor in the given type.static java.lang.reflect.AnnotatedType
getExactReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType)
Resolves the exact return type of the given method in the given type.static java.lang.reflect.Type
getExactReturnType(java.lang.reflect.Method m, java.lang.reflect.Type declaringType)
Resolves the exact return type of the given method in the given type.static java.lang.reflect.AnnotatedType
getExactSubType(java.lang.reflect.AnnotatedType superType, java.lang.Class<?> searchSubClass)
The equivalent ofgetExactSubType(Type, Class)
but works withAnnotatedType
sstatic java.lang.reflect.Type
getExactSubType(java.lang.reflect.Type superType, java.lang.Class<?> searchSubClass)
Finds the most specific subtype ofsuperType
whose erasure issearchSubClass
.static java.lang.reflect.AnnotatedType
getExactSuperType(java.lang.reflect.AnnotatedType subType, java.lang.Class<?> searchSuperClass)
The equivalent ofgetExactSuperType(Type, Class)
but works withAnnotatedType
sstatic java.lang.reflect.Type
getExactSuperType(java.lang.reflect.Type subType, java.lang.Class<?> searchSuperClass)
Finds the most specific supertype ofsubType
whose erasure issearchSuperClass
.static java.lang.reflect.AnnotatedType
getFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType)
Resolves the type of the given field in the given type.private static java.lang.reflect.AnnotatedType
getFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
static java.lang.reflect.Type
getFieldType(java.lang.reflect.Field f, java.lang.reflect.Type type)
Resolves the type of the given field in the given type.static java.lang.reflect.AnnotatedType[]
getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType)
private static java.lang.reflect.AnnotatedType[]
getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
static java.lang.reflect.Type[]
getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.Type declaringType)
static java.lang.reflect.AnnotatedType
getReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType)
Resolves the return type of the given method in the given type.private static java.lang.reflect.AnnotatedType
getReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
static java.lang.reflect.Type
getReturnType(java.lang.reflect.Method m, java.lang.reflect.Type declaringType)
Resolves the return type of the given method in the given type.static java.lang.String
getTypeName(java.lang.reflect.Type type)
Returns the display name of a Type.static java.lang.reflect.AnnotatedType
getTypeParameter(java.lang.reflect.AnnotatedType type, java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
Gets the type parameter for a given type that is the value for a given type variable.static java.lang.reflect.Type
getTypeParameter(java.lang.reflect.Type type, java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
static java.util.List<java.lang.Class<?>>
getUpperBoundClassAndInterfaces(java.lang.reflect.Type type)
Returns list of classes and interfaces that are supertypes of the given type.static int
hashCode(java.lang.reflect.AnnotatedType... types)
(package private) static int
hashCode(java.util.stream.Stream<java.lang.annotation.Annotation> annotations)
private static boolean
isArraySupertype(java.lang.reflect.Type arraySuperType, java.lang.reflect.Type subType)
static boolean
isBoxType(java.lang.reflect.Type type)
static boolean
isFullyBound(java.lang.reflect.Type type)
static boolean
isMissingTypeParameters(java.lang.reflect.Type type)
Checks if the given type is a class that is supposed to have type parameters, but doesn't.static boolean
isSuperType(java.lang.reflect.Type superType, java.lang.reflect.Type subType)
Checks if the capture of subType is a subtype of superTypeprivate static <I,O>
O[]mapArray(I[] array, java.util.function.IntFunction<O[]> resultCtor, java.util.function.Function<I,O> mapper)
private static java.lang.reflect.AnnotatedType
mapTypeParameters(java.lang.reflect.AnnotatedType toMapType, java.lang.reflect.AnnotatedType typeAndParams)
Maps type parameters in a type to their values.private static java.lang.reflect.AnnotatedType
mapTypeParameters(java.lang.reflect.AnnotatedType toMapType, java.lang.reflect.AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
static java.lang.annotation.Annotation[]
merge(java.lang.annotation.Annotation[]... annotations)
Merges an arbitrary number of annotations arrays, and removes duplicates.static <T extends java.lang.reflect.AnnotatedType>
TmergeAnnotations(T t1, T t2)
static java.lang.reflect.AnnotatedType
reduceBounded(java.lang.reflect.AnnotatedType type)
Recursively traverses the structure of the given type, reducing all bounded types (AnnotatedTypeVariable
,AnnotatedWildcardType
andAnnotatedCaptureType
) to their first bound.static <T extends java.lang.reflect.AnnotatedType>
TreplaceAnnotations(T original, java.lang.annotation.Annotation[] annotations)
Creates a newAnnotatedType
of the same type as the original, but with its annotations replaced with the provided ones.private static java.lang.reflect.AnnotatedParameterizedType
replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.annotation.Annotation[] annotations, java.lang.reflect.AnnotatedType[] typeParameters, java.lang.reflect.AnnotatedType ownerType)
static java.lang.reflect.AnnotatedParameterizedType
replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.reflect.AnnotatedType[] typeParameters)
Creates a newAnnotatedParameterizedType
of the same raw class as the providedtype
by with all of its type parameters replaced bytypeParameters
.static java.lang.reflect.AnnotatedParameterizedType
replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.reflect.AnnotatedType[] typeParameters, java.lang.reflect.AnnotatedType ownerType)
static java.lang.reflect.AnnotatedType
resolveExactType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams)
static java.lang.reflect.Type
resolveExactType(java.lang.reflect.Type unresolved, java.lang.reflect.Type typeAndParams)
static java.lang.reflect.AnnotatedType
resolveType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams)
private static java.lang.reflect.AnnotatedType
resolveType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
static java.lang.reflect.Type
resolveType(java.lang.reflect.Type unresolved, java.lang.reflect.Type typeAndParams)
static <T extends java.lang.reflect.AnnotatedType>
TtoCanonical(T type)
Returns anAnnotatedType
functionally identical to the given one, but in a canonical form that implementsequals
andhashCode
.private static <T extends java.lang.reflect.AnnotatedType>
TtoCanonical(T type, java.util.function.Function<java.lang.reflect.Type,java.lang.reflect.Type> leafTransformer)
This is the method underlyingtoCanonical(AnnotatedType)
.static <T extends java.lang.reflect.AnnotatedType>
TtoCanonicalBoxed(T type)
Returns anAnnotatedType
functionally identical to the given one, but in a canonical form that implementsequals
andhashCode
and has all the primitives replaced by their boxed form.static java.lang.reflect.AnnotatedType
transform(java.lang.reflect.AnnotatedType type, TypeVisitor visitor)
Recursively applies a transformation implemented by the providedTypeVisitor
to the structure of the given type.(package private) static boolean
typeArraysEqual(java.lang.reflect.AnnotatedType[] t1, java.lang.reflect.AnnotatedType[] t2)
static <T extends java.lang.reflect.AnnotatedType>
TupdateAnnotations(T original, java.lang.annotation.Annotation[] annotations)
Creates a newAnnotatedType
of the same structure as the original, but with its annotations replaced with the provided ones.
-
-
-
Method Detail
-
erase
public static java.lang.Class<?> erase(java.lang.reflect.Type type)
Returns the erasure of the given type.
-
box
public static java.lang.reflect.Type box(java.lang.reflect.Type type)
-
isBoxType
public static boolean isBoxType(java.lang.reflect.Type type)
-
isFullyBound
public static boolean isFullyBound(java.lang.reflect.Type type)
-
mapTypeParameters
private static java.lang.reflect.AnnotatedType mapTypeParameters(java.lang.reflect.AnnotatedType toMapType, java.lang.reflect.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 java.lang.reflect.AnnotatedType mapTypeParameters(java.lang.reflect.AnnotatedType toMapType, java.lang.reflect.AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
-
resolveExactType
public static java.lang.reflect.AnnotatedType resolveExactType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams)
-
resolveExactType
public static java.lang.reflect.Type resolveExactType(java.lang.reflect.Type unresolved, java.lang.reflect.Type typeAndParams)
-
resolveType
public static java.lang.reflect.AnnotatedType resolveType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams)
-
resolveType
public static java.lang.reflect.Type resolveType(java.lang.reflect.Type unresolved, java.lang.reflect.Type typeAndParams)
-
resolveType
private static java.lang.reflect.AnnotatedType resolveType(java.lang.reflect.AnnotatedType unresolved, java.lang.reflect.AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
-
isMissingTypeParameters
public static boolean isMissingTypeParameters(java.lang.reflect.Type type)
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
public static java.lang.reflect.Type addWildcardParameters(java.lang.Class<?> clazz)
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
public static java.lang.reflect.AnnotatedType getExactSuperType(java.lang.reflect.AnnotatedType subType, java.lang.Class<?> searchSuperClass)
The equivalent ofgetExactSuperType(Type, Class)
but works withAnnotatedType
s- Parameters:
subType
- The type whose supertype is to be searched forsearchSuperClass
- The class of the supertype to search for- Returns:
- The annotated type representing
searchSuperClass
with type parameters fromsubType
-
getExactSuperType
public static java.lang.reflect.Type getExactSuperType(java.lang.reflect.Type subType, java.lang.Class<?> searchSuperClass)
Finds the most specific supertype ofsubType
whose erasure issearchSuperClass
. In other words, returns a type representing the classsearchSuperClass
plus its exact type parameters insubType
.- Returns an instance of
ParameterizedType
ifsearchSuperClass
is a real class or interface andsubType
has parameters for it - Returns an instance of
GenericArrayType
ifsearchSuperClass
is an array type, andsubType
has type parameters for it - Returns an instance of
Class
ifsubType
is a raw type, or has no type parameters forsearchSuperClass
- Returns null if
searchSuperClass
is not a superclass ofsubType
.
For example, with
class StringList implements List<String>
,getExactSuperType(StringList.class, Collection.class)
returns aParameterizedType
representingCollection<String>
.- Parameters:
subType
- The type whose supertype is to be searched forsearchSuperClass
- The class of the supertype to search for- Returns:
- The type representing
searchSuperClass
with type parameters fromsubType
- Returns an instance of
-
getExactSubType
public static java.lang.reflect.AnnotatedType getExactSubType(java.lang.reflect.AnnotatedType superType, java.lang.Class<?> searchSubClass)
The equivalent ofgetExactSubType(Type, Class)
but works withAnnotatedType
s- Parameters:
superType
- The type whose subtype is to be searched forsearchSubClass
- The class of the subtype to search for- Returns:
- The annotated type representing
searchSubClass
with type parameters fromsuperType
-
getExactSubType
public static java.lang.reflect.Type getExactSubType(java.lang.reflect.Type superType, java.lang.Class<?> searchSubClass)
Finds the most specific subtype ofsuperType
whose erasure issearchSubClass
. In other words, returns a type representing the classsearchSubClass
plus its exact type parameters insuperType
, if they are possible to resolve.- Returns an instance of
AnnotatedParameterizedType
ifsearchSubClass
is a real class or interface andsuperType
has parameters for it - Returns an instance of
AnnotatedArrayType
ifsearchSubClass
is an array type, andsuperType
has type parameters for it - Returns an instance of
AnnotatedType
ifsuperType
is a raw type, or has no type parameters forsearchSubClass
- Returns null if
searchSubClass
is not a subclass ofsuperType
.
For example, with
getExactSubType(new TypeToken<List<String>>(){}.getAnnotatedType(), ArrayList.class)
returns aAnnotatedParameterizedType
representingArrayList<String>
. - Returns an instance of
-
getTypeParameter
public static java.lang.reflect.AnnotatedType getTypeParameter(java.lang.reflect.AnnotatedType type, java.lang.reflect.TypeVariable<? extends java.lang.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
public static java.lang.reflect.Type getTypeParameter(java.lang.reflect.Type type, java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
-
isSuperType
public static boolean isSuperType(java.lang.reflect.Type superType, java.lang.reflect.Type subType)
Checks if the capture of subType is a subtype of superType
-
isArraySupertype
private static boolean isArraySupertype(java.lang.reflect.Type arraySuperType, java.lang.reflect.Type subType)
-
getArrayComponentType
public static java.lang.reflect.AnnotatedType getArrayComponentType(java.lang.reflect.AnnotatedType type)
If type is an array type, returns the annotated type of the component of the array. Otherwise, returns null.
-
getArrayComponentType
public static java.lang.reflect.Type getArrayComponentType(java.lang.reflect.Type type)
If type is an array type, returns the type of the component of the array. Otherwise, returns null.
-
contains
private static boolean contains(java.lang.reflect.Type containingType, java.lang.reflect.Type containedType)
-
extractVariables
private static void extractVariables(java.lang.reflect.AnnotatedParameterizedType resolvedTyped, java.lang.reflect.AnnotatedParameterizedType unresolvedType, java.lang.Class<?> declaringClass, VarMap variables)
-
getExactDirectSuperTypes
private static java.lang.reflect.AnnotatedType[] getExactDirectSuperTypes(java.lang.reflect.AnnotatedType type)
Returns the direct supertypes of the given type. Resolves type parameters.
-
getArrayExactDirectSuperTypes
private static java.lang.reflect.AnnotatedType[] getArrayExactDirectSuperTypes(java.lang.reflect.AnnotatedType arrayType)
-
getExactReturnType
public static java.lang.reflect.AnnotatedType getExactReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType)
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, ordeclaringType
has a type parameter that is used in the return type, ordeclaringType
is a raw type.
-
getExactReturnType
public static java.lang.reflect.Type getExactReturnType(java.lang.reflect.Method m, java.lang.reflect.Type declaringType)
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, ordeclaringType
has a type parameter that is used in the return type, ordeclaringType
is a raw type.
-
getReturnType
public static java.lang.reflect.AnnotatedType getReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType)
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, ordeclaringType
has a type parameter that is used in the return type, ordeclaringType
is a raw type.
-
getReturnType
public static java.lang.reflect.Type getReturnType(java.lang.reflect.Method m, java.lang.reflect.Type declaringType)
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, ordeclaringType
has a type parameter that is used in the return type, ordeclaringType
is a raw type.
-
getReturnType
private static java.lang.reflect.AnnotatedType getReturnType(java.lang.reflect.Method m, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
-
getExactFieldType
public static java.lang.reflect.AnnotatedType getExactFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType)
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, ordeclaringType
has a type parameter that is used in the type of the field, ordeclaringType
is a raw type.
-
getExactFieldType
public static java.lang.reflect.Type getExactFieldType(java.lang.reflect.Field f, java.lang.reflect.Type type)
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, ordeclaringType
has a type parameter that is used in the type of the field, ordeclaringType
is a raw type.
-
getFieldType
public static java.lang.reflect.AnnotatedType getFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType)
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, ordeclaringType
has a type parameter that is used in the type of the field, ordeclaringType
is a raw type.
-
getFieldType
public static java.lang.reflect.Type getFieldType(java.lang.reflect.Field f, java.lang.reflect.Type type)
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, ordeclaringType
has a type parameter that is used in the type of the field, ordeclaringType
is a raw type.
-
getFieldType
private static java.lang.reflect.AnnotatedType getFieldType(java.lang.reflect.Field f, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
-
getExactParameterTypes
public static java.lang.reflect.AnnotatedType[] getExactParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType)
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, ordeclaringType
has a type parameter that is used in one of the parameters, ordeclaringType
is a raw type.
-
getExactParameterTypes
public static java.lang.reflect.Type[] getExactParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.Type declaringType)
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, ordeclaringType
has a type parameter that is used in one of the parameters, ordeclaringType
is a raw type.
-
getParameterTypes
public static java.lang.reflect.AnnotatedType[] getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType)
-
getParameterTypes
public static java.lang.reflect.Type[] getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.Type declaringType)
-
getParameterTypes
private static java.lang.reflect.AnnotatedType[] getParameterTypes(java.lang.reflect.Executable exe, java.lang.reflect.AnnotatedType declaringType, VarMap.MappingMode mappingMode)
-
capture
public static java.lang.reflect.AnnotatedType capture(java.lang.reflect.AnnotatedType type)
Applies capture conversion to the given type.
-
capture
public static java.lang.reflect.AnnotatedParameterizedType capture(java.lang.reflect.AnnotatedParameterizedType type)
Applies capture conversion to the given type.- See Also:
capture(AnnotatedType)
-
getTypeName
public static java.lang.String getTypeName(java.lang.reflect.Type type)
Returns the display name of a Type.
-
getUpperBoundClassAndInterfaces
public static java.util.List<java.lang.Class<?>> getUpperBoundClassAndInterfaces(java.lang.reflect.Type type)
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 containingNumber
andIterable
.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
private static java.lang.reflect.AnnotatedType annotate(java.lang.reflect.Type type, boolean expandGenerics)
-
annotate
public static java.lang.reflect.AnnotatedType annotate(java.lang.reflect.Type type)
Recursively wraps aType
into anAnnotatedType
using the annotations found on the erasure classes.- Parameters:
type
- Type to annotate- Returns:
- Type whose structure has been recursively annotated
-
annotate
public static java.lang.reflect.AnnotatedType annotate(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
Recursively wraps aType
into anAnnotatedType
using the annotations found on the erasure classes, plus adding the provided annotations to the top levelType
only.- 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 java.lang.reflect.AnnotatedType annotate(java.lang.reflect.Type type, boolean expandGenerics, java.util.Map<GenericTypeReflector.CaptureCacheKey,java.lang.reflect.AnnotatedType> cache)
This is the method underlying bothannotate(Type)
andannotate(Type, Annotation[])
. It goes recursively through the structure of the providedType
wrapping all type parameters, bounds etc. encountered intoAnnotatedType
s using annotations found directly on the corresponding erasure class, with a special treatment forCaptureType
which can have infinitely recursive structure by having itself as its upper bound.- Parameters:
type
- The type to annotatecache
- The cache for already encounteredCaptureType
s. Necessary becauseCaptureType
s can have infinitely recursive structure.- Returns:
- Type whose structure has been recursively annotated
See
annotate(Type)
See
annotate(Type, Annotation[])
See
GenericTypeReflector.CaptureCacheKey
See
CaptureType
-
replaceAnnotations
public static <T extends java.lang.reflect.AnnotatedType> T replaceAnnotations(T original, java.lang.annotation.Annotation[] annotations)
Creates a newAnnotatedType
of 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
public static <T extends java.lang.reflect.AnnotatedType> T updateAnnotations(T original, java.lang.annotation.Annotation[] annotations)
Creates a newAnnotatedType
of 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
public static <T extends java.lang.reflect.AnnotatedType> T mergeAnnotations(T t1, T t2)
-
replaceParameters
public static java.lang.reflect.AnnotatedParameterizedType replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.reflect.AnnotatedType[] typeParameters)
Creates a newAnnotatedParameterizedType
of the same raw class as the providedtype
by 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 java.lang.reflect.AnnotatedParameterizedType replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.reflect.AnnotatedType[] typeParameters, java.lang.reflect.AnnotatedType ownerType)
-
replaceParameters
private static java.lang.reflect.AnnotatedParameterizedType replaceParameters(java.lang.reflect.AnnotatedParameterizedType type, java.lang.annotation.Annotation[] annotations, java.lang.reflect.AnnotatedType[] typeParameters, java.lang.reflect.AnnotatedType ownerType)
-
toCanonical
public static <T extends java.lang.reflect.AnnotatedType> T toCanonical(T type)
Returns anAnnotatedType
functionally identical to the given one, but in a canonical form that implementsequals
andhashCode
.- 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
public static <T extends java.lang.reflect.AnnotatedType> T toCanonicalBoxed(T type)
Returns anAnnotatedType
functionally identical to the given one, but in a canonical form that implementsequals
andhashCode
and 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
private static <T extends java.lang.reflect.AnnotatedType> T toCanonical(T type, java.util.function.Function<java.lang.reflect.Type,java.lang.reflect.Type> leafTransformer)
This is the method underlyingtoCanonical(AnnotatedType)
. It recursively traverses the structure of the providedAnnotatedType
turning all type parameters, bounds etc. into their canonical forms, with a special treatment forAnnotatedCaptureType
which 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.AnnotatedCaptureCacheKey
See
CaptureType
-
expandGenerics
private static java.lang.reflect.AnnotatedType expandGenerics(java.lang.reflect.AnnotatedType type)
-
transform
public static java.lang.reflect.AnnotatedType transform(java.lang.reflect.AnnotatedType type, TypeVisitor visitor)
Recursively applies a transformation implemented by the providedTypeVisitor
to 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
public static java.lang.reflect.AnnotatedType reduceBounded(java.lang.reflect.AnnotatedType type)
Recursively traverses the structure of the given type, reducing all bounded types (AnnotatedTypeVariable
,AnnotatedWildcardType
andAnnotatedCaptureType
) 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
private static java.lang.reflect.AnnotatedParameterizedType expandClassGenerics(java.lang.Class<?> type)
-
merge
public static java.lang.annotation.Annotation[] merge(java.lang.annotation.Annotation[]... annotations)
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
static boolean typeArraysEqual(java.lang.reflect.AnnotatedType[] t1, java.lang.reflect.AnnotatedType[] t2)
-
hashCode
public static int hashCode(java.lang.reflect.AnnotatedType... types)
-
hashCode
static int hashCode(java.util.stream.Stream<java.lang.annotation.Annotation> annotations)
-
equals
public static boolean equals(java.lang.reflect.AnnotatedType t1, java.lang.reflect.AnnotatedType t2)
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
private static void buildUpperBoundClassAndInterfaces(java.lang.reflect.Type type, java.util.Set<java.lang.Class<?>> result)
Helper method for getUpperBoundClassAndInterfaces, adding the result to the given set.
-
mapArray
private static <I,O> O[] mapArray(I[] array, java.util.function.IntFunction<O[]> resultCtor, java.util.function.Function<I,O> mapper)
-
-