Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.reflect

public Class TypeUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Array, .GenericArrayType, .GenericDeclaration, .ParameterizedType, .Type, .TypeVariable, .WildcardType, java.util.Arrays, .Collection, .Collections, .HashMap, .HashSet, .List, .Map, .Objects, .Set, .TreeSet, org.apache.commons.lang3.AppendableJoiner, .ArrayUtils, .ClassUtils, .ObjectUtils, .Validate, org.apache.commons.lang3.builder.Builder

Utility methods focusing on type inspection, particularly with regard to generics.
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TypeUtils.GenericArrayTypeImpl

GenericArrayType implementation class.

private static class
TypeUtils.ParameterizedTypeImpl

ParameterizedType implementation class.

public static class
private static class
TypeUtils.WildcardTypeImpl

WildcardType implementation class.

Field Summary

Modifier and TypeField and Description
private static final AppendableJoiner<Type>
AMP_JOINER

Ampersand sign joiner.

private static final AppendableJoiner<TypeVariable<Class<?>>>
CTJ_JOINER

Method classToString joiner.

private static final AppendableJoiner<Object>
GT_JOINER

Greater than and lesser than sign joiner.

public static final WildcardType
WILDCARD_ALL

A wildcard instance matching ?

Constructor Summary

AccessConstructor and Description
public
TypeUtils()
Deprecated TODO Make private in 4.0.

TypeUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
private static <T> String
anyToString(final T object)

private static void
appendRecursiveTypes(final StringBuilder builder, final int[] recursiveTypeIndexes, final Type[] argumentTypes)

private static <T> String

Returns:

String.
classToString
(final Class<T>
Class to format.
cls
)

Formats a Class as a String.

public static boolean

Returns:

boolean.
containsTypeVariables
(final Type
the type to check for type variables.
type
)

Tests, recursively, whether any of the type parameters associated with type are bound to variables.

private static boolean
containsVariableTypeSameParametrizedTypeBound(final TypeVariable<?> typeVariable, final ParameterizedType parameterizedType)

public static Map<TypeVariable<?>, Type>

Returns:

a Map of the type assignments that could be determined for the type variables in each type in the inheritance hierarchy from type to toClass inclusive.
determineTypeArguments
(final Class<?>
the class whose type parameters are to be determined, not null.
cls
,
final ParameterizedType
the super type from which cls's type arguments are to be determined, not null.
superParameterizedType
)

Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments.

private static boolean

Returns:

boolean.
equals
(final GenericArrayType
LHS.
genericArrayType
,
final Type
RHS.
type
)

Tests whether t equals a.

private static boolean

Returns:

boolean.
equals
(final ParameterizedType
LHS.
parameterizedType
,
final Type
RHS.
type
)

Tests whether t equals p.

public static boolean

Returns:

boolean.
equals
(final Type
the first type.
type1
,
final Type
the second type.
type2
)

Tests equality of types.

private static boolean

Returns:

boolean.
equals
(final Type[]
LHS.
type1
,
final Type[]
RHS.
type2
)

Tests whether t1 equals t2.

private static boolean

Returns:

boolean.
equals
(final WildcardType
LHS.
wildcardType
,
final Type
RHS.
type
)

Tests whether t equals w.

private static Type[]

Returns:

array of map values corresponding to specified keys.
extractTypeArgumentsFrom
(final Map<TypeVariable<?>, Type>
map containing the assignments.
mappings
,
final TypeVariable<?>[]
expected map keys.
variables
)

Helper method to establish the formal parameters for a parameterized type.

private static int[]
findRecursiveTypes(final ParameterizedType parameterizedType)

public static GenericArrayType
genericArrayType(final Type
the type of the elements of the array. For example the component type of boolean[] is boolean.
componentType
)

Creates a generic array type instance.

private static String

Returns:

String.
genericArrayTypeToString
(final GenericArrayType
GenericArrayType to format.
genericArrayType
)

Formats a GenericArrayType as a String.

public static Type

Returns:

component type or null if type is not an array type.
getArrayComponentType
(final Type
the type to be checked.
type
)

Gets the array component type of type.

private static Type

Returns:

the closes parent type.
getClosestParentType
(final Class<?>
the class in question.
cls
,
final Class<?>
the super class.
superClass
)

Gets the closest parent type to the super class specified by superClass.

public static Type[]

Returns:

a non-empty array containing the bounds of the type variable, which could be Object.
getImplicitBounds
(final TypeVariable<?>
the subject type variable, not null.
typeVariable
)

Gets an array containing the sole type of Object if TypeVariable#getBounds() returns an empty array.

public static Type[]

Returns:

a non-empty array containing the lower bounds of the wildcard type, which could be null.
getImplicitLowerBounds
(final WildcardType
the subject wildcard type, not null.
wildcardType
)

Gets an array containing a single value of null if WildcardType#getLowerBounds() returns an empty array.

