diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47d2a498e..8c49281cf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@
* 【cache 】 缓存降低锁的粒度,提高并发能力(pr#1385@Github)
* 【core 】 SimpleCache缓存降低锁的粒度,提高并发能力(pr#1385@Github)
* 【core 】 增加RadixUtil(pr#260@Gitee)
+* 【core 】 BeanUtil.getFieldValue支持获取字段集合(pr#254@Gitee)
### Bug修复
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java
index 33fafbd4d..e99afde07 100644
--- a/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java
@@ -263,11 +263,17 @@ public class BeanUtil {
* 获得字段值,通过反射直接获得字段值,并不调用getXXX方法
* 对象同样支持Map类型,fieldNameOrIndex即为key
*
+ *
+ * - Map: fieldNameOrIndex需为key,获取对应value
+ * - Collection: fieldNameOrIndex当为数字,返回index对应值,非数字遍历集合返回子bean对应name值
+ * - Array: fieldNameOrIndex当为数字,返回index对应值,非数字遍历数组返回子bean对应name值
+ *
+ *
* @param bean Bean对象
* @param fieldNameOrIndex 字段名或序号,序号支持负数
* @return 字段值
*/
- public static Object getFieldValue(Object bean, String fieldNameOrIndex) {
+ public static Object getFieldValue(Object bean, String fieldNameOrIndex) {
if (null == bean || null == fieldNameOrIndex) {
return null;
}
@@ -275,9 +281,19 @@ public class BeanUtil {
if (bean instanceof Map) {
return ((Map, ?>) bean).get(fieldNameOrIndex);
} else if (bean instanceof Collection) {
- return CollUtil.get((Collection>) bean, Integer.parseInt(fieldNameOrIndex));
+ try{
+ return CollUtil.get((Collection>) bean, Integer.parseInt(fieldNameOrIndex));
+ } catch (NumberFormatException e){
+ // 非数字,see pr#254@Gitee
+ return CollUtil.map((Collection>) bean, (beanEle)-> getFieldValue(beanEle, fieldNameOrIndex), false);
+ }
} else if (ArrayUtil.isArray(bean)) {
- return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex));
+ try{
+ return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex));
+ } catch (NumberFormatException e){
+ // 非数字,see pr#254@Gitee
+ return ArrayUtil.map(bean, Object.class, (beanEle)-> getFieldValue(beanEle, fieldNameOrIndex));
+ }
} else {// 普通Bean对象
return ReflectUtil.getFieldValue(bean, fieldNameOrIndex);
}
diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java
index e4fb7c1c5..ea32bca85 100644
--- a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java
@@ -1650,6 +1650,26 @@ public class ArrayUtil extends PrimitiveArrayUtil {
return result;
}
+ /**
+ * 按照指定规则,将一种类型的数组转换为另一种类型
+ *
+ * @param array 被转换的数组
+ * @param targetComponentType 目标的元素类型
+ * @param func 转换规则函数
+ * @param 原数组类型
+ * @param 目标数组类型
+ * @return 转换后的数组
+ * @since 5.5.8
+ */
+ public static R[] map(Object array, Class targetComponentType, Function super T, ? extends R> func) {
+ final int length = length(array);
+ final R[] result = newArray(targetComponentType, length);
+ for (int i = 0; i < length; i++) {
+ result[i] = func.apply(get(array, i));
+ }
+ return result;
+ }
+
/**
* 按照指定规则,将一种类型的数组元素提取后转换为List
*
diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java
index 56ec38dac..a54fea44e 100644
--- a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java
@@ -5,6 +5,7 @@ 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 cn.hutool.core.util.ArrayUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@@ -537,4 +538,34 @@ public class BeanUtilTest {
private Long sortOrder;
private Date createTime;
}
+
+ @Test
+ public void getFieldValue(){
+ TestPojo testPojo = new TestPojo();
+ testPojo.setName("名字");
+
+ TestPojo2 testPojo2 = new TestPojo2();
+ testPojo2.setAge(2);
+ TestPojo2 testPojo3 = new TestPojo2();
+ testPojo3.setAge(3);
+
+
+ testPojo.setTestPojo2List(new TestPojo2[]{testPojo2,testPojo3});
+
+ BeanPath beanPath = BeanPath.create("testPojo2List.age");
+ Object o = beanPath.get(testPojo);
+
+ Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o,0));
+ Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o,1));
+ }
+
+ @Data
+ public static class TestPojo{
+ private String name;
+ private TestPojo2[] testPojo2List;
+ }
+ @Data
+ public static class TestPojo2{
+ private int age;
+ }
}