[jdom-interest] TODO clone [eg]

Joseph Bowbeer jozart at csi.com
Sat Apr 14 04:25:21 PDT 2001


Things TODO related to Cloneable.

1. CloneNotSupportedException. Our clone implementations have the following
form:

    public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException ex) {
            // Can't happen
        }
    }

I suggest that clone *not* catch CloneNotSupportedException -- except in
final classes.  Instead, add the exception to clone's declaration, as
follows:

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

This gives the subclasses final say about whether they are cloneable or not,
and it forces the programmer to deal with the possibility that a subclass
may not be cloneable.

If we don't add the exception to the declaration, we should at least throw
an InternalError as a sanity check:

    public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException ex) {
            throw new InternalError();
        }
    }


2. As was mentioned in the message about subclassing, clone should not
invoke any overridable methods on the cloned object.


--
Joe Bowbeer











More information about the jdom-interest mailing list