mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
BeanUtil.getFieldValue suuport coll return
This commit is contained in:
parent
97ed8ca46a
commit
b65be102ea
@ -16,6 +16,7 @@
|
|||||||
* 【cache 】 缓存降低锁的粒度,提高并发能力(pr#1385@Github)
|
* 【cache 】 缓存降低锁的粒度,提高并发能力(pr#1385@Github)
|
||||||
* 【core 】 SimpleCache缓存降低锁的粒度,提高并发能力(pr#1385@Github)
|
* 【core 】 SimpleCache缓存降低锁的粒度,提高并发能力(pr#1385@Github)
|
||||||
* 【core 】 增加RadixUtil(pr#260@Gitee)
|
* 【core 】 增加RadixUtil(pr#260@Gitee)
|
||||||
|
* 【core 】 BeanUtil.getFieldValue支持获取字段集合(pr#254@Gitee)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
|
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
|
||||||
|
@ -263,6 +263,12 @@ public class BeanUtil {
|
|||||||
* 获得字段值,通过反射直接获得字段值,并不调用getXXX方法<br>
|
* 获得字段值,通过反射直接获得字段值,并不调用getXXX方法<br>
|
||||||
* 对象同样支持Map类型,fieldNameOrIndex即为key
|
* 对象同样支持Map类型,fieldNameOrIndex即为key
|
||||||
*
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>Map: fieldNameOrIndex需为key,获取对应value</li>
|
||||||
|
* <li>Collection: fieldNameOrIndex当为数字,返回index对应值,非数字遍历集合返回子bean对应name值</li>
|
||||||
|
* <li>Array: fieldNameOrIndex当为数字,返回index对应值,非数字遍历数组返回子bean对应name值</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
* @param bean Bean对象
|
* @param bean Bean对象
|
||||||
* @param fieldNameOrIndex 字段名或序号,序号支持负数
|
* @param fieldNameOrIndex 字段名或序号,序号支持负数
|
||||||
* @return 字段值
|
* @return 字段值
|
||||||
@ -275,9 +281,19 @@ public class BeanUtil {
|
|||||||
if (bean instanceof Map) {
|
if (bean instanceof Map) {
|
||||||
return ((Map<?, ?>) bean).get(fieldNameOrIndex);
|
return ((Map<?, ?>) bean).get(fieldNameOrIndex);
|
||||||
} else if (bean instanceof Collection) {
|
} else if (bean instanceof Collection) {
|
||||||
|
try{
|
||||||
return CollUtil.get((Collection<?>) bean, Integer.parseInt(fieldNameOrIndex));
|
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)) {
|
} else if (ArrayUtil.isArray(bean)) {
|
||||||
|
try{
|
||||||
return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex));
|
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对象
|
} else {// 普通Bean对象
|
||||||
return ReflectUtil.getFieldValue(bean, fieldNameOrIndex);
|
return ReflectUtil.getFieldValue(bean, fieldNameOrIndex);
|
||||||
}
|
}
|
||||||
|
@ -1650,6 +1650,26 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按照指定规则,将一种类型的数组转换为另一种类型
|
||||||
|
*
|
||||||
|
* @param array 被转换的数组
|
||||||
|
* @param targetComponentType 目标的元素类型
|
||||||
|
* @param func 转换规则函数
|
||||||
|
* @param <T> 原数组类型
|
||||||
|
* @param <R> 目标数组类型
|
||||||
|
* @return 转换后的数组
|
||||||
|
* @since 5.5.8
|
||||||
|
*/
|
||||||
|
public static <T, R> R[] map(Object array, Class<R> 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
|
* 按照指定规则,将一种类型的数组元素提取后转换为List
|
||||||
*
|
*
|
||||||
|
@ -5,6 +5,7 @@ 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 cn.hutool.core.util.ArrayUtil;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -537,4 +538,34 @@ public class BeanUtilTest {
|
|||||||
private Long sortOrder;
|
private Long sortOrder;
|
||||||
private Date createTime;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user