public class TypeUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
TypeUtils.WildcardTypeBuilder
WildcardType builder. |
| Constructor and Description |
|---|
TypeUtils()
TypeUtils instances should NOT be constructed in standard
programming. |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsTypeVariables(Type type)
Learn, recursively, whether any of the type parameters associated with
type are bound to variables. |
static Map<TypeVariable<?>,Type> |
determineTypeArguments(Class<?> cls,
ParameterizedType superType)
Tries to determine the type arguments of a class/interface based on a
super parameterized type's type arguments.
|
static boolean |
equals(Type t1,
Type t2)
Check equality of types.
|
static GenericArrayType |
genericArrayType(Type componentType)
Create a generic array type instance.
|
static Type |
getArrayComponentType(Type type)
Get the array component type of
type. |
static Class<?> |
getClass(Field field)
获得Field对应的原始类
|
static Class<?> |
getClass(Type type)
获得Type对应的原始类
|
static Class<?> |
getFirstParamClass(Method method)
获取方法的第一个参数类
|
static Type |
getFirstParamType(Method method)
获取方法的第一个参数类型
优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes |
static Type[] |
getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type of
Object if
TypeVariable.getBounds() returns an empty array. |
static Type[] |
getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value of
null if
WildcardType.getLowerBounds() returns an empty array. |
static Type[] |
getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value of
Object if
WildcardType.getUpperBounds() returns an empty array. |
static Class |
getListType(Field field)
获取列表字段对应的类型
|
static Class<?> |
getParamClass(Method method,
int index)
获取方法的参数类
|
static Class<?>[] |
getParamClasses(Method method)
解析方法的参数类型列表
依赖jre\lib\rt.jar |
static Type |
getParamType(Method method,
int index)
获取方法的参数类型
优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes |
static Type[] |
getParamTypes(Method method)
获取方法的参数类型列表
优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes |
static Class<?> |
getRawType(Type type,
Type assigningType)
Get the raw type of a Java type, given its context.
|
static Class<?> |
getReturnClass(Method method)
解析方法的返回类型类列表
|
static Type |
getReturnType(Method method)
获取方法的返回值类型
获取方法的GenericReturnType |
static Type |
getType(Field field)
获取字段对应的Type类型
方法优先获取GenericType,获取不到则获取Type |
static Type |
getTypeArgument(Class<?> clazz)
获得给定类的第一个泛型参数
|
static Type |
getTypeArgument(Class<?> clazz,
int index)
获得给定类的泛型参数
|
static Type |
getTypeArgument(Type type)
获得给定类的第一个泛型参数
|
static Type |
getTypeArgument(Type type,
int index)
获得给定类的泛型参数
|
static Map<TypeVariable<?>,Type> |
getTypeArguments(ParameterizedType type)
Retrieves all the type arguments for this parameterized type
including owner hierarchy arguments such as
Outer<K,V>.Inner<T>.DeepInner<E> . |
static Type[] |
getTypeArguments(Type type)
获得指定类型中所有泛型参数类型
|
static Map<TypeVariable<?>,Type> |
getTypeArguments(Type type,
Class<?> toClass)
Gets the type arguments of a class/interface based on a subtype.
|
static boolean |
isAbstract(Class<?> clazz)
是否为抽象类
|
static boolean |
isAbstractOrInterface(Class<?> clazz)
是抽象类或者接口
|
static boolean |
isArray(Class<?> clazz)
是否为 数组 class 类型
|
static boolean |
isArrayType(Type type)
Learn whether the specified type denotes an array type.
|
static boolean |
isAssignable(Type type,
Type toType)
Checks if the subject type may be implicitly cast to the target type
following the Java generics rules.
|
static boolean |
isBase(Class<?> clazz)
是否为基本类型
1.
|
static boolean |
isBean(Class<?> clazz)
判断是否为Bean对象
判定方法是是否存在只有一个参数的setXXX方法 |
static boolean |
isCollection(Class<?> clazz)
是否为 Collection class 类型
|
static boolean |
isInstance(Object value,
Type type)
Checks if the given value can be assigned to the target type
following the Java generics rules.
|
static boolean |
isIterable(Class<?> clazz)
是否为 Iterable class 类型
|
static boolean |
isJavaBean(Class<?> clazz)
是否为标准的类
这个类必须: |
static boolean |
isJdk(Class<?> clazz)
判断一个类是JDK 自带的类型
jdk 自带的类,classLoader 是为空的。
|
static boolean |
isList(Class clazz)
是否为列表
|
static boolean |
isMap(Class<?> clazz)
是否为 map class 类型
|
static boolean |
isSet(Class clazz)
是否为 set
|
static boolean |
isWildcardGenericType(Type type)
是否为通配符泛型
|
static Type[] |
normalizeUpperBounds(Type[] bounds)
This method 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 ParameterizedType |
parameterize(Class<?> raw,
Map<TypeVariable<?>,Type> typeArgMappings)
Create a parameterized type instance.
|
static ParameterizedType |
parameterize(Class<?> raw,
Type... typeArguments)
Create a parameterized type instance.
|
static ParameterizedType |
parameterizeWithOwner(Type owner,
Class<?> raw,
Map<TypeVariable<?>,Type> typeArgMappings)
Create a parameterized type instance.
|
static ParameterizedType |
parameterizeWithOwner(Type owner,
Class<?> raw,
Type... typeArguments)
Create a parameterized type instance.
|
static String |
toLongString(TypeVariable<?> var)
Format a
TypeVariable including its GenericDeclaration. |
static String |
toString(Type type)
Present a given type as a Java-esque String.
|
static boolean |
typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)
Determines whether or not specified types satisfy the bounds of their
mapped type variables.
|
static Type |
unrollVariables(Map<TypeVariable<?>,Type> typeArguments,
Type type)
Get a type representing
type with variable assignments "unrolled." |
static TypeUtils.WildcardTypeBuilder |
wildcardType()
|
static <T> Typed<T> |
wrap(Class<T> type)
|
static <T> Typed<T> |
wrap(Type type)
|
public TypeUtils()
TypeUtils instances should NOT be constructed in standard
programming. Instead, the class should be used as
TypeUtils.isAssignable(cls, toClass).
This constructor is public to permit tools that require a JavaBean instance to operate.
public static boolean isMap(Class<?> clazz)
clazz - 对象类型public static boolean isArray(Class<?> clazz)
clazz - 对象类型public static boolean isCollection(Class<?> clazz)
clazz - 对象类型public static boolean isIterable(Class<?> clazz)
clazz - 对象类型public static boolean isBase(Class<?> clazz)
clazz - 对象类型public static boolean isAbstract(Class<?> clazz)
clazz - 类public static boolean isAbstractOrInterface(Class<?> clazz)
clazz - 类信息public static boolean isJavaBean(Class<?> clazz)
0、不为 null 1、非接口 2、非抽象类 3、非Enum枚举 4、非数组 5、非注解 6、非原始类型(int, long等) 7、非集合 Iterable 8、非 Map.clas 9、非 JVM 生成类
clazz - 类public static boolean isJdk(Class<?> clazz)
clazz - 类public static boolean isBean(Class<?> clazz)
clazz - 待测试类public static Class getListType(Field field)
field - 字段public static boolean isWildcardGenericType(Type type)
type - 类型public static boolean isList(Class clazz)
clazz - 类型public static boolean isSet(Class clazz)
clazz - 类型public static Class<?> getClass(Type type)
type - Typenullpublic static Type getType(Field field)
field - 字段Type,可能为nullpublic static Class<?> getClass(Field field)
field - Fieldnullpublic static Type getFirstParamType(Method method)
method - 方法Type,可能为nullpublic static Class<?> getFirstParamClass(Method method)
method - 方法nullpublic static Type getParamType(Method method, int index)
method - 方法index - 第几个参数的索引,从0开始计数Type,可能为nullpublic static Class<?> getParamClass(Method method, int index)
method - 方法index - 第几个参数的索引,从0开始计数nullpublic static Type[] getParamTypes(Method method)
method - 方法Type列表,可能为nullMethod.getGenericParameterTypes(),
Method.getParameterTypes()public static Class<?>[] getParamClasses(Method method)
method - t方法Method.getGenericParameterTypes(),
Method.getParameterTypes()public static Type getReturnType(Method method)
method - 方法Type,可能为nullMethod.getGenericReturnType(),
Method.getReturnType()public static Class<?> getReturnClass(Method method)
method - 方法Method.getGenericReturnType(),
Method.getReturnType()public static Type getTypeArgument(Class<?> clazz)
clazz - 被检查的类,必须是已经确定泛型类型的类Type,可能为nullpublic static Type getTypeArgument(Class<?> clazz, int index)
clazz - 被检查的类,必须是已经确定泛型类型的类index - 泛型类型的索引号,既第几个泛型类型Typepublic static Type getTypeArgument(Type type)
type - 被检查的类型,必须是已经确定泛型类型的类型Type,可能为nullpublic static Type getTypeArgument(Type type, int index)
type - 被检查的类型,必须是已经确定泛型类型的类index - 泛型类型的索引号,既第几个泛型类型Typepublic static Type[] getTypeArguments(Type type)
type - 指定类型public static boolean isAssignable(Type type, Type toType)
Checks if the subject type may be implicitly cast to the target type
following the Java generics rules. If both types are Class
objects, the method returns the result of
ClassUtils.isAssignable(Class, Class).
type - the subject type to be assigned to the target typetoType - the target typetrue if type is assignable to toType.public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)
Retrieves 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 a
Map specifying the argument type for each TypeVariable.
type - specifies the subject parameterized type from which to
harvest the parameters.Map of the type arguments to their respective type
variables.public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, 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
interface Map are Object for the subtype
Properties even though the subtype does not
directly implement the Map interface.
This method returns null if type is not assignable to
toClass. 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 type and toClass. So with the above
example, this method will also determine that the type arguments for
Hashtable are also both Object.
In cases where the interface specified by toClass is
(indirectly) implemented more than once (e.g. where toClass
specifies the interface Iterable and
type specifies a parameterized type that implements both
Set and Collection),
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 the type to
toClass hierarchy.
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 typeMap of the type assignments for the type variables in
each type in the inheritance hierarchy from type to
toClass inclusive.public static Map<TypeVariable<?>,Type> determineTypeArguments(Class<?> cls, ParameterizedType superType)
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 subject Class
object to the supertype.
Example: TreeSet sets its parameter as the parameter for
NavigableSet, which in turn sets the
parameter of SortedSet, which in turn sets the
parameter of Set, which in turn sets the parameter of
Collection, which in turn sets the parameter of
Iterable. Since TreeSet's parameter maps
(indirectly) to Iterable's parameter, it will be able to
determine that based on the super type Iterable<? extends
Map<Integer, ? extends Collection<?>>>, the parameter of
TreeSet is ? extends Map<Integer, ? extends
Collection<?>>.
cls - the class whose type parameters are to be determined, not nullsuperType - the super type from which cls's type
arguments are to be determined, not nullMap of the type assignments that could be determined
for the type variables in each type in the inheritance hierarchy from
type to toClass inclusive.public static boolean isInstance(Object value, Type type)
Checks if the given value can be assigned to the target type following the Java generics rules.
value - the value to be checkedtype - the target typetrue if value is an instance of type.public static Type[] normalizeUpperBounds(Type[] bounds)
This method 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 List is a subinterface of Collection,
this method will return the bounds as if the declaration had been:
<K extends java.util.List<String>>
bounds - an array of types representing the upper bounds of either
WildcardType or TypeVariable, not null.bounds minus the
redundant types.public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
Returns an array containing the sole type of Object if
TypeVariable.getBounds() returns an empty array. Otherwise, it
returns the result of TypeVariable.getBounds() passed into
normalizeUpperBounds(java.lang.reflect.Type[]).
typeVariable - the subject type variable, not nullpublic static Type[] getImplicitUpperBounds(WildcardType wildcardType)
Returns an array containing the sole value of Object if
WildcardType.getUpperBounds() returns an empty array. Otherwise,
it returns the result of WildcardType.getUpperBounds()
passed into normalizeUpperBounds(java.lang.reflect.Type[]).
wildcardType - the subject wildcard type, not nullpublic static Type[] getImplicitLowerBounds(WildcardType wildcardType)
Returns an array containing a single value of null if
WildcardType.getLowerBounds() returns an empty array. Otherwise,
it returns the result of WildcardType.getLowerBounds().
wildcardType - the subject wildcard type, not nullpublic static boolean typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)
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
in typeVarAssigns.
typeVarAssigns - specifies the potential types to be assigned to the
type variables, not null.public static Class<?> getRawType(Type type, Type assigningType)
Get the raw type of a Java type, given its context. Primarily for use
with TypeVariables and GenericArrayTypes, or when you do
not know the runtime type of type: if you know you have a
Class instance, it is already raw; if you know you have a
ParameterizedType, its raw type is only a method call away.
type - to resolveassigningType - type to be resolved againstClass object or null if
the type could not be resolvedpublic static boolean isArrayType(Type type)
type - the type to be checkedtrue if type is an array class or a GenericArrayType.public static Type getArrayComponentType(Type type)
type.type - the type to be checkedpublic static Type unrollVariables(Map<TypeVariable<?>,Type> typeArguments, Type type)
type with variable assignments "unrolled."typeArguments - as from getTypeArguments(Type, Class)type - the type to unroll variable assignments forpublic static boolean containsTypeVariables(Type type)
type are bound to variables.type - the type to check for type variablespublic static final ParameterizedType parameterize(Class<?> raw, Type... typeArguments)
raw - the raw class to create a parameterized type instance fortypeArguments - the types used for parameterizationParameterizedTypepublic static final ParameterizedType parameterize(Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)
raw - the raw class to create a parameterized type instance fortypeArgMappings - the mapping used for parameterizationParameterizedTypepublic static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Type... typeArguments)
owner - the owning typeraw - the raw class to create a parameterized type instance fortypeArguments - the types used for parameterizationParameterizedTypepublic static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)
owner - the owning typeraw - the raw class to create a parameterized type instance fortypeArgMappings - the mapping used for parameterizationParameterizedTypepublic static TypeUtils.WildcardTypeBuilder wildcardType()
TypeUtils.WildcardTypeBuilderpublic static GenericArrayType genericArrayType(Type componentType)
componentType - the type of the elements of the array. For example the component type of boolean[]
is booleanGenericArrayTypepublic static boolean equals(Type t1, Type t2)
t1 - the first typet2 - the second typepublic static String toString(Type type)
type - the type to create a String representation for, not nullpublic static String toLongString(TypeVariable<?> var)
TypeVariable including its GenericDeclaration.var - the type variable to create a String representation for, not nullpublic static <T> Typed<T> wrap(Type type)
T - inferred generic typetype - to wrapCopyright © 2019. All rights reserved.