Frage

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.

War es hilfreich?

Lösung 2

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top