commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Emmanuel Bourg (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-433) clone() method for ObjectUtils
Date Sun, 04 May 2008 17:00:55 GMT

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

Emmanuel Bourg commented on LANG-433:
-------------------------------------

I tend to prefer an unchecked exception for such an utility method, but I don't mind if it
throws a CloneNotSupportedException. (and that's Oliver's method in the first place, I just
copied it from the Commons Configuration code :) )

> 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
>
>         Attachments: lang-433.patch
>
>
> 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.


Mime
View raw message