Type
. These types can be used with the GenericTypeReflector
or anything else handling Java types.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic AnnotatedType
annotatedClass
(Class<?> clazz, Annotation[] annotations) static AnnotatedType
annotatedInnerClass
(Type owner, Class<?> clazz, Annotation[] annotations) static <A extends Annotation>
Aannotation
(Class<A> annotationType, Map<String, Object> values) Creates an instance of an annotation.static AnnotatedArrayType
arrayOf
(AnnotatedType componentType, Annotation[] annotations) Creates anAnnotatedArrayType
wrapped around an array types created byarrayOf(Type)
static Type
Creates a array type.private static void
Check if the type arguments of the given type are within the bounds declared on the type parameters.private static boolean
couldHaveCommonSubtype
(Type type1, Type type2) Checks if the intersection of two types is not empty.static Type
innerClass
(Type owner, Class<?> clazz) Creates a type ofclazz
nested inowner
.static AnnotatedType
parameterizedAnnotatedClass
(Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) static AnnotatedType
parameterizedAnnotatedInnerClass
(AnnotatedType owner, Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) static AnnotatedType
parameterizedAnnotatedInnerClass
(Type owner, Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) static AnnotatedParameterizedType
parameterizedAnnotatedType
(ParameterizedType type, Annotation[] typeAnnotations, Annotation[]... argumentAnnotations) static Type
parameterizedClass
(Class<?> clazz, Type... arguments) Creates a type of classclazz
witharguments
as type arguments.static Type
parameterizedInnerClass
(Type owner, Class<?> clazz, Type... arguments) Creates a type ofclazz
witharguments
as type arguments, nested inowner
.private static Type
transformOwner
(Type givenOwner, Class<?> clazz) Transforms the given owner type into an appropriate one when constructing a parameterized type.static WildcardType
Returns the wildcard type without bounds.static WildcardType
wildcardExtends
(Type upperBound) Creates a wildcard type with an upper bound.static WildcardType
wildcardSuper
(Type lowerBound) Creates a wildcard type with a lower bound.
-
Field Details
-
UNBOUND_WILDCARD
-
-
Constructor Details
-
TypeFactory
public TypeFactory()
-
-
Method Details
-
parameterizedClass
Creates a type of classclazz
witharguments
as type arguments.For example:
parameterizedClass(Map.class, Integer.class, String.class)
returns the typeMap<Integer, String>
.- Parameters:
clazz
- Type class of the type to createarguments
- Type arguments for the variables ofclazz
, or null if these are not known.- Returns:
- A
ParameterizedType
, or simplyclazz
ifarguments
isnull
or empty.
-
annotatedClass
-
parameterizedAnnotatedClass
public static AnnotatedType parameterizedAnnotatedClass(Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) -
annotatedInnerClass
public static AnnotatedType annotatedInnerClass(Type owner, Class<?> clazz, Annotation[] annotations) -
parameterizedAnnotatedInnerClass
public static AnnotatedType parameterizedAnnotatedInnerClass(Type owner, Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) -
parameterizedAnnotatedInnerClass
public static AnnotatedType parameterizedAnnotatedInnerClass(AnnotatedType owner, Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments) -
parameterizedAnnotatedType
public static AnnotatedParameterizedType parameterizedAnnotatedType(ParameterizedType type, Annotation[] typeAnnotations, Annotation[]... argumentAnnotations) -
innerClass
Creates a type ofclazz
nested inowner
.- Parameters:
owner
- The owner type. This should be a subtype ofclazz.getDeclaringClass()
, ornull
if no owner is known.clazz
- Type class of the type to create- Returns:
- A
ParameterizedType
if the class declaringclazz
is generic and its type parameters are known inowner
andclazz
itself has no type parameters. Otherwise, just returnsclazz
.
-
parameterizedInnerClass
Creates a type ofclazz
witharguments
as type arguments, nested inowner
.In the ideal case, this returns a
ParameterizedType
with all generic information in it. If some type arguments are missing or if the resulting type simply doesn't need any type parameters, it returns the rawclazz
. Note that types with some parameters specified and others not, don't exist in Java.If the caller does not know the exact
owner
type orarguments
,null
should be given (orparameterizedClass(Class, Type...)
orinnerClass(Type, Class)
could be used). If they are not needed (non-generic owner and/orclazz
has no type parameters), they will be filled in automatically. If they are needed but are not given, the rawclazz
is returned.The specified
owner
may be any subtype ofclazz.getDeclaringClass()
. It is automatically converted into the right parameterized version of the declaring class. Ifclazz
is astatic
(nested) class, the owner is not used.- Parameters:
owner
- The owner type. This should be a subtype ofclazz.getDeclaringClass()
, ornull
if no owner is known.clazz
- Type class of the type to createarguments
- Type arguments for the variables ofclazz
, or null if these are not known.- Returns:
- A
ParameterizedType
ifclazz
or the class declaringclazz
is generic, and all the needed type arguments are specified inowner
andarguments
. Otherwise, just returnsclazz
. - Throws:
IllegalArgumentException
- ifarguments
(is non-null and) has an incorrect length, or if one of thearguments
is not within the bounds declared on the matching type variable, or if owner is non-null butclazz
has no declaring class (e.g. is a top-level class), or if owner is not a a subtype ofclazz.getDeclaringClass()
.NullPointerException
- ifclazz
or one of the elements inarguments
is null.
-
checkParametersWithinBound
Check if the type arguments of the given type are within the bounds declared on the type parameters. Only the type arguments of the type itself are checked, the possible owner type is assumed to be valid.It does not follow the checks defined in the JLS because there are several problems with those (see http://stackoverflow.com/questions/7003009 for one). Instead, this applies some intuition and follows what Java compilers seem to do.
- Parameters:
type
- possibly inconsistent type to check.- Throws:
IllegalArgumentException
- if the type arguments are not within the bounds
-
couldHaveCommonSubtype
Checks if the intersection of two types is not empty. -
transformOwner
Transforms the given owner type into an appropriate one when constructing a parameterized type. -
unboundWildcard
Returns the wildcard type without bounds. This is the '?
' in for exampleList<?>
.- Returns:
- The unbound wildcard type
-
wildcardExtends
Creates a wildcard type with an upper bound.For example
wildcardExtends(String.class)
returns the type? extends String
.- Parameters:
upperBound
- Upper bound of the wildcard- Returns:
- A wildcard type
-
wildcardSuper
Creates a wildcard type with a lower bound.For example
wildcardSuper(String.class)
returns the type? super String
.- Parameters:
lowerBound
- Lower bound of the wildcard- Returns:
- A wildcard type
-
arrayOf
Creates a array type.If
componentType
is not a generic type but aClass
object, this returns theClass
representing the non-generic array type. Otherwise, returns aGenericArrayType
.For example:
arrayOf(String.class)
returnsString[].class
arrayOf(parameterizedClass(List.class, String.class))
returns theGenericArrayType
forList<String>[]
- Parameters:
componentType
- The type of the components of the array.- Returns:
- An array type.
-
arrayOf
Creates anAnnotatedArrayType
wrapped around an array types created byarrayOf(Type)
- Parameters:
componentType
- The type of the components of the array.annotations
- The annotations to be added to the array type itself.- Returns:
- An array type.
-
annotation
public static <A extends Annotation> A annotation(Class<A> annotationType, Map<String, Object> values) throws AnnotationFormatExceptionCreates an instance of an annotation.- Type Parameters:
A
- The type of the annotation.- Parameters:
annotationType
- TheClass
representing the type of the annotation to be created.values
- A map of values to be assigned to the annotation elements.- Returns:
- An
Annotation
instanceof matchingannotationType
- Throws:
AnnotationFormatException
- Thrown if incomplete or invalidvalues
are provided
-