public class TypeFactory extends Object
Type
. These types can be used with the GenericTypeReflector
or anything else handling Java types.Constructor and Description |
---|
TypeFactory() |
Modifier and Type | Method and Description |
---|---|
static AnnotatedType |
annotatedClass(Class<?> clazz,
Annotation[] annotations) |
static AnnotatedType |
annotatedInnerClass(Type owner,
Class<?> clazz,
Annotation[] annotations) |
static <A extends Annotation> |
annotation(Class<A> annotationType,
Map<String,Object> values)
Creates an instance of an annotation.
|
static AnnotatedArrayType |
arrayOf(AnnotatedType componentType,
Annotation[] annotations)
Creates an
AnnotatedArrayType wrapped around an array types created by arrayOf(Type) |
static Type |
arrayOf(Type componentType)
Creates a array type.
|
static Type |
innerClass(Type owner,
Class<?> clazz)
Creates a type of
clazz nested in owner . |
static AnnotatedType |
parameterizedAnnotatedClass(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 class
clazz with arguments as type arguments. |
static Type |
parameterizedInnerClass(Type owner,
Class<?> clazz,
Type... arguments)
Creates a type of
clazz with arguments as type arguments, nested in
owner . |
static WildcardType |
unboundWildcard()
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.
|
public static Type parameterizedClass(Class<?> clazz, Type... arguments)
clazz
with arguments
as type arguments.
For example: parameterizedClass(Map.class, Integer.class, String.class)
returns the type Map<Integer, String>
.
clazz
- Type class of the type to createarguments
- Type arguments for the variables of clazz
, or null if these are not
known.ParameterizedType
, or simply clazz
if arguments
is
null
or empty.public static AnnotatedType annotatedClass(Class<?> clazz, Annotation[] annotations)
public static AnnotatedType parameterizedAnnotatedClass(Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments)
public static AnnotatedType annotatedInnerClass(Type owner, Class<?> clazz, Annotation[] annotations)
public static AnnotatedType parameterizedAnnotatedInnerClass(Type owner, Class<?> clazz, Annotation[] annotations, AnnotatedType... arguments)
public static AnnotatedParameterizedType parameterizedAnnotatedType(ParameterizedType type, Annotation[] typeAnnotations, Annotation[]... argumentAnnotations)
public static Type innerClass(Type owner, Class<?> clazz)
clazz
nested in owner
.owner
- The owner type. This should be a subtype of clazz.getDeclaringClass()
,
or null
if no owner is known.clazz
- Type class of the type to createParameterizedType
if the class declaring clazz
is generic and its
type parameters are known in owner
and clazz
itself has no type parameters.
Otherwise, just returns clazz
.public static Type parameterizedInnerClass(Type owner, Class<?> clazz, Type... arguments)
clazz
with arguments
as type arguments, nested in
owner
. 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 raw clazz
. 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 or arguments
, null
should be given (or
parameterizedClass(Class, Type...)
or innerClass(Type, Class)
could be
used). If they are not needed (non-generic owner and/or clazz
has no type
parameters), they will be filled in automatically. If they are needed but are not given, the
raw clazz
is returned.
The specified owner
may be any subtype of
clazz.getDeclaringClass()
. It is automatically converted into the right
parameterized version of the declaring class. If clazz
is a static
(nested)
class, the owner is not used.
owner
- The owner type. This should be a subtype of clazz.getDeclaringClass()
,
or null
if no owner is known.clazz
- Type class of the type to createarguments
- Type arguments for the variables of clazz
, or null if these are not
known.ParameterizedType
if clazz
or the class declaring clazz
is generic, and all the needed type arguments are specified in owner
and
arguments
. Otherwise, just returns clazz
.IllegalArgumentException
- if arguments
(is non-null and) has an incorrect
length, or if one of the arguments
is not within
the bounds declared on the matching type variable, or if
owner is non-null but clazz
has no declaring class
(e.g. is a top-level class), or if owner is not a a subtype
of clazz.getDeclaringClass()
.NullPointerException
- if clazz
or one of the elements in
arguments
is null.public static WildcardType unboundWildcard()
?
' in for example List<?>
.public static WildcardType wildcardExtends(Type upperBound)
For example wildcardExtends(String.class)
returns the type ? extends String
.
upperBound
- Upper bound of the wildcardpublic static WildcardType wildcardSuper(Type lowerBound)
For example wildcardSuper(String.class)
returns the type ? super String
.
lowerBound
- Lower bound of the wildcardpublic static Type arrayOf(Type componentType)
If componentType
is not a generic type but a Class
object, this returns the Class
representing the non-generic array type. Otherwise,
returns a GenericArrayType
.
For example:
arrayOf(String.class)
returns String[].class
arrayOf(parameterizedClass(List.class,
String.class))
returns the GenericArrayType
for List<String>[]
componentType
- The type of the components of the array.public static AnnotatedArrayType arrayOf(AnnotatedType componentType, Annotation[] annotations)
AnnotatedArrayType
wrapped around an array types created by arrayOf(Type)
componentType
- The type of the components of the array.annotations
- The annotations to be added to the array type itself.public static <A extends Annotation> A annotation(Class<A> annotationType, Map<String,Object> values) throws AnnotationFormatException
A
- The type of the annotation.annotationType
- The Class
representing the type of the annotation to be created.values
- A map of values to be assigned to the annotation elements.Annotation
instanceof matching annotationType
AnnotationFormatException
- Thrown if incomplete or invalid values
are providedCopyright © 2016–2019. All rights reserved.