Honor generic type information when copying properties with BeanUtils
See original GitHub issueAffects: spring-beans-5.1.2.RELEASE
class XA{}
class XB{}
class A{
List<XA> a = new ArrayList<>();
private String b;
private String c;
public List<XA> getA() {
return a;
}
public void setA(List<XA> a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
class B {
List<XB> a = new ArrayList<>();
private String b;
private String c;
public List<XB> getA() {
return a;
}
public void setA(List<XB> a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
public class TestBeanUtils{
public static void main(String[] args) {
A a = new A();
a.getA().add(new XA());
B b= new B();
BeanUtils.copyProperties(a,b);
List<XB> a1 = b.getA();
for (XB xb : a1) {
System.out.println("AAA");
}
List<XA> a2 = a.getA();
for (XA xb : a2) {
System.out.println("AAA");
}
}
}
run the main method , then there will throw a java.lang.ClassCastException
;
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
BeanUtils (Spring Framework 6.0.2 API)
Copy the property values of the given source bean into the target bean. ... this method honors generic type information when matching properties...
Read more >BeanUtils copyProperties to copy Arraylist - Stack Overflow
Copy property values from the origin bean to the destination bean for all cases where the property names are the same. Share.
Read more >Apache Commons BeanUtils - Baeldung
BeanUtils class provides a copyProperties method that copies the properties of source object to target object where the property name is same ...
Read more >BeanUtils.copyProperties的用法_垃圾王子晗的博客
As of Spring Framework 5.3, this method honors generic type information when matching properties in the source and target objects.
Read more >Spring - How to copy properties from one bean to another?
Spring's BeanUtils provides following methods to copy property values of the given source bean into the target bean.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you for raising the issue.
I have distilled the behavior via the following test.
The reason the test fails with a
ClassCastException
is that the generic type information is ignored inBeanUtils.copyProperties(Object, Object, Class<?>, String...)
since it performs a comparison between the return type of the read method in the source and parameter type of the write method in the target, both of which arejava.util.List
.Note, however, that the generic type information is available in the
java.lang.reflect.Method
instances used. Thus, Spring could potentially honor the generic type information when copying properties; however, Spring currently ignores the generic type information.Superseded by #24281