I have the following example where I try to copy a private attribute from the source instance to the target instance.
public class MyClass {
public void cloneTo(MyClass target) {
target.identifier = this.identifier; // identifier is not null
System.out.println(target.getIdentifier()) // problem: prints null
}
}
This code usually should work, but the problem is that the MyClass
instance is a CGLIB
proxy: MyClass$EnhancerBySpringCGLIB$someId
, and in this case, the identifier attribute is not set in the proxied target class, so when I call getIdentifier()
, it returns null instead of the identifier.
Is it possible to copy private attributes without creating a getter/setter for each attribute?