mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
fix code
This commit is contained in:
parent
683a7b74bd
commit
8c4b1262ef
@ -54,17 +54,17 @@ public class BeanToBeanCopier<S, T> extends AbsCopier<S, T> {
|
||||
return;
|
||||
}
|
||||
|
||||
// 忽略不需要拷贝的 key,
|
||||
if (false == copyOptions.testKeyFilter(sFieldName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
sFieldName = copyOptions.editFieldName(sFieldName);
|
||||
// 对key做转换,转换后为null的跳过
|
||||
if (null == sFieldName) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 忽略不需要拷贝的 key,
|
||||
if (false == copyOptions.testKeyFilter(sFieldName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查目标字段可写性
|
||||
final PropDesc tDesc = targetPropDescMap.get(sFieldName);
|
||||
if (null == tDesc || false == tDesc.isWritable(this.copyOptions.transientSupport)) {
|
||||
|
@ -8,6 +8,7 @@ import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.core.lang.func.LambdaUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
@ -78,12 +79,12 @@ public class CopyOptions implements Serializable {
|
||||
* 自定义类型转换器,默认使用全局万能转换器转换
|
||||
*/
|
||||
protected TypeConverter converter = (type, value) -> {
|
||||
if(null == value){
|
||||
if (null == value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(value instanceof IJSONTypeConverter) {
|
||||
return ((IJSONTypeConverter)value).toBean(ObjectUtil.defaultIfNull(type, Object.class));
|
||||
if (value instanceof IJSONTypeConverter) {
|
||||
return ((IJSONTypeConverter) value).toBean(ObjectUtil.defaultIfNull(type, Object.class));
|
||||
}
|
||||
|
||||
return Convert.convertWithCheck(type, value, null, ignoreError);
|
||||
@ -293,7 +294,7 @@ public class CopyOptions implements Serializable {
|
||||
*/
|
||||
protected Object editFieldValue(String fieldName, Object fieldValue) {
|
||||
return (null != this.fieldValueEditor) ?
|
||||
this.fieldValueEditor.apply(fieldName, fieldValue) : fieldValue;
|
||||
this.fieldValueEditor.apply(fieldName, fieldValue) : fieldValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -343,7 +344,7 @@ public class CopyOptions implements Serializable {
|
||||
*/
|
||||
protected Object convertField(Type targetType, Object fieldValue) {
|
||||
return (null != this.converter) ?
|
||||
this.converter.convert(targetType, fieldValue) : fieldValue;
|
||||
this.converter.convert(targetType, fieldValue) : fieldValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -375,6 +376,19 @@ public class CopyOptions implements Serializable {
|
||||
* @return 是否保留
|
||||
*/
|
||||
protected boolean testKeyFilter(Object key) {
|
||||
return CollUtil.isEmpty(this.ignoreKeySet) || false == this.ignoreKeySet.contains(key);
|
||||
if (CollUtil.isEmpty(this.ignoreKeySet)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ignoreCase) {
|
||||
// 忽略大小写时要遍历检查
|
||||
for (final String ignoreKey : this.ignoreKeySet) {
|
||||
if (StrUtil.equalsIgnoreCase(key.toString(), ignoreKey)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false == this.ignoreKeySet.contains(key);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IssueI80FP4Test {
|
||||
@Test
|
||||
public void copyPropertiesTest() {
|
||||
final Dest sourceDest = new Dest();
|
||||
sourceDest.setCPF(33699);
|
||||
sourceDest.setEnderDest("abc");
|
||||
|
||||
final Dest dest = new Dest();
|
||||
final CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true).setIgnoreCase(true).setIgnoreProperties("enderDest");
|
||||
BeanUtil.copyProperties(sourceDest, dest, copyOptions);
|
||||
Assert.assertNull(dest.getEnderDest());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Dest{
|
||||
private int cPF;
|
||||
private String enderDest;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user