public static Type[]

Returns:

a non-empty array containing the upper bounds of the wildcard type.
getImplicitUpperBounds
(final WildcardType
the subject wildcard type, not null.
wildcardType
)

Gets an array containing the sole value of Object if WildcardType#getUpperBounds() returns an empty array.

private static Class<?>

Returns:

the corresponding Class object.
getRawType
(final ParameterizedType
the type to be converted.
parameterizedType
)

Transforms the passed in type to a Class object.

public static Class<?>

Returns:

the resolved Class object or null if the type could not be resolved.
getRawType
(final Type
to resolve.
type
,
final Type
type to be resolved against.
assigningType
)

Gets the raw type of a Java type, given its context.

private static Map<TypeVariable<?>, Type>

Returns:

the Map with type arguments.
getTypeArguments
(Class<?>
the class in question.
cls
,
final Class<?>
the context class.
toClass
,
final Map<TypeVariable<?>, Type>
a map with type variables.
subtypeVarAssigns
)

Gets a map of the type arguments of a class in the context of toClass.

public static Map<TypeVariable<?>, Type>

Returns:

a Map of the type arguments to their respective type variables.
getTypeArguments
(final ParameterizedType
specifies the subject parameterized type from which to harvest the parameters.
type
)

Gets all the type arguments for this parameterized type including owner hierarchy arguments such as Outer<K, V>.Inner<T>.DeepInner<E> .

private static Map<TypeVariable<?>, Type>

Returns:

the Map with type arguments.
getTypeArguments
(final ParameterizedType
the parameterized type.
parameterizedType
,
final Class<?>
the class.
toClass
,
final Map<TypeVariable<?>, Type>
a map with type variables.
subtypeVarAssigns
)

Gets a map of the type arguments of a parameterized type in the context of toClass.

public static Map<TypeVariable<?>, Type>

Returns:

a Map of the type assignments for the type variables in each type in the inheritance hierarchy from type to toClass inclusive.
getTypeArguments
(final Type
the type from which to determine the type parameters of toClass
type
,
final Class<?>
the class whose type parameters are to be determined based on the subtype type
toClass
)

Gets the type arguments of a class/interface based on a subtype.

private static Map<TypeVariable<?>, Type>

Returns:

the Map with type arguments.
getTypeArguments
(final Type
the type in question.
type
,
final Class<?>
the class.
toClass
,
final Map<TypeVariable<?>, Type>
a map with type variables.
subtypeVarAssigns
)

Gets a map of the type arguments of type in the context of toClass.

public static boolean

Returns:

true if type is an array class or a GenericArrayType.
isArrayType
(final Type
the type to be checked.
type
)

Tests whether the specified type denotes an array type.

private static boolean

Returns:

true if type is assignable to toClass.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final Class<?>
the target class.
toClass
)

Tests if the subject type may be implicitly cast to the target class following the Java generics rules.

private static boolean

Returns:

true if type is assignable to toGenericArrayType.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final GenericArrayType
the target generic array type.
toGenericArrayType
,
final Map<TypeVariable<?>, Type>
a map with type variables.
typeVarAssigns
)

Tests if the subject type may be implicitly cast to the target generic array type following the Java generics rules.

private static boolean

Returns:

true if type is assignable to toType.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final ParameterizedType
the target parameterized type.
toParameterizedType
,
final Map<TypeVariable<?>, Type>
a map with type variables.
typeVarAssigns
)

Tests if the subject type may be implicitly cast to the target parameterized type following the Java generics rules.

public static boolean

Returns:

true if type is assignable to toType.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final Type
the target type.
toType
)

Tests if the subject type may be implicitly cast to the target type following the Java generics rules.

private static boolean

Returns:

true if type is assignable to toType.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final Type
the target type.
toType
,
final Map<TypeVariable<?>, Type>
optional map of type variable assignments.
typeVarAssigns
)

Tests if the subject type may be implicitly cast to the target type following the Java generics rules.

private static boolean

Returns:

true if type is assignable to toTypeVariable.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final TypeVariable<?>
the target type variable.
toTypeVariable
,
final Map<TypeVariable<?>, Type>
a map with type variables.
typeVarAssigns
)

Tests if the subject type may be implicitly cast to the target type variable following the Java generics rules.

private static boolean

Returns:

true if type is assignable to toWildcardType.
isAssignable
(final Type
the subject type to be assigned to the target type.
type
,
final WildcardType
the target wildcard type.
toWildcardType
,
final Map<TypeVariable<?>, Type>
a map with type variables.
typeVarAssigns
)

Tests if the subject type may be implicitly cast to the target wildcard type following the Java generics rules.

private static boolean

Returns:

whether the class contains a cyclical reference.
isCyclical
(final Class<?>
the class to test.
cls
)

Tests whether the class contains a cyclical reference in the qualified name of a class.

public static boolean

Returns:

