Honor generic type information in BeanUtils.copyProperties()

Prior to this commit, BeanUtils.copyProperties() ignored generic type
information when comparing candidate source and target property types.

This commit reworks the implementation of BeanUtils.copyProperties() so
that generic type information is taken into account when copying
properties.

See gh-24281
This commit is contained in:
Kunal Patel
2020-01-02 19:22:09 +05:30
committed by Sam Brannen
parent cdde19c0bc
commit 89ee0b077f
2 changed files with 58 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ import java.net.URI;
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;
@@ -336,6 +337,49 @@ class BeanUtilsTests {
private void assertSignatureEquals(Method desiredMethod, String signature) {
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
}
@Test
void testCopiedParametersType() {
A a = new A();
a.getList().add(42);
B b = new B();
BeanUtils.copyProperties(a, b);
assertThat(a.getList()).containsOnly(42);
b.getList().forEach(n -> assertThat(n).isInstanceOf(Long.class));
assertThat(b.getList()).isEmpty();
}
class A {
private List<Integer> list = new ArrayList<>();
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
}
class B {
private List<Long> list = new ArrayList<>();
public List<Long> getList() {
return list;
}
public void setList(List<Long> list) {
this.list = list;
}
}
@SuppressWarnings("unused")