public class GenericTypeReflector extends Object
Constructor and Description |
---|
GenericTypeReflector() |
Modifier and Type | Method and Description |
---|---|
static Type |
addWildcardParameters(Class<?> clazz)
Returns a type representing the class, with all type parameters the unbound wildcard ("?").
|
static AnnotatedType |
annotate(Type type)
Recursively wraps a
Type into an AnnotatedType using the annotations found on
the erasure classes. |
static AnnotatedType |
annotate(Type type,
Annotation[] annotations)
Recursively wraps a
Type into an AnnotatedType using the annotations found on
the erasure classes, plus adding the provided annotations to the top level Type only. |
static Type |
box(Type type) |
static AnnotatedParameterizedType |
capture(AnnotatedParameterizedType type)
Applies capture conversion to the given type.
|
static AnnotatedType |
capture(AnnotatedType type)
Applies capture conversion to the given type.
|
static boolean |
equals(AnnotatedType t1,
AnnotatedType t2)
Checks whether the two provided types are of the same structure and annotations on all levels.
|
static Class<?> |
erase(Type type)
Returns the erasure of the given type.
|
static AnnotatedType |
getArrayComponentType(AnnotatedType type)
If type is an array type, returns the annotated type of the component of the array.
|
static Type |
getArrayComponentType(Type type)
If type is an array type, returns the type of the component of the array.
|
static AnnotatedType |
getExactFieldType(Field f,
AnnotatedType declaringType)
Resolves the exact type of the given field in the given type.
|
static Type |
getExactFieldType(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 AnnotatedType |
getExactReturnType(Method m,
AnnotatedType declaringType)
Resolves the exact return type of the given method in the given type.
|
static Type |
getExactReturnType(Method m,
Type declaringType)
Resolves the exact return type of the given method in the given type.
|
static AnnotatedType |
getExactSubType(AnnotatedType superType,
Class<?> searchSubClass)
The equivalent of
getExactSubType(Type, Class) but works with AnnotatedType s |
static Type |
getExactSubType(Type superType,
Class<?> searchSubClass)
Finds the most specific subtype of
superType whose erasure is searchSubClass . |
static AnnotatedType |
getExactSuperType(AnnotatedType subType,
Class<?> searchSuperClass)
The equivalent of
getExactSuperType(Type, Class) but works with AnnotatedType s |
static Type |
getExactSuperType(Type subType,
Class<?> searchSuperClass)
Finds the most specific supertype of
subType whose erasure is searchSuperClass . |
static AnnotatedType |
getFieldType(Field f,
AnnotatedType declaringType)
Resolves the type of the given field in the given type.
|
static Type |
getFieldType(Field f,
Type type)
Resolves the type of the given field in the given type.
|
static AnnotatedType[] |
getParameterTypes(Executable exe,
AnnotatedType declaringType) |
static Type[] |
getParameterTypes(Executable exe,
Type declaringType) |
static AnnotatedType |
getReturnType(Method m,
AnnotatedType declaringType)
Resolves the return type of the given method in the given type.
|
static Type |
getReturnType(Method m,
Type declaringType)
Resolves the return type of the given method in the given type.
|
static String |
getTypeName(Type type)
Returns the display name of a Type.
|
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.
|
static Type |
getTypeParameter(Type type,
TypeVariable<? extends Class<?>> variable) |
static List<Class<?>> |
getUpperBoundClassAndInterfaces(Type type)
Returns list of classes and interfaces that are supertypes of the given type.
|
static int |
hashCode(AnnotatedType... types) |
static boolean |
isFullyBound(Type type) |
static boolean |
isMissingTypeParameters(Type type)
Checks if the given type is a class that is supposed to have type parameters, but doesn't.
|
static boolean |
isSuperType(Type superType,
Type subType)
Checks if the capture of subType is a subtype of superType
|
static Annotation[] |
merge(Annotation[]... annotations)
Merges an arbitrary number of annotations arrays, and removes duplicates.
|
static <T extends AnnotatedType> |
mergeAnnotations(T t1,
T t2) |
static <T extends AnnotatedType> |
replaceAnnotations(T original,
Annotation[] annotations)
Creates a new
AnnotatedType of the same type as the original, but with its annotations
replaced with the provided ones. |
static AnnotatedParameterizedType |
replaceParameters(AnnotatedParameterizedType type,
AnnotatedType[] typeParameters)
Creates a new
AnnotatedParameterizedType of the same raw class as the provided type
by with all of its type parameters replaced by typeParameters . |
static AnnotatedType |
resolveExactType(AnnotatedType unresolved,
AnnotatedType typeAndParams) |
static Type |
resolveExactType(Type unresolved,
Type typeAndParams) |
static AnnotatedType |
resolveType(AnnotatedType unresolved,
AnnotatedType typeAndParams) |
static Type |
resolveType(Type unresolved,
Type typeAndParams) |
static <T extends AnnotatedType> |
toCanonical(T type)
Returns an
AnnotatedType functionally identical to the given one, but in a canonical form that
implements equals and hashCode . |
static <T extends AnnotatedType> |
toCanonicalBoxed(T type)
Returns an
AnnotatedType functionally identical to the given one, but in a canonical form that
implements equals and hashCode and has all the primitives replaced by their boxed form. |
static AnnotatedType |
transform(AnnotatedType type,
TypeVisitor visitor) |
static <T extends AnnotatedType> |
updateAnnotations(T original,
Annotation[] annotations)
Creates a new
AnnotatedType of the same structure as the original, but with its annotations
replaced with the provided ones. |
public static boolean isFullyBound(Type type)
public static AnnotatedType resolveExactType(AnnotatedType unresolved, AnnotatedType typeAndParams)
public static AnnotatedType resolveType(AnnotatedType unresolved, AnnotatedType typeAndParams)
public static boolean isMissingTypeParameters(Type type)
public static Type addWildcardParameters(Class<?> clazz)
addWildcardParameters(Map.class)
returns a type representing Map<?,?>
.public static AnnotatedType getExactSuperType(AnnotatedType subType, Class<?> searchSuperClass)
getExactSuperType(Type, Class)
but works with AnnotatedType
ssubType
- The type whose supertype is to be searched forsearchSuperClass
- The class of the supertype to search forsearchSuperClass
with type parameters from subType
public static Type getExactSuperType(Type subType, Class<?> searchSuperClass)
subType
whose erasure is searchSuperClass
.
In other words, returns a type representing the class searchSuperClass
plus its exact type parameters in subType
.
ParameterizedType
if searchSuperClass
is a real class or interface and subType
has parameters for itGenericArrayType
if searchSuperClass
is an array type, and subType
has type parameters for itClass
if subType
is a raw type, or has no type parameters for searchSuperClass
searchSuperClass
is not a superclass of subType
.For example, with class StringList implements List<String>
, getExactSuperType(StringList.class, Collection.class)
returns a ParameterizedType
representing Collection<String>
.
subType
- The type whose supertype is to be searched forsearchSuperClass
- The class of the supertype to search forsearchSuperClass
with type parameters from subType
public static AnnotatedType getExactSubType(AnnotatedType superType, Class<?> searchSubClass)
getExactSubType(Type, Class)
but works with AnnotatedType
ssuperType
- The type whose subtype is to be searched forsearchSubClass
- The class of the subtype to search forsearchSubClass
with type parameters from superType
public static Type getExactSubType(Type superType, Class<?> searchSubClass)
superType
whose erasure is searchSubClass
.
In other words, returns a type representing the class searchSubClass
plus its exact type parameters in superType
,
if they are possible to resolve.
AnnotatedParameterizedType
if searchSubClass
is a real class or interface and superType
has parameters for itAnnotatedArrayType
if searchSubClass
is an array type, and superType
has type parameters for itAnnotatedType
if superType
is a raw type, or has no type parameters for searchSubClass
searchSubClass
is not a subclass of superType
.For example, with getExactSubType(new TypeToken<List<String>>(){}.getAnnotatedType(), ArrayList.class)
returns a AnnotatedParameterizedType
representing ArrayList<String>
.
public static AnnotatedType getTypeParameter(AnnotatedType type, TypeVariable<? extends Class<?>> variable)
class StringList implements List<String>
,
getTypeParameter(StringList.class, Collection.class.getTypeParameters()[0])
returns String
.type
- The type to inspect.variable
- The type variable to find the value for.public static Type getTypeParameter(Type type, TypeVariable<? extends Class<?>> variable)
public static boolean isSuperType(Type superType, Type subType)
public static AnnotatedType getArrayComponentType(AnnotatedType type)
public static Type getArrayComponentType(Type type)
public static AnnotatedType getExactReturnType(Method m, AnnotatedType declaringType)
m.getAnnotatedReturnType()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in the return type, or declaringType
is a raw type.public static Type getExactReturnType(Method m, Type declaringType)
m.getGenericReturnType()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in the return type, or declaringType
is a raw type.public static AnnotatedType getReturnType(Method m, AnnotatedType declaringType)
getExactReturnType(Method, AnnotatedType)
.
This may be different from m.getAnnotatedReturnType()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in the return type, or declaringType
is a raw type.public static Type getReturnType(Method m, Type declaringType)
getExactReturnType(Method, Type)
.
This may be different from m.getGenericReturnType()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in the return type, or declaringType
is a raw type.public static AnnotatedType getExactFieldType(Field f, AnnotatedType declaringType)
f.getAnnotatedType()
when the field was declared in a superclass,
or declaringType
has a type parameter that is used in the type of the field, or declaringType
is a raw type.public static Type getExactFieldType(Field f, Type type)
f.getGenericType()
when the field was declared in a superclass,
or declaringType
has a type parameter that is used in the type of the field, or declaringType
is a raw type.public static AnnotatedType getFieldType(Field f, AnnotatedType declaringType)
getExactFieldType(Field, AnnotatedType)
).
This may be different from f.getAnnotatedType()
when the field was declared in a superclass,
or declaringType
has a type parameter that is used in the type of the field, or declaringType
is a raw type.public static Type getFieldType(Field f, Type type)
getExactFieldType(Field, Type)
).
This may be different from f.getGenericType()
when the field was declared in a superclass,
or declaringType
has a type parameter that is used in the type of the field, or declaringType
is a raw type.public static AnnotatedType[] getExactParameterTypes(Executable exe, AnnotatedType declaringType)
exe.getAnnotatedParameterTypes()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in one of the parameters, or declaringType
is a raw type.public static Type[] getExactParameterTypes(Executable exe, Type declaringType)
exe.getParameterTypes()
when the method was declared in a superclass,
or declaringType
has a type parameter that is used in one of the parameters, or declaringType
is a raw type.public static AnnotatedType[] getParameterTypes(Executable exe, AnnotatedType declaringType)
public static Type[] getParameterTypes(Executable exe, Type declaringType)
public static AnnotatedType capture(AnnotatedType type)
public static AnnotatedParameterizedType capture(AnnotatedParameterizedType type)
capture(AnnotatedType)
public static List<Class<?>> getUpperBoundClassAndInterfaces(Type type)
class {@literal Foo<A extends Number & Iterable<A>, B extends A>}
B
(Foo.class.getTypeParameters()[1]
)
returns a list containing Number
and Iterable
.
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.
public static AnnotatedType annotate(Type type)
Type
into an AnnotatedType
using the annotations found on
the erasure classes.type
- Type to annotatepublic static AnnotatedType annotate(Type type, Annotation[] annotations)
Type
into an AnnotatedType
using the annotations found on
the erasure classes, plus adding the provided annotations to the top level Type
only.type
- Type to annotateSee annotate(Type)
public static <T extends AnnotatedType> T replaceAnnotations(T original, Annotation[] annotations)
AnnotatedType
of the same type as the original, but with its annotations
replaced with the provided ones.original
- The type whose structure is to be copiedannotations
- Annotations to use instead of the ones found on the original
public static <T extends AnnotatedType> T updateAnnotations(T original, Annotation[] annotations)
AnnotatedType
of the same structure as the original, but with its annotations
replaced with the provided ones.original
- The type whose structure is to be copiedannotations
- Extra annotations to be added on top of the ones found on the original
public static <T extends AnnotatedType> T mergeAnnotations(T t1, T t2)
public static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters)
AnnotatedParameterizedType
of the same raw class as the provided type
by with all of its type parameters replaced by typeParameters
.type
- The original parameterized type from which the raw class is to be takentypeParameters
- The new type parameters to usepublic static <T extends AnnotatedType> T toCanonical(T type)
AnnotatedType
functionally identical to the given one, but in a canonical form that
implements equals
and hashCode
.type
- The type to turn into the canonical formpublic static <T extends AnnotatedType> T toCanonicalBoxed(T type)
AnnotatedType
functionally identical to the given one, but in a canonical form that
implements equals
and hashCode
and has all the primitives replaced by their boxed form.type
- The type to turn into the canonical formpublic static AnnotatedType transform(AnnotatedType type, TypeVisitor visitor)
public static Annotation[] merge(Annotation[]... annotations)
annotations
- Annotation arrays to merge and deduplicatepublic static int hashCode(AnnotatedType... types)
public static boolean equals(AnnotatedType t1, AnnotatedType t2)
t1
- The first type to be comparedt2
- The second type to be comparedCopyright © 2016–2019. All rights reserved.