My framework handles domain objects of any class. I need to be able to shallow clone such a domain object instance foo
, as if it implemented Cloneable
(which it doesn't) and I called Object.clone()
on it which returns a shallow clone.
Things that do NOT work:
- Force foo's class to implement
Cloneable
and therefor the public methodclone()
. - Through reflection call foo.clone() (to reach the protected method
Object.clone()
). It throwsCloneNotSupportedException
becausefoo
's class does not implementCloneable
. - Serializing and deserializing foo: I need a shallow copy, not a deep copy.
Limitations:
foo
's class is not know at compile time.foo
might have field that are not exposed as getters/setters.
Note: there are a couple of similar questions, but none seem to focus on a getting a shallow clone.