public class TypeUtils extends Object
Utility methods focusing on type inspection, particularly with regard to generics.
Modifier and Type | Class and Description |
---|---|
static class |
TypeUtils.WildcardTypeBuilder
WildcardType builder. |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsTypeVariables(Type type)
Learn, recursively, whether any of the type parameters associated with
type are bound to variables. |
static boolean |
equals(Type t1,
Type t2)
Check equality of types.
|
static Type |
getArrayComponentType(Type type)
Get the array component type of
type . |
static Type[] |
getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type of
Object if
TypeVariable.getBounds() returns an empty array. |
static Type[] |
getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value of
null if
WildcardType.getLowerBounds() returns an empty array. |
static Type[] |
getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value of
Object if
WildcardType.getUpperBounds() returns an empty array. |
static Class<?> |
getRawType(Type type,
Type assigningType)
Get the raw type of a Java type, given its context.
|
static Map<TypeVariable<?>,Type> |
getTypeArguments(ParameterizedType type)
Retrieves all the type arguments for this parameterized type
including owner hierarchy arguments such as
Outer<K,V>.Inner<T>.DeepInner<E> . |
static Map<TypeVariable<?>,Type> |
getTypeArguments(Type type,
Class<?> toClass)
Gets the type arguments of a class/interface based on a subtype.
|
static boolean |
isArrayType(Type type)
Learn whether the specified type denotes an array type.
|
static boolean |
isAssignable(Type type,
Type toType)
Checks if the subject type may be implicitly cast to the target type
following the Java generics rules.
|
static boolean |
isInstance(Object value,
Type type)
Checks if the given value can be assigned to the target type
following the Java generics rules.
|
static Type[] |
normalizeUpperBounds(Type[] bounds)
This method strips out the redundant upper bound types in type
variable types and wildcard types (or it would with wildcard types if
multiple upper bounds were allowed).
|
static ParameterizedType |
parameterizeWithOwner(Type owner,
Class<?> raw,
Type... typeArguments)
Create a parameterized type instance.
|
static String |
toString(Type type)
Present a given type as a Java-esque String.
|
static Type |
unrollVariables(Map<TypeVariable<?>,Type> typeArguments,
Type type)
Get a type representing
type with variable assignments "unrolled." |
static TypeUtils.WildcardTypeBuilder |
wildcardType()
|
public static boolean isAssignable(Type type, Type toType)
Checks if the subject type may be implicitly cast to the target type
following the Java generics rules. If both types are Class
objects, the method returns the result of
Class.isAssignableFrom(Class)
.
type
- the subject type to be assigned to the target typetoType
- the target typetrue
if type
is assignable to toType
.public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)
Retrieves all the type arguments for this parameterized type
including owner hierarchy arguments such as
Outer<K,V>.Inner<T>.DeepInner<E>
.
The arguments are returned in a
Map
specifying the argument type for each TypeVariable
.
type
- specifies the subject parameterized type from which to
harvest the parameters.Map
of the type arguments to their respective type
variables.public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass)
Gets the type arguments of a class/interface based on a subtype. For
instance, this method will determine that both of the parameters for the
interface Map
are Object
for the subtype
Properties
even though the subtype does not
directly implement the Map
interface.
This method returns null
if type
is not assignable to
toClass
. It returns an empty map if none of the classes or
interfaces in its inheritance hierarchy specify any type arguments.
A side effect of this method is that it also retrieves the type
arguments for the classes and interfaces that are part of the hierarchy
between type
and toClass
. So with the above
example, this method will also determine that the type arguments for
Hashtable
are also both Object
.
In cases where the interface specified by toClass
is
(indirectly) implemented more than once (e.g. where toClass
specifies the interface Iterable
and
type
specifies a parameterized type that implements both
Set
and Collection
),
this method will look at the inheritance hierarchy of only one of the
implementations/subclasses; the first interface encountered that isn't a
subinterface to one of the others in the type
to
toClass
hierarchy.
type
- the type from which to determine the type parameters of
toClass
toClass
- the class whose type parameters are to be determined based
on the subtype type
Map
of the type assignments for the type variables in
each type in the inheritance hierarchy from type
to
toClass
inclusive.public static boolean isInstance(Object value, Type type)
Checks if the given value can be assigned to the target type following the Java generics rules.
value
- the value to be checkedtype
- the target typetrue
if value
is an instance of type
.public static Type[] normalizeUpperBounds(Type[] bounds)
This method strips out the redundant upper bound types in type variable types and wildcard types (or it would with wildcard types if multiple upper bounds were allowed).
Example, with the variable type declaration:
<K extends java.util.Collection<String> & java.util.List<String>>
since List
is a subinterface of Collection
,
this method will return the bounds as if the declaration had been:
<K extends java.util.List<String>>
bounds
- an array of types representing the upper bounds of either
WildcardType
or TypeVariable
, not null
.bounds
minus the
redundant types.public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type of Object
if
TypeVariable.getBounds()
returns an empty array. Otherwise, it
returns the result of TypeVariable.getBounds()
passed into
normalizeUpperBounds(java.lang.reflect.Type[])
.
typeVariable
- the subject type variable, not null
public static Type[] getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value of Object
if
WildcardType.getUpperBounds()
returns an empty array. Otherwise,
it returns the result of WildcardType.getUpperBounds()
passed into normalizeUpperBounds(java.lang.reflect.Type[])
.
wildcardType
- the subject wildcard type, not null
public static Type[] getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value of null
if
WildcardType.getLowerBounds()
returns an empty array. Otherwise,
it returns the result of WildcardType.getLowerBounds()
.
wildcardType
- the subject wildcard type, not null
public static Class<?> getRawType(Type type, Type assigningType)
Get the raw type of a Java type, given its context. Primarily for use
with TypeVariable
s and GenericArrayType
s, or when you do
not know the runtime type of type
: if you know you have a
Class
instance, it is already raw; if you know you have a
ParameterizedType
, its raw type is only a method call away.
type
- to resolveassigningType
- type to be resolved againstClass
object or null
if
the type could not be resolvedpublic static boolean isArrayType(Type type)
type
- the type to be checkedtrue
if type
is an array class or a GenericArrayType
.public static Type getArrayComponentType(Type type)
type
.type
- the type to be checkedpublic static Type unrollVariables(Map<TypeVariable<?>,Type> typeArguments, Type type)
type
with variable assignments "unrolled."typeArguments
- as from getTypeArguments(Type, Class)
type
- the type to unroll variable assignments forpublic static boolean containsTypeVariables(Type type)
type
are bound to variables.type
- the type to check for type variablespublic static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Type... typeArguments)
owner
- the owning typeraw
- the raw class to create a parameterized type instance fortypeArguments
- the types used for parameterizationParameterizedType
public static TypeUtils.WildcardTypeBuilder wildcardType()
TypeUtils.WildcardTypeBuilder
public static boolean equals(Type t1, Type t2)
t1
- the first typet2
- the second typeCopyright © 2010–2020 The Apache Software Foundation. All rights reserved.