This commit is contained in:
Looly 2022-11-10 12:09:37 +08:00
parent 1eb8acfcba
commit 10ffd8750b
2 changed files with 12 additions and 4 deletions

View File

@ -142,15 +142,15 @@ public class BeanPath implements Serializable {
* @param patternParts 表达式块列表
* @param value
*/
private Object set(final Object bean, final List<String> patternParts, final boolean nextNumberPart, final Object value) {
private void set(final Object bean, final List<String> patternParts, final boolean nextNumberPart, final Object value) {
Object subBean = this.get(patternParts, bean, true);
if (null == subBean) {
final List<String> parentParts = getParentParts(patternParts);
// 避免get方法的重复调用
subBean = this.set(bean, parentParts, lastIsNumber(parentParts), nextNumberPart ? new ArrayList<>() : new HashMap<>());
this.set(bean, parentParts, lastIsNumber(parentParts), nextNumberPart ? new ArrayList<>() : new HashMap<>());
//set中有可能做过转换因此此处重新获取bean
subBean = this.get(patternParts, bean, true);
}
BeanUtil.setFieldValue(subBean, patternParts.get(patternParts.size() - 1), value);
return value;
}
/**

View File

@ -2,6 +2,7 @@ package cn.hutool.core.bean;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.lang.test.bean.UserInfoDict;
import cn.hutool.core.map.Dict;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -144,4 +145,11 @@ public class BeanPathTest {
beanPath.set(map, "张三");
Assert.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
}
@Test
public void putByPathTest() {
final Dict dict = new Dict();
BeanPath.of("aa.bb").set(dict, "BB");
Assert.assertEquals("{aa={bb=BB}}", dict.toString());
}
}