true if value is an instance of type.
isInstance
(final Object
the value to be checked.
value
,
final Type
the target type.
type
)

Tests if the given value can be assigned to the target type following the Java generics rules.

private static <
the generic type of the class in question.
T
>
void
mapTypeVariablesToArguments(final Class<T>
the class in question.
cls
,
final ParameterizedType
the parameterized type.
parameterizedType
,
final Map<TypeVariable<?>, Type>
the map to be filled.
typeVarAssigns
)

Maps type variables.

public static Type[]

Returns:

an array containing the values from bounds minus the redundant types.
normalizeUpperBounds
(final Type[]
an array of types representing the upper bounds of either WildcardType or TypeVariable, not null.
bounds
)

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).

private static Type[]

Returns:

result from normalizeUpperBounds(Type[]) unless bounds is empty in which case return an array with the element Object.class.
normalizeUpperToObject
(final Type[]
bounds an array of types representing the upper bounds of either WildcardType or TypeVariable, not null.
bounds
)

Delegates to normalizeUpperBounds(Type[]) unless bounds is empty in which case return an array with the element Object.class.

public static final ParameterizedType
parameterize(final Class<?>
the raw class to create a parameterized type instance for.
rawClass
,
final Map<TypeVariable<?>, Type>
the map used for parameterization.
typeVariableMap
)

Creates a parameterized type instance.

public static final ParameterizedType
parameterize(final Class<?>
the raw class to create a parameterized type instance for.
rawClass
,
final Type...
the types used for parameterization.
typeArguments
)

Creates a parameterized type instance.

private static String

Returns:

String.
parameterizedTypeToString
(final ParameterizedType parameterizedType)

Formats a ParameterizedType as a String.

public static final ParameterizedType
parameterizeWithOwner(final Type
the owning type.
owner
,
final Class<?>
the raw class to create a parameterized type instance for.
rawClass
,
final Map<TypeVariable<?>, Type>
the map used for parameterization.
typeVariableMap
)

Creates a parameterized type instance.

public static final ParameterizedType
parameterizeWithOwner(final Type
the owning type.
owner
,
final Class<?>
the raw class to create a parameterized type instance for.
rawClass
,
final Type...
the types used for parameterization.
typeArguments
)

Creates a parameterized type instance.

private static Type

Returns:

the replaced type.
substituteTypeVariables
(final Type
the type to be replaced.
type
,
final Map<TypeVariable<?>, Type>
the map with type variables.
typeVarAssigns
)

Finds the mapping for type in typeVarAssigns.

public static String

Returns:

String.
toLongString
(final TypeVariable<?>
the type variable to create a String representation for, not null.
typeVariable
)

Formats a TypeVariable including its GenericDeclaration.

public static String

Returns:

String.
toString
(final Type
the type to create a String representation for, not null.
type
)

Formats a given type as a Java-esque String.

public static boolean

Returns:

whether or not the types can be assigned to their respective type variables.
typesSatisfyVariables
(final Map<TypeVariable<?>, Type>
specifies the potential types to be assigned to the type variables, not null.
typeVariableMap
)

Determines whether or not specified types satisfy the bounds of their mapped type variables.

private static String

Returns:

String.
typeVariableToString
(final TypeVariable<?>
TypeVariable to format.
typeVariable
)

Formats a TypeVariable as a String.

private static Type[]

Returns:

bounds with any variables reassigned.
unrollBounds
(final Map<TypeVariable<?>, Type>
assignments Map.
typeArguments
,
final Type[]
in which to expand variables.
bounds
)

Unrolls variables in a type bounds array.

private static Type

Returns:

Type or null if some variable was not in the map.
unrollVariableAssignments
(TypeVariable<?>
the type variable to look up.
typeVariable
,
final Map<TypeVariable<?>, Type>
the map used for the look-up.
typeVarAssigns
)

Looks up typeVariable in typeVarAssigns transitively, i.e. keep looking until the value found is not a type variable.

public static Type

Returns:

Type.
unrollVariables
(Map<TypeVariable<?>, Type> typeArguments, final Type
the type to unroll variable assignments for.
type
)

Gets a type representing type with variable assignments "unrolled."

public static TypeUtils.WildcardTypeBuilder

Returns:

a new WildcardTypeBuilder.
wildcardType
()

Creates a new WildcardTypeBuilder.

private static String

Returns:

String.
wildcardTypeToString
(final WildcardType
WildcardType to format.
wildcardType
)

Formats a WildcardType as a String.

public static <
generic type.
T
>
Typed<T>

Returns:

Typed<T>.
wrap
(final Class<T>
to wrap.
type
)

Wraps the specified Class in a Typed wrapper.

public static <
inferred generic type.
T
>
Typed<T>

Returns:

Typed<T>.
wrap
(final Type
to wrap.
type
)

Wraps the specified Type in a Typed wrapper.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait