Browse Source

fix copyProperties bug

Looly 5 years ago
parent
commit
c1edd9b401

+ 4 - 2
CHANGELOG.md

@@ -7,10 +7,12 @@
 
 
 ### 新特性
 ### 新特性
 * 【core   】     增加URLUtil.getContentLength方法(issue#I1GB1Z@Gitee)
 * 【core   】     增加URLUtil.getContentLength方法(issue#I1GB1Z@Gitee)
+* 【extra  】     增加PinyinUtil(issue#I1GMIV@Gitee)
 
 
 ### Bug修复
 ### 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)
 
 
 -------------------------------------------------------------------------------------------------------------
 -------------------------------------------------------------------------------------------------------------
 
 

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/bean/BeanDesc.java

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

+ 32 - 4
hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java

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

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java

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

+ 31 - 3
hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java

@@ -5,12 +5,14 @@ import cn.hutool.core.bean.copier.CopyOptions;
 import cn.hutool.core.bean.copier.ValueProvider;
 import cn.hutool.core.bean.copier.ValueProvider;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.map.MapUtil;
+import lombok.Data;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.Setter;
 import org.junit.Assert;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.Test;
 
 
 import java.beans.PropertyDescriptor;
 import java.beans.PropertyDescriptor;
+import java.io.Serializable;
 import java.lang.reflect.Type;
 import java.lang.reflect.Type;
 import java.time.LocalDate;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
@@ -213,7 +215,7 @@ public class BeanUtilTest {
 	}
 	}
 
 
 	@Test
 	@Test
-	public void copyProperties() {
+	public void copyPropertiesTest() {
 		SubPerson person = new SubPerson();
 		SubPerson person = new SubPerson();
 		person.setAge(14);
 		person.setAge(14);
 		person.setOpenid("11213232");
 		person.setOpenid("11213232");
@@ -331,9 +333,9 @@ public class BeanUtilTest {
 	}
 	}
 
 
 	@Test
 	@Test
-	public void beanToBeanTest(){
+	public void beanToBeanTest() {
 		// 修复对象无getter方法导致报错的问题
 		// 修复对象无getter方法导致报错的问题
-		Page page1=new Page();
+		Page page1 = new Page();
 		BeanUtil.toBean(page1, Page.class);
 		BeanUtil.toBean(page1, Page.class);
 	}
 	}
 
 
@@ -349,4 +351,30 @@ public class BeanUtilTest {
 			return this;
 			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;
+	}
 }
 }

+ 13 - 0
hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java

@@ -6,7 +6,9 @@ import cn.hutool.core.lang.Filter;
 import org.junit.Assert;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.Map;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Objects;
 
 
@@ -161,6 +163,17 @@ public class ArrayUtilTest {
 
 
 		double maxDouble = ArrayUtil.max(1D, 2.4D, 13.0D, 4.55D, 5D);
 		double maxDouble = ArrayUtil.max(1D, 2.4D, 13.0D, 4.55D, 5D);
 		Assert.assertEquals(13.0, maxDouble, 2);
 		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
 	@Test