Class TypeUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.TypeUtils
-
public class TypeUtils extends java.lang.Object
Utility methods focusing on type inspection, particularly with regard to generics.
- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TypeUtils.GenericArrayTypeImpl
GenericArrayType implementation class.private static class
TypeUtils.ParameterizedTypeImpl
ParameterizedType implementation class.static class
TypeUtils.WildcardTypeBuilder
WildcardType
builder.private static class
TypeUtils.WildcardTypeImpl
WildcardType implementation class.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.reflect.WildcardType
WILDCARD_ALL
A wildcard instance matching?
.
-
Constructor Summary
Constructors Constructor Description TypeUtils()
TypeUtils
instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static <T> java.lang.StringBuilder
appendAllTo(java.lang.StringBuilder buf, java.lang.String sep, T... types)
Appendtypes
tobuf
with separatorsep
.private static void
appendRecursiveTypes(java.lang.StringBuilder buf, int[] recursiveTypeIndexes, java.lang.reflect.Type[] argumentTypes)
private static java.lang.String
classToString(java.lang.Class<?> c)
Format aClass
as aString
.static boolean
containsTypeVariables(java.lang.reflect.Type type)
Learn, recursively, whether any of the type parameters associated withtype
are bound to variables.private static boolean
containsVariableTypeSameParametrizedTypeBound(java.lang.reflect.TypeVariable<?> typeVariable, java.lang.reflect.ParameterizedType p)
static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
determineTypeArguments(java.lang.Class<?> cls, java.lang.reflect.ParameterizedType superType)
Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments.private static boolean
equals(java.lang.reflect.GenericArrayType a, java.lang.reflect.Type t)
Learn whethert
equalsa
.private static boolean
equals(java.lang.reflect.ParameterizedType p, java.lang.reflect.Type t)
Learn whethert
equalsp
.private static boolean
equals(java.lang.reflect.Type[] t1, java.lang.reflect.Type[] t2)
Learn whethert1
equalst2
.static boolean
equals(java.lang.reflect.Type t1, java.lang.reflect.Type t2)
Check equality of types.private static boolean
equals(java.lang.reflect.WildcardType w, java.lang.reflect.Type t)
Learn whethert
equalsw
.private static java.lang.reflect.Type[]
extractTypeArgumentsFrom(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> mappings, java.lang.reflect.TypeVariable<?>[] variables)
Helper method to establish the formal parameters for a parameterized type.private static int[]
findRecursiveTypes(java.lang.reflect.ParameterizedType p)
static java.lang.reflect.GenericArrayType
genericArrayType(java.lang.reflect.Type componentType)
Create a generic array type instance.private static java.lang.String
genericArrayTypeToString(java.lang.reflect.GenericArrayType g)
Format aGenericArrayType
as aString
.static java.lang.reflect.Type
getArrayComponentType(java.lang.reflect.Type type)
Get the array component type oftype
.private static java.lang.reflect.Type
getClosestParentType(java.lang.Class<?> cls, java.lang.Class<?> superClass)
Get the closest parent type to the super class specified bysuperClass
.static java.lang.reflect.Type[]
getImplicitBounds(java.lang.reflect.TypeVariable<?> typeVariable)
Returns an array containing the sole type ofObject
ifTypeVariable.getBounds()
returns an empty array.static java.lang.reflect.Type[]
getImplicitLowerBounds(java.lang.reflect.WildcardType wildcardType)
Returns an array containing a single value ofnull
ifWildcardType.getLowerBounds()
returns an empty array.static java.lang.reflect.Type[]
getImplicitUpperBounds(java.lang.reflect.WildcardType wildcardType)
Returns an array containing the sole value ofObject
ifWildcardType.getUpperBounds()
returns an empty array.private static java.lang.Class<?>
getRawType(java.lang.reflect.ParameterizedType parameterizedType)
Transforms the passed in type to aClass
object.static java.lang.Class<?>
getRawType(java.lang.reflect.Type type, java.lang.reflect.Type assigningType)
Get the raw type of a Java type, given its context.private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
getTypeArguments(java.lang.Class<?> cls, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments of a class in the context oftoClass
.static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
getTypeArguments(java.lang.reflect.ParameterizedType type)
Retrieves all the type arguments for this parameterized type including owner hierarchy arguments such asOuter<K,V>.Inner<T>.DeepInner<E>
.private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
getTypeArguments(java.lang.reflect.ParameterizedType parameterizedType, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments of a parameterized type in the context oftoClass
.static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
getTypeArguments(java.lang.reflect.Type type, java.lang.Class<?> toClass)
Gets the type arguments of a class/interface based on a subtype.private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>
getTypeArguments(java.lang.reflect.Type type, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments oftype
in the context oftoClass
.static boolean
isArrayType(java.lang.reflect.Type type)
Learn whether the specified type denotes an array type.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.Class<?> toClass)
Checks if the subject type may be implicitly cast to the target class following the Java generics rules.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.GenericArrayType toGenericArrayType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target generic array type following the Java generics rules.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.ParameterizedType toParameterizedType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target parameterized type following the Java generics rules.static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType)
Checks if the subject type may be implicitly cast to the target type following the Java generics rules.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target type following the Java generics rules.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.TypeVariable<?> toTypeVariable, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target type variable following the Java generics rules.private static boolean
isAssignable(java.lang.reflect.Type type, java.lang.reflect.WildcardType toWildcardType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target wildcard type following the Java generics rules.static boolean
isInstance(java.lang.Object value, java.lang.reflect.Type type)
Checks if the given value can be assigned to the target type following the Java generics rules.private static <T> void
mapTypeVariablesToArguments(java.lang.Class<T> cls, java.lang.reflect.ParameterizedType parameterizedType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Performs a mapping of type variables.static java.lang.reflect.Type[]
normalizeUpperBounds(java.lang.reflect.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 java.lang.reflect.ParameterizedType
parameterize(java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)
Create a parameterized type instance.static java.lang.reflect.ParameterizedType
parameterize(java.lang.Class<?> raw, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArgMappings)
Create a parameterized type instance.private static java.lang.String
parameterizedTypeToString(java.lang.reflect.ParameterizedType p)
Format aParameterizedType
as aString
.static java.lang.reflect.ParameterizedType
parameterizeWithOwner(java.lang.reflect.Type owner, java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)
Create a parameterized type instance.static java.lang.reflect.ParameterizedType
parameterizeWithOwner(java.lang.reflect.Type owner, java.lang.Class<?> raw, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArgMappings)
Create a parameterized type instance.private static java.lang.reflect.Type
substituteTypeVariables(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Find the mapping fortype
intypeVarAssigns
.static java.lang.String
toLongString(java.lang.reflect.TypeVariable<?> var)
Format aTypeVariable
including itsGenericDeclaration
.static java.lang.String
toString(java.lang.reflect.Type type)
Present a given type as a Java-esque String.private static <T> java.lang.String
toString(T object)
static boolean
typesSatisfyVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Determines whether or not specified types satisfy the bounds of their mapped type variables.private static java.lang.String
typeVariableToString(java.lang.reflect.TypeVariable<?> v)
Format aTypeVariable
as aString
.private static java.lang.reflect.Type[]
unrollBounds(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type[] bounds)
Local helper method to unroll variables in a type bounds array.private static java.lang.reflect.Type
unrollVariableAssignments(java.lang.reflect.TypeVariable<?> var, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Look upvar
intypeVarAssigns
transitively, i.e.static java.lang.reflect.Type
unrollVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type type)
Get a type representingtype
with variable assignments "unrolled."static TypeUtils.WildcardTypeBuilder
wildcardType()
private static java.lang.String
wildcardTypeToString(java.lang.reflect.WildcardType w)
Format aWildcardType
as aString
.static <T> Typed<T>
wrap(java.lang.Class<T> type)
Wrap the specifiedClass
in aTyped
wrapper.static <T> Typed<T>
wrap(java.lang.reflect.Type type)
Wrap the specifiedType
in aTyped
wrapper.
-
-
-
Method Detail
-
isAssignable
public static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.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 ofClassUtils.isAssignable(Class, Class)
.- Parameters:
type
- the subject type to be assigned to the target typetoType
- the target type- Returns:
true
iftype
is assignable totoType
.
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target type following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoType
- the target typetypeVarAssigns
- optional map of type variable assignments- Returns:
true
iftype
is assignable totoType
.
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.Class<?> toClass)
Checks if the subject type may be implicitly cast to the target class following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoClass
- the target class- Returns:
true
iftype
is assignable totoClass
.
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.ParameterizedType toParameterizedType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target parameterized type following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoParameterizedType
- the target parameterized typetypeVarAssigns
- a map with type variables- Returns:
true
iftype
is assignable totoType
.
-
unrollVariableAssignments
private static java.lang.reflect.Type unrollVariableAssignments(java.lang.reflect.TypeVariable<?> var, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Look upvar
intypeVarAssigns
transitively, i.e. keep looking until the value found is not a type variable.- Parameters:
var
- the type variable to look uptypeVarAssigns
- the map used for the look up- Returns:
- Type or
null
if some variable was not in the map - Since:
- 3.2
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.GenericArrayType toGenericArrayType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target generic array type following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoGenericArrayType
- the target generic array typetypeVarAssigns
- a map with type variables- Returns:
true
iftype
is assignable totoGenericArrayType
.
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.WildcardType toWildcardType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target wildcard type following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoWildcardType
- the target wildcard typetypeVarAssigns
- a map with type variables- Returns:
true
iftype
is assignable totoWildcardType
.
-
isAssignable
private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.TypeVariable<?> toTypeVariable, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Checks if the subject type may be implicitly cast to the target type variable following the Java generics rules.
- Parameters:
type
- the subject type to be assigned to the target typetoTypeVariable
- the target type variabletypeVarAssigns
- a map with type variables- Returns:
true
iftype
is assignable totoTypeVariable
.
-
substituteTypeVariables
private static java.lang.reflect.Type substituteTypeVariables(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Find the mapping for
type
intypeVarAssigns
.- Parameters:
type
- the type to be replacedtypeVarAssigns
- the map with type variables- Returns:
- the replaced type
- Throws:
java.lang.IllegalArgumentException
- if the type cannot be substituted
-
getTypeArguments
public static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.reflect.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 java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.reflect.Type type, java.lang.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.
-
getTypeArguments
private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.reflect.Type type, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments of
type
in the context oftoClass
.- Parameters:
type
- the type in questiontoClass
- the classsubtypeVarAssigns
- a map with type variables- Returns:
- the
Map
with type arguments
-
getTypeArguments
private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.reflect.ParameterizedType parameterizedType, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments of a parameterized type in the context of
toClass
.- Parameters:
parameterizedType
- the parameterized typetoClass
- the classsubtypeVarAssigns
- a map with type variables- Returns:
- the
Map
with type arguments
-
getTypeArguments
private static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.Class<?> cls, java.lang.Class<?> toClass, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> subtypeVarAssigns)
Return a map of the type arguments of a class in the context of
toClass
.- Parameters:
cls
- the class in questiontoClass
- the context classsubtypeVarAssigns
- a map with type variables- Returns:
- the
Map
with type arguments
-
determineTypeArguments
public static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> determineTypeArguments(java.lang.Class<?> cls, java.lang.reflect.ParameterizedType superType)
Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments. This method is the inverse of
getTypeArguments(Type, Class)
which gets a class/interface's type arguments based on a subtype. It is far more limited in determining the type arguments for the subject class's type variables in that it can only determine those parameters that map from the subjectClass
object to the supertype.Example:
TreeSet
sets its parameter as the parameter forNavigableSet
, which in turn sets the parameter ofSortedSet
, which in turn sets the parameter ofSet
, which in turn sets the parameter ofCollection
, which in turn sets the parameter ofIterable
. SinceTreeSet
's parameter maps (indirectly) toIterable
's parameter, it will be able to determine that based on the super typeIterable<? extends Map<Integer, ? extends Collection<?>>>
, the parameter ofTreeSet
is? extends Map<Integer, ? extends Collection<?>>
.- Parameters:
cls
- the class whose type parameters are to be determined, notnull
superType
- the super type from whichcls
's type arguments are to be determined, notnull
- Returns:
- a
Map
of the type assignments that could be determined for the type variables in each type in the inheritance hierarchy fromtype
totoClass
inclusive.
-
mapTypeVariablesToArguments
private static <T> void mapTypeVariablesToArguments(java.lang.Class<T> cls, java.lang.reflect.ParameterizedType parameterizedType, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Performs a mapping of type variables.
- Type Parameters:
T
- the generic type of the class in question- Parameters:
cls
- the class in questionparameterizedType
- the parameterized typetypeVarAssigns
- the map to be filled
-
getClosestParentType
private static java.lang.reflect.Type getClosestParentType(java.lang.Class<?> cls, java.lang.Class<?> superClass)
Get the closest parent type to the super class specified by
superClass
.- Parameters:
cls
- the class in questionsuperClass
- the super class- Returns:
- the closes parent type
-
isInstance
public static boolean isInstance(java.lang.Object value, java.lang.reflect.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 java.lang.reflect.Type[] normalizeUpperBounds(java.lang.reflect.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 java.lang.reflect.Type[] getImplicitBounds(java.lang.reflect.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 java.lang.reflect.Type[] getImplicitUpperBounds(java.lang.reflect.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 java.lang.reflect.Type[] getImplicitLowerBounds(java.lang.reflect.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.
-
typesSatisfyVariables
public static boolean typesSatisfyVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeVarAssigns)
Determines whether or not specified types satisfy the bounds of their mapped type variables. When a type parameter extends another (such as
<T, S extends T>
), uses another as a type parameter (such as<T, S extends Comparable>>
), or otherwise depends on another type variable to be specified, the dependencies must be included intypeVarAssigns
.- Parameters:
typeVarAssigns
- specifies the potential types to be assigned to the type variables, notnull
.- Returns:
- whether or not the types can be assigned to their respective type variables.
-
getRawType
private static java.lang.Class<?> getRawType(java.lang.reflect.ParameterizedType parameterizedType)
Transforms the passed in type to a
Class
object. Type-checking method of convenience.- Parameters:
parameterizedType
- the type to be converted- Returns:
- the corresponding
Class
object - Throws:
java.lang.IllegalStateException
- if the conversion fails
-
getRawType
public static java.lang.Class<?> getRawType(java.lang.reflect.Type type, java.lang.reflect.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(java.lang.reflect.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 java.lang.reflect.Type getArrayComponentType(java.lang.reflect.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 java.lang.reflect.Type unrollVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.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
- Since:
- 3.2
-
unrollBounds
private static java.lang.reflect.Type[] unrollBounds(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type[] bounds)
Local helper method to unroll variables in a type bounds array.- Parameters:
typeArguments
- assignmentsMap
bounds
- in which to expand variables- Returns:
bounds
with any variables reassigned- Since:
- 3.2
-
containsTypeVariables
public static boolean containsTypeVariables(java.lang.reflect.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
- Since:
- 3.2
-
parameterize
public static final java.lang.reflect.ParameterizedType parameterize(java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)
Create a parameterized type instance.- Parameters:
raw
- the raw class to create a parameterized type instance fortypeArguments
- the types used for parameterization- Returns:
ParameterizedType
- Since:
- 3.2
-
parameterize
public static final java.lang.reflect.ParameterizedType parameterize(java.lang.Class<?> raw, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArgMappings)
Create a parameterized type instance.- Parameters:
raw
- the raw class to create a parameterized type instance fortypeArgMappings
- the mapping used for parameterization- Returns:
ParameterizedType
- Since:
- 3.2
-
parameterizeWithOwner
public static final java.lang.reflect.ParameterizedType parameterizeWithOwner(java.lang.reflect.Type owner, java.lang.Class<?> raw, java.lang.reflect.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
- Since:
- 3.2
-
parameterizeWithOwner
public static final java.lang.reflect.ParameterizedType parameterizeWithOwner(java.lang.reflect.Type owner, java.lang.Class<?> raw, java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArgMappings)
Create a parameterized type instance.- Parameters:
owner
- the owning typeraw
- the raw class to create a parameterized type instance fortypeArgMappings
- the mapping used for parameterization- Returns:
ParameterizedType
- Since:
- 3.2
-
extractTypeArgumentsFrom
private static java.lang.reflect.Type[] extractTypeArgumentsFrom(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> mappings, java.lang.reflect.TypeVariable<?>[] variables)
Helper method to establish the formal parameters for a parameterized type.- Parameters:
mappings
- map containing the assignmentsvariables
- expected map keys- Returns:
- array of map values corresponding to specified keys
-
wildcardType
public static TypeUtils.WildcardTypeBuilder wildcardType()
- Returns:
TypeUtils.WildcardTypeBuilder
- Since:
- 3.2
-
genericArrayType
public static java.lang.reflect.GenericArrayType genericArrayType(java.lang.reflect.Type componentType)
Create a generic array type instance.- Parameters:
componentType
- the type of the elements of the array. For example the component type ofboolean[]
isboolean
- Returns:
GenericArrayType
- Since:
- 3.2
-
equals
public static boolean equals(java.lang.reflect.Type t1, java.lang.reflect.Type t2)
Check equality of types.- Parameters:
t1
- the first typet2
- the second type- Returns:
- boolean
- Since:
- 3.2
-
equals
private static boolean equals(java.lang.reflect.ParameterizedType p, java.lang.reflect.Type t)
Learn whethert
equalsp
.- Parameters:
p
- LHSt
- RHS- Returns:
- boolean
- Since:
- 3.2
-
equals
private static boolean equals(java.lang.reflect.GenericArrayType a, java.lang.reflect.Type t)
Learn whethert
equalsa
.- Parameters:
a
- LHSt
- RHS- Returns:
- boolean
- Since:
- 3.2
-
equals
private static boolean equals(java.lang.reflect.WildcardType w, java.lang.reflect.Type t)
Learn whethert
equalsw
.- Parameters:
w
- LHSt
- RHS- Returns:
- boolean
- Since:
- 3.2
-
equals
private static boolean equals(java.lang.reflect.Type[] t1, java.lang.reflect.Type[] t2)
Learn whethert1
equalst2
.- Parameters:
t1
- LHSt2
- RHS- Returns:
- boolean
- Since:
- 3.2
-
toString
public static java.lang.String toString(java.lang.reflect.Type type)
Present a given type as a Java-esque String.- Parameters:
type
- the type to create a String representation for, notnull
- Returns:
- String
- Since:
- 3.2
-
toLongString
public static java.lang.String toLongString(java.lang.reflect.TypeVariable<?> var)
Format aTypeVariable
including itsGenericDeclaration
.- Parameters:
var
- the type variable to create a String representation for, notnull
- Returns:
- String
- Since:
- 3.2
-
wrap
public static <T> Typed<T> wrap(java.lang.reflect.Type type)
Wrap the specifiedType
in aTyped
wrapper.- Type Parameters:
T
- inferred generic type- Parameters:
type
- to wrap- Returns:
- Typed<T>
- Since:
- 3.2
-
wrap
public static <T> Typed<T> wrap(java.lang.Class<T> type)
Wrap the specifiedClass
in aTyped
wrapper.- Type Parameters:
T
- generic type- Parameters:
type
- to wrap- Returns:
- Typed<T>
- Since:
- 3.2
-
classToString
private static java.lang.String classToString(java.lang.Class<?> c)
Format aClass
as aString
.- Parameters:
c
-Class
to format- Returns:
- String
- Since:
- 3.2
-
typeVariableToString
private static java.lang.String typeVariableToString(java.lang.reflect.TypeVariable<?> v)
Format aTypeVariable
as aString
.- Parameters:
v
-TypeVariable
to format- Returns:
- String
- Since:
- 3.2
-
parameterizedTypeToString
private static java.lang.String parameterizedTypeToString(java.lang.reflect.ParameterizedType p)
Format aParameterizedType
as aString
.- Parameters:
p
-ParameterizedType
to format- Returns:
- String
- Since:
- 3.2
-
appendRecursiveTypes
private static void appendRecursiveTypes(java.lang.StringBuilder buf, int[] recursiveTypeIndexes, java.lang.reflect.Type[] argumentTypes)
-
findRecursiveTypes
private static int[] findRecursiveTypes(java.lang.reflect.ParameterizedType p)
-
containsVariableTypeSameParametrizedTypeBound
private static boolean containsVariableTypeSameParametrizedTypeBound(java.lang.reflect.TypeVariable<?> typeVariable, java.lang.reflect.ParameterizedType p)
-
wildcardTypeToString
private static java.lang.String wildcardTypeToString(java.lang.reflect.WildcardType w)
Format aWildcardType
as aString
.- Parameters:
w
-WildcardType
to format- Returns:
- String
- Since:
- 3.2
-
genericArrayTypeToString
private static java.lang.String genericArrayTypeToString(java.lang.reflect.GenericArrayType g)
Format aGenericArrayType
as aString
.- Parameters:
g
-GenericArrayType
to format- Returns:
- String
- Since:
- 3.2
-
appendAllTo
private static <T> java.lang.StringBuilder appendAllTo(java.lang.StringBuilder buf, java.lang.String sep, T... types)
Appendtypes
tobuf
with separatorsep
.- Parameters:
buf
- destinationsep
- separatortypes
- to append- Returns:
buf
- Since:
- 3.2
-
toString
private static <T> java.lang.String toString(T object)
-
-