This commit is contained in:
Looly 2023-09-12 18:25:57 +08:00
parent 683a7b74bd
commit 8c4b1262ef
3 changed files with 51 additions and 11 deletions

View File

@ -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)) {

View File

@ -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);
@ -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);
}
}

View File

@ -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;
}
}