Reputation: 1585
I want to copy an entire object which doesn't implement clone method.
The BeanUtils.copyProperties(obj1, obj2)
does the copy however makes the process tedious as we need to register which are the value will be null
.
ex:
ConvertUtils.register(new DateConverter(null), Date.class);
BeanUtils.copyProperties(emp1, emp2);
where the emp2
will have some date methods where the property might be null..
Assume there might be 100
of properties might be null and we need to just ignore the same..
I need the exact copy of the object.
Could someone suggest the best way or a utility to achieve this?
Thanks.
Upvotes: 0
Views: 1982
Reputation: 168
sanbhat's answer is the only way to achieve 100% clone. Be careful, serialization and deserialization are slow processes.
Upvotes: 0
Reputation: 17622
You can serialize the object and deserialize it back. Serialization->Deserialization is alternative to deep cloning
Upvotes: 3