CloneNotSupportedException should extends RuntimeException not Exception

Remi Forax forax at univ-mlv.fr
Mon Oct 15 17:08:47 UTC 2012


Hi Jeroen,

On 10/15/2012 01:48 PM, Jeroen Frijters wrote:
> The solution is actually pretty easy. Just deprecate Object.clone() and add a new method to Object:
>
> protected final native Object shallowClone();
>
> It doesn't require the useless Cloneable interface and doesn't conflate shallow and deep cloning.
>
> (This is similar to .NET's Object.MemberwiseClone().)
>
> Note that this means that any non-final class is cloneable, BUT THAT IS ALREADY THE  CASE even though some people want to pretend otherwise.

To avoid collisions it can be a static method of Object or j.u.Objects
that uses generics to be nicely typed.

static native <T> T shallowClone(T object);

and I disagree about Cloneable, while a non-final class is by definition 
mutable, it doesn't mean
that a non final class can be cloned because it may share some states 
that should not be shared,
at least without the consent from the author the class.

The other nice property is that any cloneable class will be publicly 
cloneable.

I also believe that the checkcast generated by javac before calling 
shallowClone can be removed
by the JIT, currently this cast is not removed when you call clone.

>
> Regards,
> Jeroen

regards,
Rémi




More information about the core-libs-dev mailing list