This commit is contained in:
Looly 2023-08-30 22:09:12 +08:00
parent a3efec9c52
commit b0eea9f919
3 changed files with 9 additions and 9 deletions

View File

@ -337,7 +337,8 @@ public class BeanUtil {
if (bean instanceof Map) {
((Map) bean).put(fieldNameOrIndex, value);
} else if (bean instanceof List) {
ListUtil.setOrPadding((List) bean, Convert.toInt(fieldNameOrIndex), value);
// 相对于5.x逻辑变更与数组逻辑保持一致
ListUtil.setOrAppend((List) bean, Convert.toInt(fieldNameOrIndex), value);
} else if (ArrayUtil.isArray(bean)) {
// issue#3008追加产生新数组此处返回新数组
return ArrayUtil.setOrAppend(bean, Convert.toInt(fieldNameOrIndex), value);

View File

@ -20,6 +20,7 @@ import org.dromara.hutool.core.collection.partition.RandomAccessAvgPartition;
import org.dromara.hutool.core.collection.partition.RandomAccessPartition;
import org.dromara.hutool.core.comparator.PinyinComparator;
import org.dromara.hutool.core.comparator.PropertyComparator;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.page.PageInfo;
import org.dromara.hutool.core.util.ObjUtil;
@ -464,7 +465,7 @@ public class ListUtil {
*
* @param <T> 元素类型
* @param list List列表
* @param index 位置
* @param index 位置不能大于(list.size()+1) * 2
* @param element 新元素
* @param paddingElement 填充的值
* @return 原List
@ -476,6 +477,10 @@ public class ListUtil {
if (index < size) {
list.set(index, element);
} else {
// issue#3286, 增加安全检查最多增加2倍
if(index > (list.size() + 1) * 2) {
throw new HutoolException("Index is too large:", index);
}
for (int i = size; i < index; i++) {
list.add(paddingElement);
}

View File

@ -456,13 +456,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
}
this.rawList.add(index, InternalJSONUtil.wrap(element, this.config));
} else {
// issue#3286, 如果用户指定的index太大容易造成Java heap space错误
// if (!config.isIgnoreNullValue()) {
// while (index != this.size()) {
// // 非末尾则填充null
// this.add(null);
// }
// }
// 相对于5.x逻辑变更当index大于size则追加而不是补充null这样更加安全
this.add(element);
}