JDK-5045376 : Add static method System.clone(Cloneable obj)
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-05-11
  • Updated: 2004-05-29
  • Resolved: 2004-05-29
Related Reports
Relates :  
Description

Name: gm110360			Date: 05/11/2004


A DESCRIPTION OF THE REQUEST :
The Cloneable interface doesn't define a public clone() method, which is a problem. For binary compatibility, this method can't be added to the Cloneable interface. However, there is a simple and fairly elegant workaround to this problem.

The clone() method in the Object class is protected. This means that other classes in the java.lang package can call this method. Some class in the java.lang package (e.g. System) should have a method to call an arbitrary object's clone() method, to solve the problem.

JUSTIFICATION :
In many cases it would be useful to call clone() polymorphically for any object that is known to only implement Cloneable, but the exact class of the object is not known at run time. To avoid a complicated and slow reflective method invokation, the new method should be added to the java.lang package.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should be possible to clone any Cloneable object with a single method call without knowing the object's exact class.
ACTUAL -
This is not possible. Either you have to know the class of the object, or invoke the clone() method via reflection.

---------- BEGIN SOURCE ----------
For example, this class would do the job. Alternatively, the clone(Cloneable) static method could be added e.g. to the System class.

package java.lang;

public class Cloner
{
    private Cloner()
    {
    }

    public static Object clone(Cloneable obj)
        throws CloneNotSupportedException
    {
        return obj.clone();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Call the clone() method via reflection.
(Incident Review ID: 264984) 
======================================================================

Comments
EVALUATION I do not believe that we can do this, as there are classes that currently depend on the fact that their clone method cannot be called because it is non-public. ###@###.### 2004-05-28
28-05-2004