This commit is contained in:
Looly
2024-09-22 10:16:56 +08:00
parent f770e00904
commit 2fdbb46259
46 changed files with 721 additions and 796 deletions

View File

@@ -62,19 +62,19 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
}
final Map<String, PropDesc> targetPropDescMap = getBeanDesc(actualEditable).getPropMap(copyOptions.ignoreCase);
targetPropDescMap.forEach((tFieldName, tDesc) -> {
targetPropDescMap.forEach((tFieldName, propDesc) -> {
if (null == tFieldName) {
return;
}
// 检查目标字段可写性
if (null == tDesc || !tDesc.isWritable(this.copyOptions.transientSupport)) {
if (null == propDesc || !propDesc.isWritable(this.copyOptions.transientSupport)) {
// 字段不可写,跳过之
return;
}
// 获取目标字段真实类型
final Type fieldType = TypeUtil.getActualType(this.targetType ,tDesc.getFieldType());
final Type fieldType = TypeUtil.getActualType(this.targetType ,propDesc.getFieldType());
// 编辑键值对
final MutableEntry<Object, Object> entry = copyOptions.editField(tFieldName, null);
if(null == entry){
@@ -92,12 +92,12 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
final Object sValue = source.value(tFieldName, fieldType);
// 检查目标对象属性是否过滤属性
if (!copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
if (!copyOptions.testPropertyFilter(propDesc.getField(), sValue)) {
return;
}
// 目标赋值
tDesc.setValue(this.target, sValue, copyOptions.ignoreNullValue, copyOptions.ignoreError, copyOptions.override);
propDesc.setValue(this.target, sValue, copyOptions.ignoreNullValue, copyOptions.ignoreError, copyOptions.override);
});
return this.target;
}

View File

@@ -29,14 +29,31 @@ import java.util.Date;
* 日期转换器
*
* @author Looly
*
*/
public class CalendarConverter extends AbstractConverter {
private static final long serialVersionUID = 1L;
/** 日期格式化 */
/**
* 日期格式化
*/
private String format;
/**
* 构造
*/
public CalendarConverter() {
this(null);
}
/**
* 构造
*
* @param format 日期格式,{@code null}表示无格式定义
*/
public CalendarConverter(final String format) {
this.format = format;
}
/**
* 获取日期格式
*
@@ -59,16 +76,16 @@ public class CalendarConverter extends AbstractConverter {
protected Calendar convertInternal(final Class<?> targetClass, final Object value) {
// Handle Date
if (value instanceof Date) {
return CalendarUtil.calendar((Date)value);
return CalendarUtil.calendar((Date) value);
}
// Handle Long
if (value instanceof Long) {
//此处使用自动拆装箱
return CalendarUtil.calendar((Long)value);
return CalendarUtil.calendar((Long) value);
}
if(value instanceof XMLGregorianCalendar){
if (value instanceof XMLGregorianCalendar) {
return CalendarUtil.calendar((XMLGregorianCalendar) value);
}

View File

@@ -57,7 +57,7 @@ public class DateConverter extends AbstractConverter implements MatcherConverter
/**
* 构造
*
* @param format 日期格式
* @param format 日期格式{@code null}表示无格式定义
*/
public DateConverter(final String format) {
this.format = format;

View File

@@ -63,6 +63,8 @@ public class TypeUtil {
}
} else if(type instanceof GenericArrayType){
return Array.newInstance(getClass(((GenericArrayType)type).getGenericComponentType()), 0).getClass();
} else if(type instanceof TypeReference){
return getClass(((TypeReference<?>)type).getType());
}
}
return null;
@@ -429,6 +431,7 @@ public class TypeUtil {
}
if (typeVariable instanceof TypeVariable) {
// TODO TypeReference无效
return ActualTypeMapperPool.getActualType(type, (TypeVariable<?>) typeVariable);
}

View File

@@ -17,14 +17,16 @@
package org.dromara.hutool.core.util;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.func.LambdaUtil;
import org.dromara.hutool.core.func.SerFunction;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -40,11 +42,11 @@ public class EnumUtil {
/**
* 指定类是否为Enum类
*
* @param clazz
* @param type
* @return 是否为Enum类
*/
public static boolean isEnum(final Class<?> clazz) {
return Assert.notNull(clazz).isEnum();
public static boolean isEnum(final Type type) {
return Assert.notNull(TypeUtil.getClass(type)).isEnum();
}
/**

View File

@@ -16,10 +16,11 @@
package org.dromara.hutool.core.util;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.reflect.TypeUtil;
import lombok.Data;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.reflect.TypeReference;
import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -27,9 +28,16 @@ import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TypeUtilTest {
@Test
void getMapClassTest() {
final Class<?> aClass = TypeUtil.getClass(new TypeReference<Map<String, String>>() {});
Assertions.assertEquals(Map.class, aClass);
}
@Test
public void getEleTypeTest() {
final Method method = MethodUtil.getMethod(TestClass.class, "getList");