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.

有帮助吗?

解决方案 2

sanbhat's answer is the only way to achieve 100% clone. Be careful, serialization and deserialization are slow processes.

其他提示

You can serialize the object and deserialize it back. Serialization->Deserialization is alternative to deep cloning

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top