CloneNotSupportedException should extends RuntimeException not Exception
Jeroen Frijters
jeroen at sumatra.nl
Mon Oct 15 11:48:19 UTC 2012
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.
Regards,
Jeroen
________________________________________
From: core-libs-dev-bounces at openjdk.java.net [core-libs-dev-bounces at openjdk.java.net] on behalf of Remi Forax [forax at univ-mlv.fr]
Sent: Monday, October 15, 2012 9:27 AM
To: David Holmes
Cc: core-libs-dev at openjdk.java.net
Subject: Re: CloneNotSupportedException should extends RuntimeException not Exception
On 10/15/2012 12:34 AM, David Holmes wrote:
> Remi,
>
> This ship has sailed you can't recall it. CloneNotSupportedException
> is a checked exception and needs to remain so for source and binary
> compatibility.
yes, it burns into flame :)
too bad, I'm pretty sure that no-one (until now) use a catch that mix
runtime exception
and clone not supported exception, people tend to catch exceptions
always in the same way,
but I have no proof.
>
> David
Rémi
>
> On 15/10/2012 2:19 AM, Remi Forax wrote:
>> Hi everybody,
>> CloneNotSupportedException is thrown when a developer calls
>> Object.clone() and forget to mark the current object as Cloneable.
>> Because it's clearly a developer error, CloneNotSupportedException
>> should be a subtype of RuntimeException and not of Exception.
>>
>> I believe this change is backward compatible because RuntimeException is
>> a subclass of Exception,
>> so I propose to first change CloneNotSupportedException to extends
>> RuntimeException.
>>
>> diff -r ff641c5b329b
>> src/share/classes/java/lang/CloneNotSupportedException.java
>> --- a/src/share/classes/java/lang/CloneNotSupportedException.java Sat
>> Oct 13 10:15:57 2012 +0100
>> +++ b/src/share/classes/java/lang/CloneNotSupportedException.java Sun
>> Oct 14 18:16:35 2012 +0200
>> @@ -42,7 +42,7 @@
>> */
>>
>> public
>> -class CloneNotSupportedException extends Exception {
>> +class CloneNotSupportedException extends RuntimeException {
>> private static final long serialVersionUID = 5195511250079656443L;
>>
>> /**
>>
>>
>> And then to clean up the whole JDK (in several patches) to remove all
>> the unnecessary
>> try/catch like the one in by example ArrayList.clone()
>>
>> public Object clone() {
>> try {
>> ArrayList<?> v = (ArrayList<?>) super.clone();
>> v.elementData = Arrays.copyOf(elementData, size);
>> v.modCount = 0;
>> return v;
>> } catch (CloneNotSupportedException e) {
>> // this shouldn't happen, since we are Cloneable
>> throw new InternalError(e);
>> }
>> }
>>
>> will become
>>
>> public Object clone() {
>> ArrayList<?> v = (ArrayList<?>) super.clone();
>> v.elementData = Arrays.copyOf(elementData, size);
>> v.modCount = 0;
>> return v;
>> }
>>
>>
>> cheers,
>> Rémi
>>
More information about the core-libs-dev
mailing list