mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复BeanUtil.copyToList复制Long等类型错误问题
This commit is contained in:
parent
9eba7f77cf
commit
c4d801e9b3
@ -737,7 +737,14 @@ public class BeanUtil {
|
|||||||
if (collection.isEmpty()) {
|
if (collection.isEmpty()) {
|
||||||
return new ArrayList<>(0);
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issue#3091
|
||||||
|
if(ClassUtil.isBasicType(targetType) || String.class == targetType){
|
||||||
|
return Convert.toList(targetType, collection);
|
||||||
|
}
|
||||||
|
|
||||||
return collection.stream().map((source) -> {
|
return collection.stream().map((source) -> {
|
||||||
|
|
||||||
final T target = ConstructorUtil.newInstanceIfPossible(targetType);
|
final T target = ConstructorUtil.newInstanceIfPossible(targetType);
|
||||||
copyProperties(source, target, copyOptions);
|
copyProperties(source, target, copyOptions);
|
||||||
return target;
|
return target;
|
||||||
|
@ -300,6 +300,15 @@ public class PropDesc {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PropDesc{" +
|
||||||
|
"field=" + field +
|
||||||
|
", getter=" + getter +
|
||||||
|
", setter=" + setter +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------ Private method start
|
//------------------------------------------------------------------------------------ Private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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) {
|
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(source, "Source bean must be not null!");
|
||||||
Assert.notNull(target, "Target bean must be not null!");
|
Assert.notNull(target, "Target bean must be not null!");
|
||||||
|
|
||||||
final Copier<T> copier;
|
final Copier<T> copier;
|
||||||
if (source instanceof Map) {
|
if (source instanceof Map) {
|
||||||
if (target 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);
|
copier = new MapToBeanCopier<>((Map<?, ?>) source, target, targetType, copyOptions);
|
||||||
}
|
}
|
||||||
} else if (source instanceof ValueProvider) {
|
} else if (source instanceof ValueProvider) {
|
||||||
//noinspection unchecked
|
|
||||||
copier = new ValueProviderToBeanCopier<>((ValueProvider<String>) source, target, targetType, copyOptions);
|
copier = new ValueProviderToBeanCopier<>((ValueProvider<String>) source, target, targetType, copyOptions);
|
||||||
} else {
|
} else {
|
||||||
if (target instanceof Map) {
|
if (target instanceof Map) {
|
||||||
//noinspection unchecked
|
|
||||||
copier = (Copier<T>) new BeanToMapCopier(source, (Map<?, ?>) target, targetType, copyOptions);
|
copier = (Copier<T>) new BeanToMapCopier(source, (Map<?, ?>) target, targetType, copyOptions);
|
||||||
} else {
|
} else {
|
||||||
copier = new BeanToBeanCopier<>(source, target, targetType, copyOptions);
|
copier = new BeanToBeanCopier<>(source, target, targetType, copyOptions);
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user