Class TypeUtils
- java.lang.Object
-
- org.apache.bval.util.reflection.TypeUtils
-
public class TypeUtils extends Object
Taken from commons-lang3.Utility methods focusing on type inspection, particularly with regard to generics.
- Since:
- 1.1.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TypeUtils.WildcardTypeBuilder
WildcardType
builder.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
containsTypeVariables(Type type)
Learn, recursively, whether any of the type parameters associated withtype
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 oftype
.static Type[]
getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type ofObject
ifTypeVariable.getBounds()
returns an empty array.static Type[]
getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value ofnull
ifWildcardType.getLowerBounds()
returns an empty array.static Type[]
getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value ofObject
ifWildcardType.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 asOuter<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 representingtype
with variable assignments "unrolled."static TypeUtils.WildcardTypeBuilder
wildcardType()
-
-
-
Method Detail
-
isAssignable
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 ofClass.isAssignableFrom(Class)
.- Parameters:
type
- the subject type to be assigned to the target typetoType
- the target type- Returns:
true
iftype
is assignable totoType
.
-
getTypeArguments
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 aMap
specifying the argument type for eachTypeVariable
.- Parameters:
type
- specifies the subject parameterized type from which to harvest the parameters.- Returns:
- a
Map
of the type arguments to their respective type variables.
-
getTypeArguments
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
areObject
for the subtypeProperties
even though the subtype does not directly implement theMap
interface.This method returns
null
iftype
is not assignable totoClass
. 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
andtoClass
. So with the above example, this method will also determine that the type arguments forHashtable
are also bothObject
. In cases where the interface specified bytoClass
is (indirectly) implemented more than once (e.g. wheretoClass
specifies the interfaceIterable
andtype
specifies a parameterized type that implements bothSet
andCollection
), 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 thetype
totoClass
hierarchy.- Parameters:
type
- the type from which to determine the type parameters oftoClass
toClass
- the class whose type parameters are to be determined based on the subtypetype
- Returns:
- a
Map
of the type assignments for the type variables in each type in the inheritance hierarchy fromtype
totoClass
inclusive.
-
isInstance
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.
- Parameters:
value
- the value to be checkedtype
- the target type- Returns:
true
ifvalue
is an instance oftype
.
-
normalizeUpperBounds
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 ofCollection
, this method will return the bounds as if the declaration had been:<K extends java.util.List<String>>
- Parameters:
bounds
- an array of types representing the upper bounds of eitherWildcardType
orTypeVariable
, notnull
.- Returns:
- an array containing the values from
bounds
minus the redundant types.
-
getImplicitBounds
public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type of
Object
ifTypeVariable.getBounds()
returns an empty array. Otherwise, it returns the result ofTypeVariable.getBounds()
passed intonormalizeUpperBounds(java.lang.reflect.Type[])
.- Parameters:
typeVariable
- the subject type variable, notnull
- Returns:
- a non-empty array containing the bounds of the type variable.
-
getImplicitUpperBounds
public static Type[] getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value of
Object
ifWildcardType.getUpperBounds()
returns an empty array. Otherwise, it returns the result ofWildcardType.getUpperBounds()
passed intonormalizeUpperBounds(java.lang.reflect.Type[])
.- Parameters:
wildcardType
- the subject wildcard type, notnull
- Returns:
- a non-empty array containing the upper bounds of the wildcard type.
-
getImplicitLowerBounds
public static Type[] getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value of
null
ifWildcardType.getLowerBounds()
returns an empty array. Otherwise, it returns the result ofWildcardType.getLowerBounds()
.- Parameters:
wildcardType
- the subject wildcard type, notnull
- Returns:
- a non-empty array containing the lower bounds of the wildcard type.
-
getRawType
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 andGenericArrayType
s, or when you do not know the runtime type oftype
: if you know you have aClass
instance, it is already raw; if you know you have aParameterizedType
, its raw type is only a method call away.- Parameters:
type
- to resolveassigningType
- type to be resolved against- Returns:
- the resolved
Class
object ornull
if the type could not be resolved
-
isArrayType
public static boolean isArrayType(Type type)
Learn whether the specified type denotes an array type.- Parameters:
type
- the type to be checked- Returns:
true
iftype
is an array class or aGenericArrayType
.
-
getArrayComponentType
public static Type getArrayComponentType(Type type)
Get the array component type oftype
.- Parameters:
type
- the type to be checked- Returns:
- component type or null if type is not an array type
-
unrollVariables
public static Type unrollVariables(Map<TypeVariable<?>,Type> typeArguments, Type type)
Get a type representingtype
with variable assignments "unrolled."- Parameters:
typeArguments
- as fromgetTypeArguments(Type, Class)
type
- the type to unroll variable assignments for- Returns:
- Type
-
containsTypeVariables
public static boolean containsTypeVariables(Type type)
Learn, recursively, whether any of the type parameters associated withtype
are bound to variables.- Parameters:
type
- the type to check for type variables- Returns:
- boolean
-
parameterizeWithOwner
public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Type... typeArguments)
Create a parameterized type instance.- Parameters:
owner
- the owning typeraw
- the raw class to create a parameterized type instance fortypeArguments
- the types used for parameterization- Returns:
ParameterizedType
-
wildcardType
public static TypeUtils.WildcardTypeBuilder wildcardType()
- Returns:
TypeUtils.WildcardTypeBuilder
-
equals
public static boolean equals(Type t1, Type t2)
Check equality of types.- Parameters:
t1
- the first typet2
- the second type- Returns:
- boolean
-
-