Class TypeUtils
- java.lang.Object
-
- org.int4.dirk.org.apache.commons.lang3.reflect.TypeUtils
-
public class TypeUtils extends java.lang.ObjectUtility methods focusing on type inspection, particularly with regard to generics.- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTypeUtils.WildcardTypeBuilderWildcardTypebuilder.
-
Constructor Summary
Constructors Constructor Description TypeUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsTypeVariables(java.lang.reflect.Type type)Tests, recursively, whether any of the type parameters associated withtypeare bound to variables.static java.lang.reflect.Type[]getImplicitUpperBounds(java.lang.reflect.WildcardType wildcardType)Gets an array containing the sole value ofObjectifWildcardType.getUpperBounds()returns an empty array.static java.lang.Class<?>getRawType(java.lang.reflect.Type type, java.lang.reflect.Type assigningType)Gets the raw type of a Java type, given its context.static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type>getTypeArguments(java.lang.reflect.ParameterizedType type)Gets all the type arguments for this parameterized type including owner hierarchy arguments such asOuter<K, V>.Inner<T>.DeepInner<E>.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.static booleanisAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType)Tests if the subject type may be implicitly cast to the target type following the Java generics rules.static java.lang.reflect.ParameterizedTypeparameterize(java.lang.Class<?> rawClass, java.lang.reflect.Type... typeArguments)Creates a parameterized type instance.static java.lang.reflect.TypeunrollVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type type)Gets a type representingtypewith variable assignments "unrolled."static TypeUtils.WildcardTypeBuilderwildcardType()Gets aTypeUtils.WildcardTypeBuilder.
-
-
-
Method Detail
-
containsTypeVariables
public static boolean containsTypeVariables(java.lang.reflect.Type type)
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
-
getImplicitUpperBounds
public static java.lang.reflect.Type[] getImplicitUpperBounds(java.lang.reflect.WildcardType wildcardType)
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.
-
getRawType
public static java.lang.Class<?> getRawType(java.lang.reflect.Type type, java.lang.reflect.Type assigningType)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
public static java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> getTypeArguments(java.lang.reflect.ParameterizedType type)
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
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 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.
-
isAssignable
public static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType)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.
-
parameterize
public static final java.lang.reflect.ParameterizedType parameterize(java.lang.Class<?> rawClass, java.lang.reflect.Type... typeArguments)Creates a parameterized type instance.- Parameters:
rawClass- the raw class to create a parameterized type instance fortypeArguments- the types used for parameterization- Returns:
ParameterizedType- Since:
- 3.2
-
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)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
public static TypeUtils.WildcardTypeBuilder wildcardType()
Gets aTypeUtils.WildcardTypeBuilder.- Returns:
TypeUtils.WildcardTypeBuilder- Since:
- 3.2
-
-