Friday, July 12, 2013

Re: how to clone shared object in gwt

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.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@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.
 
 

No comments:

Post a Comment