修复props.toBean 数组字段未赋值问题

This commit is contained in:
Looly
2023-03-26 05:53:45 +08:00
parent 8bd76de6fe
commit 5799e018c1
9 changed files with 106 additions and 62 deletions

View File

@@ -10,11 +10,7 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Bean路径表达式用于获取多层嵌套Bean中的字段值或Bean对象<br>
@@ -136,20 +132,26 @@ public class BeanPath implements Serializable {
* 2. 如果为数组,如果下标不大于数组长度,则替换原有值,否则追加值
* </pre>
*
* @param bean Bean、Map或List
* @param patternParts 表达式块列表
* @param value 值
* @return
* @param bean Bean、Map或List
* @param patternParts 表达式块列表
* @param nextNumberPart 下一个值是否
* @param value
*/
private void set(Object bean, List<String> patternParts, boolean nextNumberPart, Object value) {
Object subBean = this.get(patternParts, bean, true);
if (null == subBean) {
// 当前节点是空,则先创建父节点
final List<String> parentParts = getParentParts(patternParts);
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);
final Object newSubBean = BeanUtil.setFieldValue(subBean, patternParts.get(patternParts.size() - 1), value);
if(newSubBean != subBean){
// 对象变更,重新加入
this.set(bean, getParentParts(patternParts), nextNumberPart, newSubBean);
}
}
/**

View File

@@ -9,29 +9,12 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ModifierUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.*;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.beans.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -116,7 +99,7 @@ public class BeanUtil {
if (method.getParameterCount() == 0) {
final String name = method.getName();
if (name.startsWith("get") || name.startsWith("is")) {
if(false == "getClass".equals(name)){
if (false == "getClass".equals(name)) {
return true;
}
}
@@ -309,24 +292,32 @@ public class BeanUtil {
/**
* 设置字段值通过反射设置字段值并不调用setXXX方法<br>
* 对象同样支持Map类型fieldNameOrIndex即为key
* 对象同样支持Map类型fieldNameOrIndex即为key,支持:
* <ul>
* <li>Map</li>
* <li>List</li>
* <li>Bean</li>
* </ul>
*
* @param bean Bean
* @param fieldNameOrIndex 字段名或序号,序号支持负数
* @param value 值
* @return bean当为数组时返回一个新的数组
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static void setFieldValue(Object bean, String fieldNameOrIndex, Object value) {
public static Object setFieldValue(Object bean, String fieldNameOrIndex, Object value) {
if (bean instanceof Map) {
((Map) bean).put(fieldNameOrIndex, value);
} else if (bean instanceof List) {
ListUtil.setOrPadding((List) bean, Convert.toInt(fieldNameOrIndex), value);
} else if (ArrayUtil.isArray(bean)) {
ArrayUtil.setOrAppend(bean, Convert.toInt(fieldNameOrIndex), value);
// issue#3008追加产生新数组此处返回新数组
return ArrayUtil.setOrAppend(bean, Convert.toInt(fieldNameOrIndex), value);
} else {
// 普通Bean对象
ReflectUtil.setFieldValue(bean, fieldNameOrIndex, value);
}
return bean;
}
/**
@@ -611,6 +602,7 @@ public class BeanUtil {
}
// --------------------------------------------------------------------------------------------- beanToMap
/**
* 将bean的部分属性转换成map<br>
* 可选拷贝哪些属性值,默认是不忽略值为{@code null}的值的。
@@ -623,7 +615,7 @@ public class BeanUtil {
public static Map<String, Object> beanToMap(Object bean, String... properties) {
int mapSize = 16;
Editor<String> keyEditor = null;
if(ArrayUtil.isNotEmpty(properties)){
if (ArrayUtil.isNotEmpty(properties)) {
mapSize = properties.length;
final Set<String> propertiesSet = CollUtil.set(false, properties);
keyEditor = property -> propertiesSet.contains(property) ? property : null;
@@ -733,7 +725,7 @@ public class BeanUtil {
* @return 目标对象
*/
public static <T> T copyProperties(Object source, Class<T> tClass, String... ignoreProperties) {
if(null == source){
if (null == source) {
return null;
}
T target = ReflectUtil.newInstanceIfPossible(tClass);
@@ -773,7 +765,7 @@ public class BeanUtil {
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
*/
public static void copyProperties(Object source, Object target, CopyOptions copyOptions) {
if(null == source){
if (null == source) {
return;
}
BeanCopier.create(source, target, ObjectUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();
@@ -980,14 +972,14 @@ public class BeanUtil {
/**
* 判断source与target的所有公共字段的值是否相同
*
* @param source 待检测对象1
* @param target 待检测对象2
* @param source 待检测对象1
* @param target 待检测对象2
* @param ignoreProperties 不需要检测的字段
* @return 判断结果如果为true则证明所有字段的值都相同
* @since 5.8.4
* @author Takak11
* @since 5.8.4
*/
public static boolean isCommonFieldsEqual(Object source, Object target, String...ignoreProperties) {
public static boolean isCommonFieldsEqual(Object source, Object target, String... ignoreProperties) {
if (null == source && null == target) {
return true;
@@ -1003,7 +995,7 @@ public class BeanUtil {
sourceFields.removeAll(Arrays.asList(ignoreProperties));
for (String field : sourceFields) {
if(ObjectUtil.notEqual(sourceFieldsMap.get(field), targetFieldsMap.get(field))){
if (ObjectUtil.notEqual(sourceFieldsMap.get(field), targetFieldsMap.get(field))) {
return false;
}
}

View File

@@ -348,7 +348,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
Array.set(buffer, index, value);
return buffer;
} else {
if(ArrayUtil.isEmpty(buffer)){
if (ArrayUtil.isEmpty(buffer)) {
// issue#I5APJE
// 可变长类型在buffer为空的情况下类型会被擦除导致报错此处修正
final T[] values = newArray(value.getClass(), 1);
@@ -360,7 +360,8 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
/**
* 将元素值设置为数组的某个位置当给定的index大于数组长度则追加
* 将元素值设置为数组的某个位置当给定的index大于数组长度则追加<br>
* 替换时返回原数组,追加时返回新数组
*
* @param array 已有数组
* @param index 位置,大于长度追加,否则替换
@@ -1037,7 +1038,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
if (null == array) {
return null;
}
if(null == indexes){
if (null == indexes) {
return newArray(array.getClass().getComponentType(), 0);
}
@@ -1656,11 +1657,11 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* 去重数组中的元素,去重后生成新的数组,原数组不变<br>
* 此方法通过{@link LinkedHashSet} 去重
*
* @param <T> 数组元素类型
* @param <K> 唯一键类型
* @param array 数组
* @param <T> 数组元素类型
* @param <K> 唯一键类型
* @param array 数组
* @param uniqueGenerator 唯一键生成器
* @param override 是否覆盖模式,如果为{@code true}加入的新值会覆盖相同key的旧值否则会忽略新加值
* @param override 是否覆盖模式,如果为{@code true}加入的新值会覆盖相同key的旧值否则会忽略新加值
* @return 去重后的数组
* @since 5.8.0
*/
@@ -1671,9 +1672,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
final UniqueKeySet<K, T> set = new UniqueKeySet<>(true, uniqueGenerator);
if(override){
if (override) {
Collections.addAll(set, array);
} else{
} else {
for (T t : array) {
set.addIfAbsent(t);
}

View File

@@ -2,6 +2,8 @@ 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.util.ArrayUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -124,4 +126,20 @@ public class BeanPathTest {
beanPath.set(map, "张三");
Assert.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
}
@Test
public void appendArrayTest(){
// issue#3008@Github
final MyUser myUser = new MyUser();
BeanPath.create("hobby[0]").set(myUser, "LOL");
BeanPath.create("hobby[1]").set(myUser, "KFC");
BeanPath.create("hobby[2]").set(myUser, "COFFE");
Assert.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(myUser.getHobby()));
}
@Data
static class MyUser {
private String[] hobby;
}
}