question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Honor generic type information when copying properties with BeanUtils

See original GitHub issue

Affects: 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sbrannencommented, Dec 11, 2019

Thank you for raising the issue.

I have distilled the behavior via the following test.

public class BeanUtilsTests {

	@Test
	void test() {
		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;
	}
}

The reason the test fails with a ClassCastException is that the generic type information is ignored in BeanUtils.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 are java.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.

0reactions
sbrannencommented, Apr 13, 2020

Superseded by #24281

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found