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

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

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.


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

Posted by "Emmanuel Bourg (JIRA)" <ji...@apache.org>.
    [ 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.


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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-433.
------------------------------

    Resolution: Duplicate

Closing as a duplicate of LANG-307.

> 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.


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

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ 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.


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

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger updated LANG-433:
------------------------------

    Attachment: lang-433.patch

This is a patch for adding the clone() method to ObjectUtils including unit tests.

Currently CloneNotSupportedException is thrown for each occurring error. Personally I would prefer an unchecked exception. What do others think?

I recently needed a more sophisticated clone functionality, i.e. something that goes in the direction of LANG-307. Would there still be interested if I provided a patch?

> 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.


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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12594048#action_12594048 ] 

Henri Yandell commented on LANG-433:
------------------------------------

Sounds good. There hasn't been much demand for LANG-307, so I think we should close that as WONTFIX and apply Emmanuel's method above. Unit test? :)

> 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.


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

Posted by "Paul Benedict (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12604965#action_12604965 ] 

Paul Benedict commented on LANG-433:
------------------------------------

I prefer this ticket be considered part of LANG-307. Because clone() is part of Java's standard object methods, it should belong in ObjectUtils too, if not anymore than to delegate to a known CloneUtils method. No checked exceptions please and allow null-safety.

> 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.


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

Posted by "Emmanuel Bourg (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12593105#action_12593105 ] 

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

I guess it's related to LANG-307. The CloneUtils proposal was more ambitious, with fallbacks to use copy constructor and serialization. For Commons Configuration we just need a simple "one liner" that calls the public clone() method of Cloneable objects.

> 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.