You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Oliver Heger (JIRA)" <ji...@apache.org> on 2008/04/29 21:46:55 UTC

[jira] Commented: (LANG-433) clone() method for ObjectUtils

    [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12593092#action_12593092 ] 

Oliver Heger commented on LANG-433:
-----------------------------------

There was already a discussion about adding support for cloning to [lang]. I found the following reference:
http://www.opensubscriber.com/message/commons-dev@jakarta.apache.org/1479423.html


> clone() method for ObjectUtils
> ------------------------------
>
>                 Key: LANG-433
>                 URL: https://issues.apache.org/jira/browse/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:
> {code:java}
> /**
>  * 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)
>     {
>         try
>         {
>             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);
>         }
>     }
>     else
>     {
>         throw new CloneNotSupportedException(obj.getClass().getName() + " does not implement Cloneable");
>     }
> }
> {code}

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