6

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.

Community
  • 1
  • 1
LPD
  • 2,833
  • 2
  • 29
  • 48
  • 1
    Any reason you don't do it the Java Way™ ? (i.e. add a `clone()` method to your class, or if you don't have polymorphism at all add a _copy constructor_: `public Container(Container container) { … }`. – Thomas Broyer Jan 10 '13 at 13:16
  • 1
    Then I will have to write clone() method in every model that I will have. I was trying to get a generic clone function which does the job for me. Actually this is in continuation with this :http://stackoverflow.com/questions/14233574/revert-undo-implementation-in-gwt – LPD Jan 10 '13 at 13:31

2 Answers2

2

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

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
1

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.

Jonathan
  • 705
  • 5
  • 16
  • I'm not sure how I feel about using `AutoBean`s to perform deep copies, but it should do the trick. – Jonathan Jan 11 '13 at 20:47