TypeUtils¶
- public class org.apache.commons.lang3.reflect.TypeUtils[source]¶
Utility methods focusing on type inspection, particularly with regard to generics.
- Since:
3.0
Constructors¶
Deprecated. |
- @Deprecated public TypeUtils()[source]¶
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.
Fields and constants¶
A wildcard instance matching |
- public static final WildcardType WILDCARD_ALL[source]¶
A wildcard instance matching
?.- Since:
3.2
Methods¶
Tests, recursively, whether any of the type parameters associated with |
|
Tries to determine the type arguments of a class/interface based on a super parameterized type’s type arguments. |
|
Tests whether the given types are equal. |
|
Creates a generic array type instance. |
|
Gets the array component type of |
|
Gets an array containing the sole type of |
|
Gets an array containing a single value of |
|
Gets an array containing the sole value of |
|
Gets the raw type of a Java type, given its context. |
|
Gets all the type arguments for this parameterized type including owner hierarchy arguments such as |
|
Gets the type arguments of a class/interface based on a subtype. |
|
Tests whether the specified type denotes an array type. |
|
Tests if the subject type may be implicitly cast to the target type following the Java generics rules. |
|
Tests if the given value can be assigned to the target type following the Java generics rules. |
|
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). |
|
Creates a parameterized type instance. |
|
Creates a parameterized type instance. |
|
Creates a parameterized type instance. |
|
Creates a parameterized type instance. |
|
Formats a |
|
Formats a given type as a Java-esque String. |
|
Determines whether or not specified types satisfy the bounds of their mapped type variables. |
|
Gets a type representing |
|
Creates a new |
|
- public static boolean containsTypeVariables(Type type)[source]¶
Tests, recursively, whether any of the type parameters associated with
typeare bound to variables.- Parameters:
type – The type to check for type variables.
- Returns:
Whether any of the type parameters associated with
typeare bound to variables.- Since:
3.2
- public static Map<TypeVariable<?>, Type> determineTypeArguments(Class<?> cls, ParameterizedType superParameterizedType)[source]¶
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 subjectClassobject to the supertype.Example:
TreeSetsets its parameter as the parameter forNavigableSet, which in turn sets the parameter ofjava.util.SortedSet, which in turn sets the parameter ofSet, which in turn sets the parameter ofjava.util.Collection, 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, not
null.superParameterizedType – The super type from which
cls’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:
java.lang.NullPointerException – Thrown if either
clsorsuperParameterizedTypeisnull.
- public static boolean equals(Type type1, Type type2)[source]¶
Tests whether the given types are equal.
- Parameters:
type1 – The first type.
type2 – The second type.
- Returns:
Whether the given types are equal.
- Since:
3.2
- public static GenericArrayType genericArrayType(Type componentType)[source]¶
Creates a generic array type instance.
- Parameters:
componentType – The type of the elements of the array. For example the component type of
boolean[]isboolean.- Returns:
- Since:
3.2
- public static Type getArrayComponentType(Type type)[source]¶
Gets the array component type of
type.- Parameters:
type – The type to be checked.
- Returns:
component type or null if type is not an array type.
- public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)[source]¶
Gets an array containing the sole type of
ObjectifTypeVariable#getBounds()returns an empty array. Otherwise, it returns the result ofTypeVariable#getBounds()passed into#normalizeUpperBounds.- Parameters:
typeVariable – The subject type variable, not
null.- Returns:
A non-empty array containing the bounds of the type variable, which could be
Object.- Throws:
java.lang.NullPointerException – Thrown if
typeVariableisnull.
- public static Type[] getImplicitLowerBounds(WildcardType wildcardType)[source]¶
Gets an array containing a single value of
nullifWildcardType#getLowerBounds()returns an empty array. Otherwise, it returns the result ofWildcardType#getLowerBounds().- Parameters:
wildcardType – The subject wildcard type, not
null.- Returns:
A non-empty array containing the lower bounds of the wildcard type, which could be null.
- Throws:
java.lang.NullPointerException – Thrown if
wildcardTypeisnull.
- public static Type[] getImplicitUpperBounds(WildcardType wildcardType)[source]¶
Gets an array containing the sole value of
ObjectifWildcardType#getUpperBounds()returns an empty array. Otherwise, it returns the result ofWildcardType#getUpperBounds()passed into#normalizeUpperBounds.- Parameters:
wildcardType – The subject wildcard type, not
null.- Returns:
A non-empty array containing the upper bounds of the wildcard type.
- Throws:
java.lang.NullPointerException – Thrown if
wildcardTypeisnull.
- public static Class<?> getRawType(Type type, Type assigningType)[source]¶
Gets the raw type of a Java type, given its context. Primarily for use with
TypeVariables 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 resolve.
assigningType – type to be resolved against.
- Returns:
The resolved
Classobject ornullif the type could not be resolved.
- public static Map<TypeVariable<?>, Type> getTypeArguments(ParameterizedType type)[source]¶
Gets 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 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.
- public static Map<TypeVariable<?>, Type> getTypeArguments(Type type, Class<?> toClass)[source]¶
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
MapareObjectfor 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 of
toClasstoClass – The class whose type parameters are to be determined based on the subtype
type
- Returns:
A
Mapof the type assignments for the type variables in each type in the inheritance hierarchy fromtypetotoClassinclusive.
- public static boolean isArrayType(Type type)[source]¶
Tests whether the specified type denotes an array type.
- Parameters:
type – The type to be checked.
- Returns:
trueiftypeis an array class or aGenericArrayType.
- public static boolean isAssignable(Type type, Type toType)[source]¶
Tests if the subject type may be implicitly cast to the target type following the Java generics rules. If both types are
Classobjects, the method returns the result ofClassUtils#isAssignable(Class, Class).- Parameters:
type – The subject type to be assigned to the target type.
toType – The target type.
- Returns:
trueiftypeis assignable totoType.
- public static boolean isInstance(Object value, Type type)[source]¶
Tests if the given value can be assigned to the target type following the Java generics rules.
- Parameters:
value – The value to be checked.
type – The target type.
- Returns:
trueifvalueis an instance oftype.
- public static Type[] normalizeUpperBounds(Type[] bounds)[source]¶
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 either
WildcardTypeorTypeVariable, notnull.- Returns:
An array containing the values from
boundsminus the redundant types.- Throws:
java.lang.NullPointerException – Thrown if
boundsisnull.
- public static final ParameterizedType parameterize(Class<?> rawClass, Type... typeArguments)[source]¶
Creates a parameterized type instance.
- Parameters:
rawClass – The raw class to create a parameterized type instance for.
typeArguments – The types used for parameterization.
- Returns:
- Throws:
java.lang.NullPointerException – Thrown if
rawClassisnull.- Since:
3.2
- public static final ParameterizedType parameterize(Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap)[source]¶
Creates a parameterized type instance.
- Parameters:
rawClass – The raw class to create a parameterized type instance for.
typeVariableMap – The map used for parameterization.
- Returns:
- Throws:
java.lang.NullPointerException – Thrown if either
rawClassortypeVariableMapisnull.- Since:
3.2
- public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Type... typeArguments)[source]¶
Creates a parameterized type instance.
- Parameters:
owner – The owning type.
rawClass – The raw class to create a parameterized type instance for.
typeArguments – The types used for parameterization.
- Returns:
- Throws:
java.lang.NullPointerException – Thrown if
rawClassisnull.- Since:
3.2
- public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Map<TypeVariable<?>, Type> typeVariableMap)[source]¶
Creates a parameterized type instance.
- Parameters:
owner – The owning type.
rawClass – The raw class to create a parameterized type instance for.
typeVariableMap – The map used for parameterization.
- Returns:
- Throws:
java.lang.NullPointerException – Thrown if either
rawClassortypeVariableMapisnull.- Since:
3.2
- public static String toLongString(TypeVariable<?> typeVariable)[source]¶
Formats a
TypeVariableincluding itsGenericDeclaration.- Parameters:
typeVariable – The type variable to create a String representation for, not
null.- Returns:
String.
- Throws:
java.lang.NullPointerException – Thrown if
typeVariableisnull.- Since:
3.2
- public static String toString(Type type)[source]¶
Formats a given type as a Java-esque String.
- Parameters:
type – The type to create a String representation for, not
null.- Returns:
String.
- Throws:
java.lang.NullPointerException – Thrown if
typeisnull.- Since:
3.2
- public static boolean typesSatisfyVariables(Map<TypeVariable<?>, Type> typeVariableMap)[source]¶
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, not
null.- Returns:
whether or not the types can be assigned to their respective type variables.
- Throws:
java.lang.NullPointerException – Thrown if
typeVariableMapisnull.
- public static Type unrollVariables(Map<TypeVariable<?>, Type> typeArguments, Type type)[source]¶
Gets a type representing
typewith variable assignments “unrolled.”- Parameters:
typeArguments – as from
TypeUtils#getTypeArguments(Type, Class).type – The type to unroll variable assignments for.
- Returns:
Type.
- Since:
3.2
- public static TypeUtils.WildcardTypeBuilder wildcardType()[source]¶
Creates a new
WildcardTypeBuilder.- Returns:
A new
WildcardTypeBuilder.- Since:
3.2