Package org.int4.dirk.util
Class Types
- java.lang.Object
-
- org.int4.dirk.util.Types
-
public class Types extends java.lang.ObjectSupport functions for types, classes and interfaces.
-
-
Constructor Summary
Constructors Constructor Description Types()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsTypeVariables(java.lang.reflect.Type type)Checks if any type parameters for the givenTypeare bound to variables.static java.util.Set<java.lang.reflect.Type>getGenericSuperTypes(java.lang.reflect.Type type)Given aType, returns a set of all types extended or implemented.static java.util.Set<java.lang.Class<?>>getSuperTypes(java.lang.Class<?> cls)Given aClass, returns a set of all classes and interfaces extended or implemented.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 java.lang.reflect.TypegetTypeParameter(java.lang.reflect.Type type, java.lang.Class<?> cls, java.lang.reflect.TypeVariable<?> typeVariable)Gets theTypeof a generic parameter, identified by given by theTypeVariable, of the givenClasswhen resolved against the givenType.static java.lang.reflect.Type[]getUpperBounds(java.lang.reflect.WildcardType type)Returns the upper bounds of the givenWildcardType.static booleanisAssignable(java.lang.reflect.Type type, java.lang.reflect.Type toType)Checks if the subject type may be implicitly cast to the target type following Java generics rules.static java.lang.reflect.ParameterizedTypeparameterize(java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)Create a newParameterizedTypewith the given type arguments.static <T> java.lang.Class<T>raw(java.lang.reflect.Type type)Gets the raw type of a JavaType.static java.lang.reflect.TyperesolveVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type type)Get a type representingtypewith variable assignments resolved.static java.lang.reflect.WildcardTypewildcardExtends(java.lang.reflect.Type... upperBounds)Creates a newWildcardTypewith the given upper bounds.
-
-
-
Method Detail
-
getSuperTypes
public static java.util.Set<java.lang.Class<?>> getSuperTypes(java.lang.Class<?> cls)
Given aClass, returns a set of all classes and interfaces extended or implemented.- Parameters:
cls- aClass, cannot benull- Returns:
- a set of all classes and interfaces extended or implemented, never
null, never containsnulls and never empty
-
getGenericSuperTypes
public static java.util.Set<java.lang.reflect.Type> getGenericSuperTypes(java.lang.reflect.Type type)
Given aType, returns a set of all types extended or implemented.- Parameters:
type- aType, cannot benull- Returns:
- a set of all types extended or implemented, never
null, never containsnulls and never empty
-
parameterize
public static java.lang.reflect.ParameterizedType parameterize(java.lang.Class<?> raw, java.lang.reflect.Type... typeArguments)Create a newParameterizedTypewith the given type arguments.- Parameters:
raw- aClassto parameterize, cannot benulltypeArguments- an array of type arguments- Returns:
- a
ParameterizedType, nevernull
-
wildcardExtends
public static java.lang.reflect.WildcardType wildcardExtends(java.lang.reflect.Type... upperBounds)
Creates a newWildcardTypewith the given upper bounds. Note that the Java specification does not allow wildcard types with multiple upper bounds to be created in source code, however you can create them programmatically in order to for example filter by types that must implement two interfaces.- Parameters:
upperBounds- an array ofTypes, cannot benullor containnulls- Returns:
- a
WildcardType, nevernull
-
raw
public static <T> java.lang.Class<T> raw(java.lang.reflect.Type type)
Gets the raw type of a JavaType. This works for classes and parameterized types. Returnsnullif no raw type can be derived.- Type Parameters:
T- the expected type- Parameters:
type- aType, cannot benull- Returns:
- a
Classrepresenting the raw type of the given type, can benull
-
containsTypeVariables
public static boolean containsTypeVariables(java.lang.reflect.Type type)
Checks if any type parameters for the givenTypeare bound to variables.- Parameters:
type- aType, cannot benull- Returns:
trueif any parameters are bound to variables, otherwisefalse
-
getTypeParameter
public static java.lang.reflect.Type getTypeParameter(java.lang.reflect.Type type, java.lang.Class<?> cls, java.lang.reflect.TypeVariable<?> typeVariable)Gets theTypeof a generic parameter, identified by given by theTypeVariable, of the givenClasswhen resolved against the givenType.- Parameters:
type- aTypefrom which to determine the parameters of the givenClass, cannot benullcls- aClassto determine a type parameter for, cannot benulltypeVariable- aTypeVariableof the givenClassto extract, cannot benull- Returns:
- a
Type, can benullif theTypeVariablewas not associated with the givenClass
-
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.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.- Parameters:
type- a type from which to determine the type parameters oftoClass, cannot benulltoClass- a class whose type parameters are to be determined based on the subtypetype, cannot benull- Returns:
- a map of the type assignments for the type variables in each type in the inheritance hierarchy from
typetotoClassinclusive ornull
-
resolveVariables
public static java.lang.reflect.Type resolveVariables(java.util.Map<java.lang.reflect.TypeVariable<?>,java.lang.reflect.Type> typeArguments, java.lang.reflect.Type type)Get a type representingtypewith variable assignments resolved.- Parameters:
typeArguments- as fromgetTypeArguments(Type, Class), cannot benulltype- a type to unroll variable assignments for, cannot benull- Returns:
- a
Typeornullwhen not all variables can be resolved
-
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 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 type, cannot benulltoType- the target type, cannot benull- Returns:
trueiftypeis assignable totoType
-
getUpperBounds
public static java.lang.reflect.Type[] getUpperBounds(java.lang.reflect.WildcardType type)
Returns the upper bounds of the givenWildcardType.- Parameters:
type- aWildcardType, cannot benull- Returns:
- a non-empty array containing the upper bounds of the wildcard type, never
null
-
-