public class TypeUtil extends Object
| 构造器和说明 |
|---|
TypeUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Type |
getActualType(Type type,
Field field)
获得泛型字段对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
|
static Type |
getActualType(Type type,
ParameterizedType parameterizedType)
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
此方法可以处理复杂的泛型化对象,类似于Map<User, Key<Long>>
|
static Type |
getActualType(Type type,
Type typeVariable)
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
此方法可以处理:
1.
|
static Type[] |
getActualTypes(Type type,
Type... typeVariables)
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
|
static Class<?> |
getClass(Field field)
获得Field对应的原始类
|
static Class<?> |
getClass(Type type)
获得Type对应的原始类
|
static Type |
getFieldType(Class<?> clazz,
String fieldName)
获得字段的泛型类型
|
static Class<?> |
getFirstParamClass(Method method)
获取方法的第一个参数类
|
static Type |
getFirstParamType(Method method)
获取方法的第一个参数类型
优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes |
static ParameterizedType[] |
getGenerics(Class<?> clazz)
获取指定类所有泛型父类和泛型接口
指定类及其所有的泛型父类
指定类实现的直接泛型接口
|
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<?> |
getReturnClass(Method method)
解析方法的返回类型类列表
|
static Type |
getReturnType(Method method)
获取方法的返回值类型
获取方法的GenericReturnType |
static Type |
getType(Field field)
获取字段对应的Type类型
方法优先获取GenericType,获取不到则获取Type |
static Type |
getTypeArgument(Type type)
获得给定类的第一个泛型参数
|
static Type |
getTypeArgument(Type type,
int index)
获得给定类的泛型参数
|
static Type[] |
getTypeArguments(Type type)
获得指定类型中所有泛型参数类型,例如:
class A<T>
class B extends A<String>
通过此方法,传入B.class即可得到String
|
static Map<Type,Type> |
getTypeMap(Class<?> clazz)
获取泛型变量和泛型实际类型的对应关系Map,例如:
T org.dromara.hutool.test.User
E java.lang.Integer
|
static boolean |
hasTypeVariable(Type... types)
指定泛型数组中是否含有泛型变量
|
static boolean |
isUnknown(Type type)
是否未知类型
type为null或者 TypeVariable 都视为未知类型 |
static ParameterizedType |
toParameterizedType(Type type)
将
Type 转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型一般用于获取泛型参数具体的参数类型,例如: class A<T> class B extends A<String> 通过此方法,传入B.class即可得到B ParameterizedType,从而获取到String |
static ParameterizedType |
toParameterizedType(Type type,
int interfaceIndex)
将
Type 转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型一般用于获取泛型参数具体的参数类型,例如:
class A<T>
class B extends A<String>;
通过此方法,传入B.class即可得到B对应的ParameterizedType,从而获取到String |
public static Class<?> getClass(Type type)
type - Typenullpublic static Type getType(Field field)
field - 字段Type,可能为nullpublic static Type getFieldType(Class<?> clazz, String fieldName)
clazz - Bean类fieldName - 字段名public 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(Type type)
type - 被检查的类型,必须是已经确定泛型类型的类型Type,可能为nullpublic static Type getTypeArgument(Type type, int index)
type - 被检查的类型,必须是已经确定泛型类型的类index - 泛型类型的索引号,即第几个泛型类型Typepublic static Type[] getTypeArguments(Type type)
class A<T> class B extends A<String>
通过此方法,传入B.class即可得到String
type - 指定类型public static ParameterizedType toParameterizedType(Type type)
Type 转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型class A<T> class B extends A<String>
通过此方法,传入B.class即可得到BParameterizedType,从而获取到String
type - TypeParameterizedTypepublic static ParameterizedType toParameterizedType(Type type, int interfaceIndex)
Type 转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型
class A<T>
class B extends A<String>;
通过此方法,传入B.class即可得到B对应的ParameterizedType,从而获取到String
type - TypeinterfaceIndex - 实现的第几个接口ParameterizedTypepublic static ParameterizedType[] getGenerics(Class<?> clazz)
clazz - 类public static boolean isUnknown(Type type)
TypeVariable 都视为未知类型type - Type类型public static boolean hasTypeVariable(Type... types)
types - 泛型数组public static Map<Type,Type> getTypeMap(Class<?> clazz)
T org.dromara.hutool.test.User
E java.lang.Integer
clazz - 被解析的包含泛型参数的类public static Type getActualType(Type type, Field field)
type - 实际类型明确的类field - 字段public static Type getActualType(Type type, Type typeVariable)
1. 泛型化对象,类似于Map<User, Key<Long>>
2. 泛型变量,类似于T
type - 类typeVariable - 泛型变量,例如T等public static Type getActualType(Type type, ParameterizedType parameterizedType)
type - 类parameterizedType - 泛型变量,例如List<T>等Copyright © 2024. All rights reserved.