How would you clone this class?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • odwrotnie
    New Member
    • Sep 2008
    • 9

    How would you clone this class?!

    Code:
    class Asdf<T extends Cloneable> implements Cloneable {
       public T t;
       protected Object clone() throws CloneNotSupportedException {
          ...
          t.clone(); // The method is not visible
          ...
       }
    }
    Thanks,
    Etam.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You can't clone that object of generic class T. The Cloneable interface does not
    guarantee that you can clone an object that implements that interface (also read
    the API documentation for that interface).

    You can either try to use reflection and find the clone() method (very ugly) or
    create your own interface:

    Code:
    public interface MyCloneable extends Cloneable {
       public Object clone();
    }
    Make your class T implement this interface.

    kind regards,

    Jos

    Comment

    Working...