fix copyProperties bug

This commit is contained in:
Looly 2020-05-09 21:50:15 +08:00
parent 8289e6a8da
commit c1edd9b401
6 changed files with 82 additions and 11 deletions

View File

@ -7,10 +7,12 @@
### 新特性
* 【core 】 增加URLUtil.getContentLength方法issue#I1GB1Z@Gitee
* 【extra 】 增加PinyinUtilissue#I1GMIV@Gitee
### Bug修复
* 【extra 】 修复Ftp设置超时问题
* 【extra 】 修复TreeUtil根据id查找子节点时的NPE问题pr#120@Gitee
* 【extra 】 修复Ftp设置超时问题issue#I1GMTQ@Gitee
* 【core 】 修复TreeUtil根据id查找子节点时的NPE问题pr#120@Gitee
* 【core 】 修复BeanUtil.copyProperties中Alias注解无效问题issue#I1GK3M@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -141,7 +141,7 @@ public class BeanDesc implements Serializable{
for (Field field : ReflectUtil.getFields(this.beanClass)) {
if(false == ModifierUtil.isStatic(field)) {
//只针对非static属性
this.propMap.put(field.getName(), createProp(field));
this.propMap.put(ReflectUtil.getFieldName(field), createProp(field));
}
}
return this;

View File

@ -3,6 +3,7 @@ package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter;
@ -13,6 +14,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
@ -3393,19 +3395,32 @@ public class ArrayUtil {
// ------------------------------------------------------------------------------------------------------------ min and max
/**
* 取最小值
*
*
* @param <T> 元素类型
* @param numberArray 数字数组
* @return 最小值
* @since 3.0.9
*/
public static <T extends Comparable<? super T>> T min(T[] numberArray) {
return min(numberArray, null);
}
/**
* 取最小值
*
* @param <T> 元素类型
* @param numberArray 数字数组
* @param comparator 比较器null按照默认比较
* @return 最小值
* @since 5.3.4
*/
public static <T extends Comparable<? super T>> T min(T[] numberArray, Comparator<T> comparator) {
if (isEmpty(numberArray)) {
throw new IllegalArgumentException("Number array must not empty !");
}
T min = numberArray[0];
for (T t : numberArray) {
if (ObjectUtil.compare(min, t) > 0) {
if (CompareUtil.compare(min, t, comparator) > 0) {
min = t;
}
}
@ -3554,19 +3569,32 @@ public class ArrayUtil {
/**
* 取最大值
*
*
* @param <T> 元素类型
* @param numberArray 数字数组
* @return 最大值
* @since 3.0.9
*/
public static <T extends Comparable<? super T>> T max(T[] numberArray) {
return max(numberArray, null);
}
/**
* 取最大值
*
* @param <T> 元素类型
* @param numberArray 数字数组
* @param comparator 比较器null表示默认比较器
* @return 最大值
* @since 5.3.4
*/
public static <T extends Comparable<? super T>> T max(T[] numberArray, Comparator<T> comparator) {
if (isEmpty(numberArray)) {
throw new IllegalArgumentException("Number array must not empty !");
}
T max = numberArray[0];
for (int i = 1; i < numberArray.length; i++) {
if (ObjectUtil.compare(max, numberArray[i]) < 0) {
if (CompareUtil.compare(max, numberArray[i], comparator) < 0) {
max = numberArray[i];
}
}

View File

@ -149,7 +149,7 @@ public class ReflectUtil {
final Field[] fields = getFields(beanClass);
if (ArrayUtil.isNotEmpty(fields)) {
for (Field field : fields) {
if ((name.equals(field.getName()))) {
if ((name.equals(getFieldName(field)))) {
return field;
}
}

View File

@ -5,12 +5,14 @@ import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.bean.copier.ValueProvider;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.junit.Assert;
import org.junit.Test;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
@ -213,7 +215,7 @@ public class BeanUtilTest {
}
@Test
public void copyProperties() {
public void copyPropertiesTest() {
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
@ -331,9 +333,9 @@ public class BeanUtilTest {
}
@Test
public void beanToBeanTest(){
public void beanToBeanTest() {
// 修复对象无getter方法导致报错的问题
Page page1=new Page();
Page page1 = new Page();
BeanUtil.toBean(page1, Page.class);
}
@ -349,4 +351,30 @@ public class BeanUtilTest {
return this;
}
}
@Test
public void copyBeanToBeanTest() {
// 测试在copyProperties方法中alias是否有效
Food info = new Food();
info.setBookID("0");
info.setCode("123");
HllFoodEntity entity = new HllFoodEntity();
BeanUtil.copyProperties(info, entity);
Assert.assertEquals(info.getBookID(), entity.getBookId());
Assert.assertEquals(info.getCode(), entity.getCode2());
}
@Data
public static class Food {
@Alias("bookId")
private String bookID;
private String code;
}
@Data
public static class HllFoodEntity implements Serializable {
private String bookId;
@Alias("code")
private String code2;
}
}

View File

@ -6,7 +6,9 @@ import cn.hutool.core.lang.Filter;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
@ -161,6 +163,17 @@ public class ArrayUtilTest {
double maxDouble = ArrayUtil.max(1D, 2.4D, 13.0D, 4.55D, 5D);
Assert.assertEquals(13.0, maxDouble, 2);
BigDecimal one = new BigDecimal("1.00");
BigDecimal two = new BigDecimal("2.0");
BigDecimal three = new BigDecimal("3");
BigDecimal[] bigDecimals = {two,one,three};
BigDecimal minAccuracy = ArrayUtil.min(bigDecimals, Comparator.comparingInt(BigDecimal::scale));
Assert.assertEquals(minAccuracy,three);
BigDecimal maxAccuracy = ArrayUtil.max(bigDecimals,Comparator.comparingInt(BigDecimal::scale));
Assert.assertEquals(maxAccuracy,one);
}
@Test