how to clone shared object in gwt

1,483 views
Skip to first unread message

Bhumika Thaker

unread,
Jul 12, 2013, 4:43:19 AM7/12/13
to google-we...@googlegroups.com
Hi,

I want to implement audit mechanism for some entity of hibernate. Those entities are in shared package. That mean it will accessible for client and server both package.  
I am doing below steps to achieve audit feature:

->Entity has one variable which hold clone copy of itself 
Just example:

class Data implements Cloneable
{
Data oldData;

public Data getOldDataCopy() {
 return oldDataCopy;
}

public void setOldDataCopy() {
  try {
this.oldDataCopy = (Data) this.clone(); // This will clone this object when I ferxg
} catch (CloneNotSupportedException e) {  
e.printStackTrace();
}
}

}


So while any changes made in object then original copy remained as original stage. 
It throw exception like

[TRACE] [v4workflow] - Finding entry point classes
[ERROR] [v4workflow] - Errors in 'file:/E:/workspace/V4Common_V17/src/com/v4common/shared/beans/workflowengine/Data/Data.java'
[ERROR] [v4workflow] - Line 612: The method clone() is undefined for the type Data
[ERROR] [v4workflow] - Line 613: No source code is available for type java.lang.CloneNotSupportedException; did you forget to inherit a required module?
[ERROR] [v4workflow] - Unable to find type 'com.nextenders.client.WorkFlowEntryPoint'
[ERROR] [v4workflow] - Hint: Previous compiler errors may have made this type unavailable
[ERROR] [v4workflow] - Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[ERROR] [v4workflow] - Failed to load module 'v4workflow' from user agent 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0' at pwd.mp.etenders.in:54479

but As I goggled then came to know that clone does not support with GWT. 
So is there any alternative other then clone? should I have to make copy of each property manually?

Thanks,
Bhumika

Thomas Broyer

unread,
Jul 12, 2013, 5:32:14 AM7/12/13
to google-we...@googlegroups.com
No (some people used hacks in JSNI, but it's rather fragile; search in the issue tracker)
 
should I have to make copy of each property manually?

Yes.

FYI, java/lang/Object#clone() isn't in any released version of GWT, but it's under review: https://gwt-review.googlesource.com/3616 It's not the same clone() as in the JVM though, it always throws CloneNotSupportedException (which means that class will be emulated)

Jens

unread,
Jul 12, 2013, 5:34:07 AM7/12/13
to google-we...@googlegroups.com
You can emulate CloneNotSupportedException yourself to make the GWT compiler happy. The only thing that you do not have in GWT is Object.clone() as its not implemented by GWT. This means you do not get a copy of an instance if you call super.clone() in one of your top level classes. In GWT trunk I think you will get an exception now when you call Object.clone().

In general I would never really use clone() of Java. Instead I define my own interface and use it to replicate clone().

Example:

interface Copyable<T> {
  T copy();
  T deepCopy();
}

class Person implements Copyable<Person> {
  String name;
  Person friend;

  protected Person(Person other, boolean deep) {
    // super(other, deep) if Person would extend HumanBeing or similar.
    name = other.name;
    friend = other.friend;
    if(deep) {
      friend = friend.deepCopy();
    }
  }

  public Person copy() {
    return new Person(this, false);
  }

  public Person deepCopy() {
    return new Person(this, true);
  }
}

-- J.

Bhumika Thaker

unread,
Jul 12, 2013, 6:37:56 AM7/12/13
to google-we...@googlegroups.com
Thanks for reply  @Thomas Broyer and @Jens

@Jens thanks for sharing good example and  technique for making copy of object.

Thanks & Regards,
Bhumika 


--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/8tgvSPOODH0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages