commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Emmanuel Bourg (JIRA)" <>
Subject [jira] Created: (LANG-433) clone() method for ObjectUtils
Date Tue, 29 Apr 2008 16:45:56 GMT
clone() method for ObjectUtils

                 Key: LANG-433
             Project: Commons Lang
          Issue Type: Improvement
    Affects Versions: 2.4
            Reporter: Emmanuel Bourg
             Fix For: 3.0

We have a simple clone() method in Commons Configuration that could be added to ObjectUtils.
It calls the public clone() method of a cloneable object, or throws a CloneNotSupportedException.

Here is the code:

 * An internally used helper method for cloning objects. This implementation
 * is not very sophisticated nor efficient. Maybe it can be replaced by an
 * implementation from Commons Lang later. The method checks whether the
 * passed in object implements the <code>Cloneable</code> interface. If
 * this is the case, the <code>clone()</code> method is invoked by
 * reflection. Errors that occur during the cloning process are re-thrown as
 * runtime exceptions.
 * @param obj the object to be cloned
 * @return the cloned object
 * @throws CloneNotSupportedException if the object cannot be cloned
public static Object clone(Object obj) throws CloneNotSupportedException
    if (obj instanceof Cloneable)
            Method m = obj.getClass().getMethod(METHOD_CLONE);
            return m.invoke(obj);
        catch (NoSuchMethodException nmex)
            throw new CloneNotSupportedException("No clone() method found for class" + obj.getClass().getName());
        catch (IllegalAccessException iaex)
            throw new ConfigurationRuntimeException(iaex);
        catch (InvocationTargetException itex)
            throw new ConfigurationRuntimeException(itex);
        throw new CloneNotSupportedException(obj.getClass().getName() + " does not implement

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message