Class TypeUtils
- Since:
- 3.0
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsTypeVariables(Type type) Tests, recursively, whether any of the type parameters associated withtypeare bound to variables.static Map<TypeVariable<?>, Type> determineTypeArguments(Class<?> cls, ParameterizedType superParameterizedType) Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments.static booleanTests equality of types.static GenericArrayTypegenericArrayType(Type componentType) Creates a generic array type instance.static TypegetArrayComponentType(Type type) Gets the array component type oftype.static Type[]getImplicitBounds(TypeVariable<?> typeVariable) Gets an array containing the sole type ofObjectifTypeVariable.getBounds()returns an empty array.static Type[]getImplicitLowerBounds(WildcardType wildcardType) Gets an array containing a single value ofnullifWildcardType.getLowerBounds()returns an empty array.static Type[]getImplicitUpperBounds(WildcardType wildcardType) Gets an array containing the sole value ofObjectifWildcardType.getUpperBounds()returns an empty array.static Class<?> getRawType(Type type, Type assigningType) Gets the raw type of a Java type, given its context.static Map<TypeVariable<?>, Type> Gets 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 booleanisArrayType(Type type) Tests whether the specified type denotes an array type.static booleanisAssignable(Type type, Type toType) Tests if the subject type may be implicitly cast to the target type following the Java generics rules.static booleanisInstance(Object value, Type type) Tests if the given value can be assigned to the target type following the Java generics rules.static Type[]normalizeUpperBounds(Type[] 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).static final ParameterizedTypeparameterize(Class<?> rawClass, Type... typeArguments) Creates a parameterized type instance.static final ParameterizedTypeparameterize(Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap) Creates a parameterized type instance.static final ParameterizedTypeparameterizeWithOwner(Type owner, Class<?> rawClass, Type... typeArguments) Creates a parameterized type instance.static final ParameterizedTypeparameterizeWithOwner(Type owner, Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap) Creates a parameterized type instance.static StringtoLongString(TypeVariable<?> typeVariable) Formats aTypeVariableincluding itsGenericDeclaration.static StringFormats a given type as a Java-esque String.static booleantypesSatisfyVariables(Map<TypeVariable<?>, Type> typeVariableMap) Determines whether or not specified types satisfy the bounds of their mapped type variables.static TypeunrollVariables(Map<TypeVariable<?>, Type> typeArguments, Type type) Gets a type representingtypewith variable assignments "unrolled."Gets aTypeUtils.WildcardTypeBuilder.static <T> Typed<T> static <T> Typed<T>
-
Field Details
-
WILDCARD_ALL
A wildcard instance matching?.- Since:
- 3.2
-
-
Constructor Details
-
TypeUtils
Deprecated.TODO Make private in 4.0.TypeUtilsinstances should NOT be constructed in standard programming. Instead, the class should be used asTypeUtils.isAssignable(cls, toClass).This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
containsTypeVariables
Tests, recursively, whether any of the type parameters associated withtypeare bound to variables.- Parameters:
type- the type to check for type variables- Returns:
- boolean
- Since:
- 3.2
-
determineTypeArguments
public static Map<TypeVariable<?>,Type> determineTypeArguments(Class<?> cls, ParameterizedType superParameterizedType) Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments. This method is the inverse ofgetTypeArguments(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 subjectClassobject to the supertype.Example:
TreeSetsets 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 ofTreeSetis? extends Map<Integer, ? extends Collection<?>>.- Parameters:
cls- the class whose type parameters are to be determined, notnullsuperParameterizedType- the super type from whichcls's type arguments are to be determined, notnull- Returns:
- a
Mapof the type assignments that could be determined for the type variables in each type in the inheritance hierarchy fromtypetotoClassinclusive. - Throws:
NullPointerException- if eitherclsorsuperParameterizedTypeisnull
-
equals
Tests equality of types.- Parameters:
type1- the first typetype2- the second type- Returns:
- boolean
- Since:
- 3.2
-
genericArrayType
Creates 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
-
getArrayComponentType
Gets the array component type oftype.- Parameters:
type- the type to be checked- Returns:
- component type or null if type is not an array type
-
getImplicitBounds
Gets an array containing the sole type ofObjectifTypeVariable.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, which could be
Object. - Throws:
NullPointerException- iftypeVariableisnull
-
getImplicitLowerBounds
Gets an array containing a single value ofnullifWildcardType.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, which could be null.
- Throws:
NullPointerException- ifwildcardTypeisnull
-
getImplicitUpperBounds
Gets an array containing the sole value ofObjectifWildcardType.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.
- Throws:
NullPointerException- ifwildcardTypeisnull
-
getRawType
Gets the raw type of a Java type, given its context. Primarily for use withTypeVariables andGenericArrayTypes, or when you do not know the runtime type oftype: if you know you have aClassinstance, 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
Classobject ornullif the type could not be resolved
-
getTypeArguments
Gets all the type arguments for this parameterized type including owner hierarchy arguments such asOuter<K, V>.Inner<T>.DeepInner<E>. The arguments are returned in aMapspecifying the argument type for eachTypeVariable.- Parameters:
type- specifies the subject parameterized type from which to harvest the parameters.- Returns:
- a
Mapof the type arguments to their respective type variables.
-
getTypeArguments
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 interfaceMapareObjectfor the subtypePropertieseven though the subtype does not directly implement theMapinterface.This method returns
nulliftypeis 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
typeandtoClass. So with the above example, this method will also determine that the type arguments forHashtableare also bothObject. In cases where the interface specified bytoClassis (indirectly) implemented more than once (e.g. wheretoClassspecifies the interfaceIterableandtypespecifies a parameterized type that implements bothSetandCollection), 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 thetypetotoClasshierarchy.- Parameters:
type- the type from which to determine the type parameters oftoClasstoClass- the class whose type parameters are to be determined based on the subtypetype- Returns:
- a
Mapof the type assignments for the type variables in each type in the inheritance hierarchy fromtypetotoClassinclusive.
-
isArrayType
Tests whether the specified type denotes an array type.- Parameters:
type- the type to be checked- Returns:
trueiftypeis an array class or aGenericArrayType.
-
isAssignable
Tests if the subject type may be implicitly cast to the target type following the Java generics rules. If both types areClassobjects, 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:
trueiftypeis assignable totoType.
-
isInstance
Tests 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:
trueifvalueis an instance oftype.
-
normalizeUpperBounds
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
Listis 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 eitherWildcardTypeorTypeVariable, notnull.- Returns:
- an array containing the values from
boundsminus the redundant types. - Throws:
NullPointerException- ifboundsisnull
-
parameterize
public static final ParameterizedType parameterize(Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap) Creates a parameterized type instance.- Parameters:
rawClass- the raw class to create a parameterized type instance fortypeVariableMap- the map used for parameterization- Returns:
ParameterizedType- Throws:
NullPointerException- if eitherrawClassortypeVariableMapisnull- Since:
- 3.2
-
parameterize
Creates a parameterized type instance.- Parameters:
rawClass- the raw class to create a parameterized type instance fortypeArguments- the types used for parameterization- Returns:
ParameterizedType- Throws:
NullPointerException- ifrawClassisnull- Since:
- 3.2
-
parameterizeWithOwner
public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap) Creates a parameterized type instance.- Parameters:
owner- the owning typerawClass- the raw class to create a parameterized type instance fortypeVariableMap- the map used for parameterization- Returns:
ParameterizedType- Throws:
NullPointerException- if eitherrawClassortypeVariableMapisnull- Since:
- 3.2
-
parameterizeWithOwner
public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Type... typeArguments) Creates a parameterized type instance.- Parameters:
owner- the owning typerawClass- the raw class to create a parameterized type instance fortypeArguments- the types used for parameterization- Returns:
ParameterizedType- Throws:
NullPointerException- ifrawClassisnull- Since:
- 3.2
-
toLongString
Formats aTypeVariableincluding itsGenericDeclaration.- Parameters:
typeVariable- the type variable to create a String representation for, notnull- Returns:
- String
- Throws:
NullPointerException- iftypeVariableisnull- Since:
- 3.2
-
toString
Formats a given type as a Java-esque String.- Parameters:
type- the type to create a String representation for, notnull- Returns:
- String
- Throws:
NullPointerException- iftypeisnull- Since:
- 3.2
-
typesSatisfyVariables
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:
typeVariableMap- 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.
- Throws:
NullPointerException- iftypeVariableMapisnull
-
unrollVariables
Gets a type representingtypewith variable assignments "unrolled."- Parameters:
typeArguments- as fromgetTypeArguments(Type, Class)type- the type to unroll variable assignments for- Returns:
- Type
- Since:
- 3.2
-
wildcardType
Gets aTypeUtils.WildcardTypeBuilder.- Returns:
TypeUtils.WildcardTypeBuilder- Since:
- 3.2
-
wrap
- Type Parameters:
T- generic type- Parameters:
type- to wrap- Returns:
Typed<T>- Since:
- 3.2
-
wrap
- Type Parameters:
T- inferred generic type- Parameters:
type- to wrap- Returns:
Typed<T>- Since:
- 3.2
-