修复BeanUtil.copyToList复制Long等类型错误问题

This commit is contained in:
Looly 2023-05-20 02:30:17 +08:00
parent 9eba7f77cf
commit c4d801e9b3
4 changed files with 46 additions and 2 deletions

View File

@ -737,7 +737,14 @@ public class BeanUtil {
if (collection.isEmpty()) {
return new ArrayList<>(0);
}
// issue#3091
if(ClassUtil.isBasicType(targetType) || String.class == targetType){
return Convert.toList(targetType, collection);
}
return collection.stream().map((source) -> {
final T target = ConstructorUtil.newInstanceIfPossible(targetType);
copyProperties(source, target, copyOptions);
return target;

View File

@ -300,6 +300,15 @@ public class PropDesc {
return this;
}
@Override
public String toString() {
return "PropDesc{" +
"field=" + field +
", getter=" + getter +
", setter=" + setter +
'}';
}
//------------------------------------------------------------------------------------ Private method start
/**

View File

@ -77,6 +77,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
public BeanCopier(final Object source, final T target, final Type targetType, final CopyOptions copyOptions) {
Assert.notNull(source, "Source bean must be not null!");
Assert.notNull(target, "Target bean must be not null!");
final Copier<T> copier;
if (source instanceof Map) {
if (target instanceof Map) {
@ -85,11 +86,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
copier = new MapToBeanCopier<>((Map<?, ?>) source, target, targetType, copyOptions);
}
} else if (source instanceof ValueProvider) {
//noinspection unchecked
copier = new ValueProviderToBeanCopier<>((ValueProvider<String>) source, target, targetType, copyOptions);
} else {
if (target instanceof Map) {
//noinspection unchecked
copier = (Copier<T>) new BeanToMapCopier(source, (Map<?, ?>) target, targetType, copyOptions);
} else {
copier = new BeanToBeanCopier<>(source, target, targetType, copyOptions);

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.bean;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
public class Issue3091Test {
@Test
void copyToListTest() {
final List<Long> list = Arrays.asList(1L,2L);
final List<Integer> result = BeanUtil.copyToList(list, Integer.class);
Assertions.assertEquals("[1, 2]", result.toString());
}
}