This commit is contained in:
Looly
2024-09-19 17:04:31 +08:00
parent bc3f6cf1ee
commit 59c168a99c
29 changed files with 415 additions and 234 deletions

View File

@@ -16,9 +16,11 @@
package org.dromara.hutool.core.bean.copier;
import org.dromara.hutool.core.bean.BeanException;
import org.dromara.hutool.core.bean.PropDesc;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.text.StrUtil;
@@ -63,8 +65,21 @@ public class BeanToBeanCopier<S, T> extends AbsCopier<S, T> {
actualEditable = copyOptions.editable;
}
final Map<String, PropDesc> targetPropDescMap = getBeanDesc(actualEditable).getPropMap(copyOptions.ignoreCase);
if(MapUtil.isEmpty(targetPropDescMap)){
if(copyOptions.ignoreError){
return target;
}
throw new BeanException("No properties for target: {}", actualEditable);
}
final Map<String, PropDesc> sourcePropDescMap = getBeanDesc(source.getClass()).getPropMap(copyOptions.ignoreCase);
if(MapUtil.isEmpty(sourcePropDescMap)){
if(copyOptions.ignoreError){
return target;
}
throw new BeanException("No properties for source: {}", source.getClass());
}
sourcePropDescMap.forEach((sFieldName, sDesc) -> {
if (null == sFieldName || !sDesc.isReadable(copyOptions.transientSupport)) {
// 字段空或不可读,跳过

View File

@@ -91,7 +91,7 @@ public class BeanConverter implements Converter, Serializable {
private Object convertInternal(final Type targetType, final Class<?> targetClass, final Object value) {
if (value instanceof Map ||
value instanceof ValueProvider ||
BeanUtil.isWritableBean(value.getClass())) {
BeanUtil.isReadableBean(value.getClass())) {
if (value instanceof Map && targetClass.isInterface()) {
// 将Map动态代理为Bean
return MapProxy.of((Map<?, ?>) value).toProxyBean(targetClass);

View File

@@ -101,4 +101,11 @@ public class BeanCopierTest {
Assertions.assertEquals("abc", b.getValue());
}
@Test
void stringToBeanTest() {
final String str = "null";
final BeanCopier<A> copier = BeanCopier.of(str, new A(), CopyOptions.of());
final A copy = copier.copy();
Assertions.assertNull(copy.value);
}
}