7

I have an arbitrary object tagged as Serializable. It has various mutable child objects representing monetary amounts, collections etc. I want to be able to clone this object so if the user modifies it and then reverts their action, I can just replace the clone with a fresh clone.

That implies I need a deep copy because I don't want the users changes to child objects (e.g. adding / inserting items from the collection) appearing on the original.

The objects don't implement Cloneable and as they're autogenerated I can't add such a method either.

Short of painfully handwriting a deep clone is there anyway to accomplish the same in GWT? If this were Java I could consider serializing the object to a byte array stream and deserializing a fresh copy.

I can't do that in GWT. Potentially I could harness RPC because the object is sent to a servlet later. I just don't know if that is easy to do.

Anyone know a simple way I can do a deep copy?

locka
  • 5,809
  • 3
  • 33
  • 38
  • What generates the types that you want to want to clone? – Jason Terk Mar 01 '11 at 18:06
  • A domain specific language. It's not something that can be changed easily and even if it could I don't relish the 500+ objects it generates all having extra GWT bloat to handle deep copy. For the moment I've hand coded deep copy where I need it but even that one instance takes up about 100 lines of code. – locka Mar 01 '11 at 20:37

1 Answers1

1

If you have a chance to add an interface or annotation to your domain classes, you can use gwt-ent for reflection, and you can easily write your own deep copy semantic by means of reflection. I have used this technique to improve gwt serialization performance in dev mode.

As another solution, you can use gwt default serialization mechanism for deep copy, but unfotunately I dont have any clue how you could accomplish this goal..

Gursel Koca
  • 20,940
  • 2
  • 24
  • 34
  • 1
    It looks like an interesting project but I don't think I can use it for this. In the end I just handwrote the deepcopy code much though I hated to do it. – locka Mar 02 '11 at 14:52
  • This isn't how I solved it (just wrote the code in the end), but the link is worth knowing. – locka Mar 29 '11 at 08:07
  • @locka any updates on something that is available right now? I'm thinking that I'll have to do something just like this. – nikhil Apr 02 '13 at 15:30
  • I just wrote a hand crafted solution. I didn't pursue the issue any further. – locka Apr 10 '13 at 16:04