Object deep copy or serialization

1,125 views
Skip to first unread message

Anky

unread,
Dec 6, 2011, 2:39:21 PM12/6/11
to Google Web Toolkit
Hi,

I have a requirement in my project where a screen data is to be
rendered from a cloned or object copy rather than on actual object.
This way, if user wish to cancel the changes, the original object will
not be affected and if user accepts the changes, the modified cloned
object replaces the original object.

Is there any proven technique to clone an object in GWT? I was
thinking of using serialization classes, like SerializerBase,
SerializationStreamReader, SerializationStreamWriter in GWT API but
their implementation is not clear to me. Moreover, our project does
not support 3rd party APIs and hence I cannot use the supporting Jars
for this objective.

Any help in this context would be appreciated.

Thanks,
Anky

Jens

unread,
Dec 6, 2011, 6:32:54 PM12/6/11
to google-we...@googlegroups.com
I think you only have the standard options:

- Use the Editor Framework of GWT (using it you would have to call a flush() method that writes changes to your object. If a user hits cancel you just won't call flush() -> no need for cloning)
- You could implement Object.clone(), implement a custom .copy() method or use copy constructors.
- If you want to traverse your object graph more often for different purposes, it may be a good idea to implement the visitor pattern and then implement a CreateCopyVisitor.

Currently I can only think of GWT's AutoBean Framework which supports cloning "out of the box" using the AutoBeanUtils class, e.g. AutoBeanUtils.decodeInto(AutoBeanUtils.encode(autobean), newAutoBean). But that would require that your objects are AutoBeans.


-- J.

-sowdri-

unread,
Dec 7, 2011, 12:07:47 AM12/7/11
to google-we...@googlegroups.com
Autobean directly have a clone() method:

clone()

An AutoBean and the property values stored within it can be cloned. The clone() method has a boolean parameter that will trigger a deep or a shallow copy. Any tag values associated with the AutoBean will not be cloned. AutoBeans that wrap a delegate object cannot be cloned.


>> e.g. AutoBeanUtils.decodeInto(AutoBeanUtils.encode(autobean), newAutoBean)
Is there any advantage of doing it this way? Am I missing something?

Thanks, 

 

Thomas Broyer

unread,
Dec 7, 2011, 5:34:46 AM12/7/11
to google-we...@googlegroups.com
Just an additional note to what others said: GWT-RPC serialization is asymmetric, to have the best (but still rather poor) performance on the client; so you cannot use SerializationStreamReader/SerializationStreamWriter to clone an object by roundtripping on the client-side.

Thomas Lefort

unread,
Feb 22, 2013, 8:32:00 AM2/22/13
to google-we...@googlegroups.com
Sorry for reviving this thread. I do have a recurring need for duplicating/cloning (shallow and deep) objects, so I would be interested in a (elegant) solution (for both).

1. (not very elegant) I have tried to do serialize/deserialize using my rpc TypeOracle etc... but I have the effect mentioned by Thomas.

2. if we could, via an annotation automatically generate a constructor (kind of like in lombok) taking an object and duplicating the field, eg

public T(T original, boolean shallow) {
  field1 = original.field1;
  // etc... + recursively copy if !shallow && is an object that has the said annotation
}

we could also add an annotation to filter the fields that should not be copied, the fields that shoudl never be deep copied, etc...

1 would have the advantage of not generating any extra code for each class but is quite resource intensive + does not allow for deep/shallow copies

2 would do it for me, unless there is a glitch that I missed somewhere.

With auto beans I need to write down an interface, etc... which defeats the point for me. I just want to be able to duplicate objects without having to maintain an interface.

Any thoughts/comments are very welcome ;-)

Marco

unread,
Mar 4, 2013, 2:08:33 PM3/4/13
to google-we...@googlegroups.com
I have the same issue. 

I found this:
but it seems a little bit strange?

One remark: AutoBean.clone() method is deprecated in the meantime.

Is there no other solution?
Reply all
Reply to author
Forward
0 new messages