public class TypeUtil extends Object
| 构造器和说明 |
|---|
TypeUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Type |
getActualType(Type actualType,
Class<?> typeDefineClass,
Type typeVariable)
获取指定泛型变量对应的真实类型
由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意: 1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明 |
static Type[] |
getActualTypes(Type actualType,
Class<?> typeDefineClass,
Type... typeVariables)
获取指定泛型变量对应的真实类型
由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意: 1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明 |
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 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 boolean |
hasTypeVeriable(Type... types)
指定泛型数组中是否含有泛型变量
|
static boolean |
isUnknow(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 |
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(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即可得到B
ParameterizedType,从而获取到Stringtype - TypeParameterizedTypepublic static Type[] getActualTypes(Type actualType, Class<?> typeDefineClass, Type... typeVariables)
1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明
actualType - 真实类型所在类,此类中记录了泛型参数对应的实际类型typeDefineClass - 泛型变量声明所在类或接口,此类中定义了泛型类型typeVariables - 泛型变量,需要的实际类型对应的泛型参数public static Type getActualType(Type actualType, Class<?> typeDefineClass, Type typeVariable)
1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明
actualType - 真实类型所在类,此类中记录了泛型参数对应的实际类型typeDefineClass - 泛型变量声明所在类或接口,此类中定义了泛型类型typeVariable - 泛型变量,需要的实际类型对应的泛型参数public static boolean isUnknow(Type type)
TypeVariable 都视为未知类型type - Type类型public static boolean hasTypeVeriable(Type... types)
types - 泛型数组Copyright © 2023. All rights reserved.