Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep clone in GWT

While going through this link How to Deep clone in javascript

I came across a generic clone method (In the accepted answer) . I tried it out running directly in javascript and it runs giving perfect outputs.

I put that code in the native tag and am trying to clone an object in GWT.

My class which am trying to clone is

    private class Container
    {
        Integer i = 5;
    }

and when I try to do that, its just returning me the same object. Could anyone please help? Ask me anything if its not clear. Thanks a ton.

like image 299
LPD Avatar asked Jan 10 '13 12:01

LPD


2 Answers

Jonathan is right: the way (and the only one in GWT) is to use https://code.google.com/p/google-web-toolkit/wiki/AutoBean

This may seam awkward but it works perfectly since many concepts are related to that (EntityProxy in RequestFactory relies also on that mechanism, and it's the future of GWT).

Deep json persistence also works with auto beans.

The only thing you have to do is to create an interface that describes your class (and implement it in your class):

public interface ContainerBean {
  Integer getI();
  void setI(Integer i);
}

Then create your factory interface

interface MyFactory extends AutoBeanFactory {
  // Factory method for a simple AutoBean
  AutoBean<ContainerBean> container();

  // Factory method for a non-simple type or to wrap an existing instance
  AutoBean<ContainerBean> container(ContainerBean toWrap);
}

Now you can wrap your object and clone it (through json since)

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.

https://code.google.com/p/google-web-toolkit/wiki/AutoBean#clone()

therefore use this method instead: https://code.google.com/p/google-web-toolkit/wiki/AutoBean#AutoBeanCodex

like image 70
Zied Hamdi Avatar answered Nov 20 '22 11:11

Zied Hamdi


One way you could possibly achieve this is with AutoBeans.

I think the only trick with this method is that you'll have to use an AutoBeanFactory to create all of your classes.

Then, you could encode your autobean into a Splittable, then use the result to decode into a new autobean instance.

like image 26
Jonathan Avatar answered Nov 20 '22 12:11

Jonathan