Code review request for 4881419 The type of X[].clone() should be X[]

Ulf Zibis Ulf.Zibis at gmx.de
Tue Sep 7 10:49:39 UTC 2010


  Am 07.09.2010 10:50, schrieb David Holmes:
> Jeroen Frijters said the following on 09/07/10 17:59:
>> David Holmes wrote:
>>> The fact that Object.clone() is implemented via a native call into
>>> the VM is simply an implementation detail.
>>
>> That's not what we're talking about. We're talking about the fact
>> that arrays (appear to) have a *public* clone method. The argument is
>> about the return type[1] of this method: according to the JLS it is
>> X[], but according to the VMSpec it is Object. This difference is
>> fine, but I'm arguing that the JLS fiction should be in the JLS, not
>> in the Object.clone() documentation.
>
> I don't see where the VMSpec says anything about cloning ... but that aisde. I hadn't realized 
> that the JLS states that arrays override Object.clone() rather than inheriting it - it would have 
> been simpler to cover the array and non-array cases in Object.clone().
>
> As it stands there is no actual API doc in which to put this information, so we really have no 
> choice but to put it Object (having it only in the JLS is inadequate). That said I would call it 
> out explicitly in its own paragraph, something like:
>
> "All array types, X[], are considered to implement Cloneable and to override this method to return 
> a new X[] containing the same elements as the original."

+1
This statement, which describes the compile-time behaviour, more belongs to the JLS.

Jeroen, do I understand you right, that you agree with me? :
In javadoc of class Object we should better note for the run-time behaviour. In other words:
As array types clone method is considered to be silently overridden in covariant way, method clone 
of an array object X[] returns a new array of class X[], filled with it's original's content.

Additionally we could note in javadoc of method clone of class Object:
If overriding the clone method, it is recommended to use the covariant return type to avoid the need 
of casting in the calling code. Example:
class MyClass {
     @override
     public MyClass clone() {
         MyClass clone = (MyClass)super.clone();
         clone.doSomeMoreWork();
         return clone;
     }
}

-Ulf





More information about the core-libs-dev mailing list