Skip to content

Honor generic type information in BeanUtils.copyProperties() #24281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions spring-beans/src/main/java/org/springframework/beans/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -711,21 +712,24 @@ private static void copyProperties(Object source, Object target, @Nullable Class
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
if (readMethod != null) {
ResolvableType sourceResolvableType = ResolvableType.forMethodReturnType(readMethod);
ResolvableType targetResolvableType = ResolvableType.forMethodParameter(writeMethod, 0);
if (targetResolvableType.isAssignableFrom(sourceResolvableType)) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.URL;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -331,6 +332,60 @@ private void assertSignatureEquals(Method desiredMethod, String signature) {
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
}

@Test
void copyPropertiesWithGenericTypes() {
ListComponentA a = new ListComponentA();
a.getList().add(42);
ListComponentB b = new ListComponentB();
ListComponentC c = new ListComponentC();

BeanUtils.copyProperties(a, b);
assertThat(a.getList()).containsOnly(42);
assertThat(b.getList()).containsOnly(42);

BeanUtils.copyProperties(a, c);
assertThat(c.getList()).isEmpty();
}

static class ListComponentA {

private List<Integer> list = new ArrayList<>();

public List<Integer> getList() {
return list;
}

public void setList(List<Integer> list) {
this.list = list;
}
}

static class ListComponentB {

private List<Integer> list = new ArrayList<>();

public List<Integer> getList() {
return list;
}

public void setList(List<Integer> list) {
this.list = list;
}
}

static class ListComponentC {

private List<Long> list = new ArrayList<>();

public List<Long> getList() {
return list;
}

public void setList(List<Long> list) {
this.list = list;
}
}


@SuppressWarnings("unused")
private static class NameAndSpecialProperty {
Expand Down