mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
dba35b9395
commit
cbbf4671ba
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.4.2 (2020-09-03)
|
# 5.4.2 (2020-09-05)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 lock放在try外边(pr#1050@Github)
|
* 【core 】 lock放在try外边(pr#1050@Github)
|
||||||
@ -14,11 +14,13 @@
|
|||||||
* 【poi 】 RowUtil增加插入和删除行(pr#1060@Github)
|
* 【poi 】 RowUtil增加插入和删除行(pr#1060@Github)
|
||||||
* 【extra 】 SpringUtil增加注册bean(pr#174@Gitee)
|
* 【extra 】 SpringUtil增加注册bean(pr#174@Gitee)
|
||||||
* 【core 】 修改NetUtil.getMacAddress避免空指针(issue#1057@Github)
|
* 【core 】 修改NetUtil.getMacAddress避免空指针(issue#1057@Github)
|
||||||
* 【core 】 增加EnumItem接口,枚举扩展转换,增加SPI自定义转换(pr#173@Gitee)
|
* 【core 】 增加EnumItem接口,枚举扩展转换,增加SPI自定义转换(pr#173@Github)
|
||||||
|
* 【core 】 TypeUtil增加getActualTypeMap方法
|
||||||
|
|
||||||
### Bug修复#
|
### Bug修复
|
||||||
* 【core 】 重新整理农历节假日,解决一个pr过来的玩笑导致的问题
|
* 【core 】 重新整理农历节假日,解决一个pr过来的玩笑导致的问题
|
||||||
* 【poi 】 修复ExcelFileUtil.isXls判断问题(pr#1055@Github)
|
* 【poi 】 修复ExcelFileUtil.isXls判断问题(pr#1055@Github)
|
||||||
|
* 【poi 】 修复CglibUtil.copyList参数错误导致的问题
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -40,7 +42,6 @@
|
|||||||
* 【core 】 EnumUtil.getEnumAt负数返回null(pr#167@Gitee)
|
* 【core 】 EnumUtil.getEnumAt负数返回null(pr#167@Gitee)
|
||||||
* 【core 】 ChineseDate增加天干地支和转换为公历方法(pr#169@Gitee)
|
* 【core 】 ChineseDate增加天干地支和转换为公历方法(pr#169@Gitee)
|
||||||
* 【core 】 Img增加stroke描边方法(issue#1033@Github)
|
* 【core 】 Img增加stroke描边方法(issue#1033@Github)
|
||||||
* 【core 】 TypeUtil增加getActualTypeMap方法
|
|
||||||
|
|
||||||
### Bug修复#
|
### Bug修复#
|
||||||
* 【poi 】 修复ExcelBase.isXlsx方法判断问题(issue#I1S502@Gitee)
|
* 【poi 】 修复ExcelBase.isXlsx方法判断问题(issue#I1S502@Gitee)
|
||||||
|
@ -61,6 +61,18 @@ public class TypeUtil {
|
|||||||
return field.getGenericType();
|
return field.getGenericType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得字段的泛型类型
|
||||||
|
*
|
||||||
|
* @param clazz Bean类
|
||||||
|
* @param fieldName 字段名
|
||||||
|
* @return 字段的泛型类型
|
||||||
|
* @since 5.4.2
|
||||||
|
*/
|
||||||
|
public static Type getFieldType(Class<?> clazz, String fieldName) {
|
||||||
|
return getType(ReflectUtil.getField(clazz, fieldName));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得Field对应的原始类
|
* 获得Field对应的原始类
|
||||||
*
|
*
|
||||||
@ -73,6 +85,7 @@ public class TypeUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------- Param Type
|
// ----------------------------------------------------------------------------------- Param Type
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取方法的第一个参数类型<br>
|
* 获取方法的第一个参数类型<br>
|
||||||
* 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
|
* 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
|
||||||
@ -101,7 +114,7 @@ public class TypeUtil {
|
|||||||
* 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
|
* 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
|
||||||
*
|
*
|
||||||
* @param method 方法
|
* @param method 方法
|
||||||
* @param index 第几个参数的索引,从0开始计数
|
* @param index 第几个参数的索引,从0开始计数
|
||||||
* @return {@link Type},可能为{@code null}
|
* @return {@link Type},可能为{@code null}
|
||||||
*/
|
*/
|
||||||
public static Type getParamType(Method method, int index) {
|
public static Type getParamType(Method method, int index) {
|
||||||
@ -116,7 +129,7 @@ public class TypeUtil {
|
|||||||
* 获取方法的参数类
|
* 获取方法的参数类
|
||||||
*
|
*
|
||||||
* @param method 方法
|
* @param method 方法
|
||||||
* @param index 第几个参数的索引,从0开始计数
|
* @param index 第几个参数的索引,从0开始计数
|
||||||
* @return 参数类,可能为{@code null}
|
* @return 参数类,可能为{@code null}
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
*/
|
*/
|
||||||
@ -147,7 +160,6 @@ public class TypeUtil {
|
|||||||
*
|
*
|
||||||
* @param method t方法
|
* @param method t方法
|
||||||
* @return 参数类型类列表
|
* @return 参数类型类列表
|
||||||
*
|
|
||||||
* @see Method#getGenericParameterTypes
|
* @see Method#getGenericParameterTypes
|
||||||
* @see Method#getParameterTypes
|
* @see Method#getParameterTypes
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
@ -157,6 +169,7 @@ public class TypeUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------- Return Type
|
// ----------------------------------------------------------------------------------- Return Type
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取方法的返回值类型<br>
|
* 获取方法的返回值类型<br>
|
||||||
* 获取方法的GenericReturnType
|
* 获取方法的GenericReturnType
|
||||||
@ -184,6 +197,7 @@ public class TypeUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------- Type Argument
|
// ----------------------------------------------------------------------------------- Type Argument
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得给定类的第一个泛型参数
|
* 获得给定类的第一个泛型参数
|
||||||
*
|
*
|
||||||
@ -197,7 +211,7 @@ public class TypeUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得给定类的泛型参数
|
* 获得给定类的泛型参数
|
||||||
*
|
*
|
||||||
* @param type 被检查的类型,必须是已经确定泛型类型的类
|
* @param type 被检查的类型,必须是已经确定泛型类型的类
|
||||||
* @param index 泛型类型的索引号,即第几个泛型类型
|
* @param index 泛型类型的索引号,即第几个泛型类型
|
||||||
* @return {@link Type}
|
* @return {@link Type}
|
||||||
*/
|
*/
|
||||||
@ -216,7 +230,7 @@ public class TypeUtil {
|
|||||||
* class A<T>
|
* class A<T>
|
||||||
* class B extends A<String>
|
* class B extends A<String>
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
* <p>
|
||||||
* 通过此方法,传入B.class即可得到String
|
* 通过此方法,传入B.class即可得到String
|
||||||
*
|
*
|
||||||
* @param type 指定类型
|
* @param type 指定类型
|
||||||
@ -240,7 +254,7 @@ public class TypeUtil {
|
|||||||
* class A<T>
|
* class A<T>
|
||||||
* class B extends A<String>
|
* class B extends A<String>
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
* <p>
|
||||||
* 通过此方法,传入B.class即可得到B{@link ParameterizedType},从而获取到String
|
* 通过此方法,传入B.class即可得到B{@link ParameterizedType},从而获取到String
|
||||||
*
|
*
|
||||||
* @param type {@link Type}
|
* @param type {@link Type}
|
||||||
@ -254,10 +268,10 @@ public class TypeUtil {
|
|||||||
} else if (type instanceof Class) {
|
} else if (type instanceof Class) {
|
||||||
final Class<?> clazz = (Class<?>) type;
|
final Class<?> clazz = (Class<?>) type;
|
||||||
Type genericSuper = clazz.getGenericSuperclass();
|
Type genericSuper = clazz.getGenericSuperclass();
|
||||||
if(null == genericSuper || Object.class.equals(genericSuper)){
|
if (null == genericSuper || Object.class.equals(genericSuper)) {
|
||||||
// 如果类没有父类,而是实现一些定义好的泛型接口,则取接口的Type
|
// 如果类没有父类,而是实现一些定义好的泛型接口,则取接口的Type
|
||||||
final Type[] genericInterfaces = clazz.getGenericInterfaces();
|
final Type[] genericInterfaces = clazz.getGenericInterfaces();
|
||||||
if(ArrayUtil.isNotEmpty(genericInterfaces)){
|
if (ArrayUtil.isNotEmpty(genericInterfaces)) {
|
||||||
// 默认取第一个实现接口的泛型Type
|
// 默认取第一个实现接口的泛型Type
|
||||||
genericSuper = genericInterfaces[0];
|
genericSuper = genericInterfaces[0];
|
||||||
}
|
}
|
||||||
@ -276,8 +290,7 @@ public class TypeUtil {
|
|||||||
* 1. typeDefineClass必须是clazz的父类或者clazz实现的接口
|
* 1. typeDefineClass必须是clazz的父类或者clazz实现的接口
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
*
|
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
||||||
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
|
||||||
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
||||||
* @return 给定泛型参数对应的实际类型,如果无对应类型,返回null
|
* @return 给定泛型参数对应的实际类型,如果无对应类型,返回null
|
||||||
* @since 5.4.1
|
* @since 5.4.1
|
||||||
@ -289,12 +302,12 @@ public class TypeUtil {
|
|||||||
|
|
||||||
// 泛型参数标识符列表
|
// 泛型参数标识符列表
|
||||||
final TypeVariable<?>[] typeVars = typeDefineClass.getTypeParameters();
|
final TypeVariable<?>[] typeVars = typeDefineClass.getTypeParameters();
|
||||||
if(ArrayUtil.isEmpty(typeVars)) {
|
if (ArrayUtil.isEmpty(typeVars)) {
|
||||||
return new TableMap<>(0);
|
return new TableMap<>(0);
|
||||||
}
|
}
|
||||||
// 实际类型列表
|
// 实际类型列表
|
||||||
final Type[] actualTypeArguments = TypeUtil.getTypeArguments(actualType);
|
final Type[] actualTypeArguments = TypeUtil.getTypeArguments(actualType);
|
||||||
if(ArrayUtil.isEmpty(actualTypeArguments)) {
|
if (ArrayUtil.isEmpty(actualTypeArguments)) {
|
||||||
return new TableMap<>(0);
|
return new TableMap<>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,10 +324,9 @@ public class TypeUtil {
|
|||||||
* 2. typeVariable必须在superClass中声明
|
* 2. typeVariable必须在superClass中声明
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
*
|
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
||||||
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
|
||||||
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
||||||
* @param typeVariables 泛型变量,需要的实际类型对应的泛型参数
|
* @param typeVariables 泛型变量,需要的实际类型对应的泛型参数
|
||||||
* @return 给定泛型参数对应的实际类型,如果无对应类型,返回null
|
* @return 给定泛型参数对应的实际类型,如果无对应类型,返回null
|
||||||
* @since 4.5.7
|
* @since 4.5.7
|
||||||
*/
|
*/
|
||||||
@ -323,7 +335,7 @@ public class TypeUtil {
|
|||||||
|
|
||||||
// 查找方法定义所在类或接口中此泛型参数的位置
|
// 查找方法定义所在类或接口中此泛型参数的位置
|
||||||
final Type[] result = new Type[typeVariables.length];
|
final Type[] result = new Type[typeVariables.length];
|
||||||
for(int i = 0; i < typeVariables.length; i++) {
|
for (int i = 0; i < typeVariables.length; i++) {
|
||||||
//noinspection SuspiciousMethodCalls
|
//noinspection SuspiciousMethodCalls
|
||||||
result[i] = (typeVariables[i] instanceof TypeVariable) ? tableMap.get(typeVariables[i]) : typeVariables[i];
|
result[i] = (typeVariables[i] instanceof TypeVariable) ? tableMap.get(typeVariables[i]) : typeVariables[i];
|
||||||
}
|
}
|
||||||
@ -340,16 +352,15 @@ public class TypeUtil {
|
|||||||
* 2. typeVariable必须在superClass中声明
|
* 2. typeVariable必须在superClass中声明
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
*
|
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
||||||
* @param actualType 真实类型所在类,此类中记录了泛型参数对应的实际类型
|
|
||||||
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
* @param typeDefineClass 泛型变量声明所在类或接口,此类中定义了泛型类型
|
||||||
* @param typeVariable 泛型变量,需要的实际类型对应的泛型参数
|
* @param typeVariable 泛型变量,需要的实际类型对应的泛型参数
|
||||||
* @return 给定泛型参数对应的实际类型
|
* @return 给定泛型参数对应的实际类型
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static Type getActualType(Type actualType, Class<?> typeDefineClass, Type typeVariable) {
|
public static Type getActualType(Type actualType, Class<?> typeDefineClass, Type typeVariable) {
|
||||||
Type[] types = getActualTypes(actualType, typeDefineClass, typeVariable);
|
Type[] types = getActualTypes(actualType, typeDefineClass, typeVariable);
|
||||||
if(ArrayUtil.isNotEmpty(types)) {
|
if (ArrayUtil.isNotEmpty(types)) {
|
||||||
return types[0];
|
return types[0];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -369,13 +380,14 @@ public class TypeUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定泛型数组中是否含有泛型变量
|
* 指定泛型数组中是否含有泛型变量
|
||||||
|
*
|
||||||
* @param types 泛型数组
|
* @param types 泛型数组
|
||||||
* @return 是否含有泛型变量
|
* @return 是否含有泛型变量
|
||||||
* @since 4.5.7
|
* @since 4.5.7
|
||||||
*/
|
*/
|
||||||
public static boolean hasTypeVeriable(Type... types) {
|
public static boolean hasTypeVeriable(Type... types) {
|
||||||
for (Type type : types) {
|
for (Type type : types) {
|
||||||
if(type instanceof TypeVariable) {
|
if (type instanceof TypeVariable) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -59,7 +60,23 @@ public class TypeUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getActualTypesTest(){
|
public void getActualTypesTest(){
|
||||||
|
final Type id = TypeUtil.getActualType(
|
||||||
|
Station.class,
|
||||||
|
Entity.class,
|
||||||
|
TypeUtil.getFieldType(Station.class, "id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Station extends Tree<Station, Long>{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Tree<E, T> extends Entity<T>{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Entity<T>{
|
||||||
|
private T id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,7 @@ public class CglibUtil {
|
|||||||
public static <S, T> List<T> copyList(Collection<S> source, Supplier<T> target, Converter converter, BiConsumer<S, T> callback) {
|
public static <S, T> List<T> copyList(Collection<S> source, Supplier<T> target, Converter converter, BiConsumer<S, T> callback) {
|
||||||
return source.stream().map(s -> {
|
return source.stream().map(s -> {
|
||||||
T t = target.get();
|
T t = target.get();
|
||||||
copy(source, t, converter);
|
copy(s, t, converter);
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.accept(s, t);
|
callback.accept(s, t);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user