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.