Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6739113

Fix Cloneable API in a backwards compatible way

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • None
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      The Cloneable API should be fixed, and I believe it can be done in a simple and backwards compatible way. Two things are needed, an interface that extends Cloneable adding a public clone method, and a generic System.clone() method. The System.clone() method would first attempt to use the new interface, and failing that, fall back to using reflection. When using reflection, only public clone() methods would be called, which is the same behavior as before. The Cloneable interface could then be marked as deprecated. See example in test case section.


      JUSTIFICATION :
      This would greatly simplfy the usage of Cloneable, which has always caused problems for Java developers. Library developers have done similar techniques, but since there is no standardized interface, it is not useful when interacting with code outside of the library. For this reason it's important that the JDK address it, and not a library/application developer.

      Also, since this preserves the previous behavior, there are no backwards compatibility issues.


      ---------- BEGIN SOURCE ----------
      // Example

      public interface PublicCloneable extends Cloneable {
          Object clone() throws CloneNotSupportedException;
      }

      // On System
      public static final <T> T clone(T cloneable) throws CloneNotSupportedException {
          if (! (cloneable instanceof Cloneable))
              throw new CloneNotSupportedException();

          if (cloneable instanceof PublicCloneable)
              return (T) ((PublicCloneable)cloneable).clone();
         
           try {
               return (T) cloneable.getClass().getMethod("clone").invoke(cloneable);
           } catch (Exception e) {
               throw new CloneNotSupportedException();
           }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: