| Modifier and Type | Class and Description |
|---|---|
static class |
TypeKit.WildcardTypeBuilder |
| Constructor and Description |
|---|
TypeKit() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsTypeVariables(Type type)
递归比较与
type关联的任何类型参数是否绑定到变量. |
static Map<TypeVariable<?>,Type> |
determineTypeArguments(Class<?> cls,
ParameterizedType superType)
尝试基于超参数化类型的类型参数确定类/接口的类型参数
|
static boolean |
equals(Type t1,
Type t2)
检查类型是否相等
|
static GenericArrayType |
genericArrayType(Type componentType)
创建泛型数组类型实例.
|
static Type |
getActualType(Type actualType,
Class<?> typeDefineClass,
Type typeVariable)
获取指定泛型变量对应的真实类型
由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型
使用此方法注意:
|
static Type[] |
getActualTypes(Type actualType,
Class<?> typeDefineClass,
Type... typeVariables)
获取指定泛型变量对应的真实类型
由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型
使用此方法注意:
|
static Type |
getArrayComponentType(Type type)
获取数组组件类型为
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)
如果
TypeVariable.getBounds()返回一个空数组,
则返回一个包含Object 唯一类型的数组. |
static Type[] |
getImplicitLowerBounds(WildcardType wildcardType)
|
static Type[] |
getImplicitUpperBounds(WildcardType wildcardType)
如果
WildcardType.getUpperBounds()返回一个空数组,
则返回一个包含Object唯一值的数组。否则,
它将返回传递给normalizeUpperBounds(java.lang.reflect.Type[])的
WildcardType.getUpperBounds()的结果 |
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)
根据上下文获取Java类型的原始类型
主要用于
TypeVariables和GenericArrayType |
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)
检索此参数化类型的所有类型参数,包括所有者层次结构参数
|
static Type[] |
getTypeArguments(Type type)
获得指定类型中所有泛型参数类型
|
static Map<TypeVariable<?>,Type> |
getTypeArguments(Type type,
Class<?> toClass)
获取基于子类型的类/接口的类型参数
|
static boolean |
hasTypeVeriable(Type... types)
指定泛型数组中是否含有泛型变量
|
static boolean |
isAbstract(Class<?> clazz)
是否为抽象类
|
static boolean |
isAbstractOrInterface(Class<?> clazz)
是抽象类或者接口
|
static boolean |
isArray(Class<?> clazz)
是否为 数组 class 类型
|
static boolean |
isArrayType(Type type)
了解指定的类型是否表示数组类型。
|
static boolean |
isAssignable(Type type,
Type toType)
检查subject类型是否可以按照Java泛型规则隐式转换为目标类型.
|
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)
检查给定值是否可以按照Java泛型规则分配给目标类型
|
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 |
isUnknow(Type type)
是否未知类型
type为null或者
TypeVariable 都视为未知类型 |
static boolean |
isWildcardGenericType(Type type)
是否为通配符泛型
|
static Type[] |
normalizeUpperBounds(Type[] bounds)
该方法在类型变量类型和通配符类型中去除冗余的上界类型
|
static ParameterizedType |
parameterize(Class<?> raw,
Map<TypeVariable<?>,Type> typeArgMappings)
创建参数化类型实例.
|
static ParameterizedType |
parameterize(Class<?> raw,
Type... typeArguments)
创建参数化类型实例.
|
static ParameterizedType |
parameterizeWithOwner(Type owner,
Class<?> raw,
Map<TypeVariable<?>,Type> typeArgMappings)
创建参数化类型实例.
|
static ParameterizedType |
parameterizeWithOwner(Type owner,
Class<?> raw,
Type... typeArguments)
创建参数化类型实例.
|
static String |
toLongString(TypeVariable<?> var)
格式化一个
TypeVariable,包括它的GenericDeclaration |
static ParameterizedType |
toParameterizedType(Type type)
|
static String |
toString(Type type)
将给定的类型表示为java风格的字符串.
|
static boolean |
typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)
确定指定的类型,是否满类型变量的边界
|
static Type |
unrollVariables(Map<TypeVariable<?>,Type> typeArguments,
Type type)
获取一个表示
type的类型,该类型具有“展开”的变量赋值。 |
static TypeKit.WildcardTypeBuilder |
wildcardType()
|
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<?> getClass(Type type)
type - Typenullpublic 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 getTypeArgument(Type type)
type - 被检查的类型,必须是已经确定泛型类型的类型Type,可能为nullpublic static Type getTypeArgument(Type type, int index)
type - 被检查的类型,必须是已经确定泛型类型的类index - 泛型类型的索引号,既第几个泛型类型Typepublic static ParameterizedType toParameterizedType(Type type)
Type 转换为ParameterizedType
ParameterizedType用于获取当前类或父类中泛型参数化后的类型
一般用于获取泛型参数具体的参数类型,例如:
class A<T> class B extends A<String>
通过此方法,传入B.class即可得到BParameterizedType,从而获取到String
type - 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 - 泛型数组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 Type getType(Field field)
field - 字段Type,可能为nullpublic static Class<?> getClass(Field field)
field - Fieldnullpublic 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[] getTypeArguments(Type type)
type - 指定类型public static boolean isAssignable(Type type, Type toType)
Class对象,
则该方法返回ClassKit.isAssignable(Class, Class)的结果type - 要分配给目标类型的主题类型toType - 目标类型type可赋值给toType,则true.public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)
type - 指定要从中获取参数的主题参数化类型Map.public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass)
type - 用于确定toClass的类型参数的类型toClass - 类型参数将根据子类型type确定的类Mappublic static Map<TypeVariable<?>,Type> determineTypeArguments(Class<?> cls, ParameterizedType superType)
cls - 要确定类型参数的类superType - 要从中确定cls的类型参数的超类型Map,可以确定继承层次结构中
从type到toClass的每个类型中的类型变量.public static boolean isInstance(Object value, Type type)
value - 要检查的值type - 目标类型value是type的实例,则true.public static Type[] normalizeUpperBounds(Type[] bounds)
bounds - 表示WildcardType或
TypeVariable的上界的类型数组.bounds的值减去冗余类型的数组.public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
TypeVariable.getBounds()返回一个空数组,
则返回一个包含Object 唯一类型的数组.
否则返回TypeVariable.getBounds()
传递给normalizeUpperBounds(java.lang.reflect.Type[])的结果typeVariable - 类型变量public static Type[] getImplicitUpperBounds(WildcardType wildcardType)
WildcardType.getUpperBounds()返回一个空数组,
则返回一个包含Object唯一值的数组。否则,
它将返回传递给normalizeUpperBounds(java.lang.reflect.Type[])的
WildcardType.getUpperBounds()的结果wildcardType - 通配符类型public static Type[] getImplicitLowerBounds(WildcardType wildcardType)
wildcardType - 通配符类型public static boolean typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)
typeVarAssigns - 指定分配给类型变量的潜在类型,而不是null.public static Class<?> getRawType(Type type, Type assigningType)
TypeVariables和GenericArrayTypetype - 类型assigningType - 要解析的类型Class对象或nullpublic static boolean isArrayType(Type type)
type - 要检查的类型type是数组类或GenericArrayType,则为truepublic static Type getArrayComponentType(Type type)
type.type - 要检查的类型public static Type unrollVariables(Map<TypeVariable<?>,Type> typeArguments, Type type)
type的类型,该类型具有“展开”的变量赋值。typeArguments - 参数 getTypeArguments(Type, Class)type - 变量赋值的类型public static boolean containsTypeVariables(Type type)
type关联的任何类型参数是否绑定到变量.type - 检查类型变量的类型public static final ParameterizedType parameterize(Class<?> raw, Type... typeArguments)
raw - 用于创建参数化类型实例的原始类typeArguments - 用于参数化的类型ParameterizedTypepublic static final ParameterizedType parameterize(Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)
raw - 用于创建参数化类型实例的原始类typeArgMappings - 用于参数化的类型ParameterizedTypepublic static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Type... typeArguments)
owner - 类型raw - 用于创建参数化类型实例的原始类typeArguments - 用于参数化的类型ParameterizedTypepublic static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)
owner - 类型raw - 用于创建参数化类型实例的原始类typeArgMappings - 用于参数化的映射ParameterizedTypepublic static TypeKit.WildcardTypeBuilder wildcardType()
TypeKit.WildcardTypeBuilderpublic static GenericArrayType genericArrayType(Type componentType)
componentType - 数组元素的类型GenericArrayTypepublic static boolean equals(Type t1, Type t2)
t1 - 第一个比较对象t2 - 第二个比较对象public static String toString(Type type)
type - 创建字符串表示的类型public static String toLongString(TypeVariable<?> var)
TypeVariable,包括它的GenericDeclarationvar - 创建字符串表示的类型变量,而不是nullCopyright © 2020. All rights reserved.