commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joerg Schaible (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils
Date Fri, 26 Mar 2010 17:14:27 GMT

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

Joerg Schaible commented on LANG-576:
-------------------------------------

We use this for serives loaded with the service API from META-INF/services. However, some
of these services are context sensitive or have state and should not be running concurrently.
All thouse will implement Cloneable while the others don't care and can run concurrently.
With cloneIfPossible is is no convenient simply to pass the original service instance loaded
from the SPI and the caller has not to take care of any returned null values or implemented
interfaces.

> Add methods for cloneables to ObjectUtils
> -----------------------------------------
>
>                 Key: LANG-576
>                 URL: https://issues.apache.org/jira/browse/LANG-576
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>            Reporter: Joerg Schaible
>            Assignee: Joerg Schaible
>            Priority: Minor
>             Fix For: 3.0
>
>         Attachments: Cloneable.diff, CloneableTest.diff
>
>
> Object.clone is declared protected, which makes it impossible to write code like:
> {code:java}
> if (obj instanceof Cloneable) {
>   Object clone = obj.clone();
>   ...
> }
> {code}
> Following two methods will help in such a situation:
> {code:java}
>     /**
>      * Clone an object.
>      * 
>      * @param <T> the type of the object
>      * @param o the object to clone
>      * @return the clone if the object implements {@link Cloneable} otherwise <code>null</code>
>      * @throws CloneFailedException if the object is cloneable and the clone operation
fails
>      * @since 3.0
>      */
>     public static <T> T clone(final T o) {
>         if (o instanceof Cloneable) {
>             try {
>                 final Method clone = o.getClass().getMethod("clone", (Class[])null);
>                 @SuppressWarnings("unchecked")
>                 final T result = (T)clone.invoke(o, (Object[])null);
>                 return result;
>             } catch (final NoSuchMethodException e) {
>                 throw new CloneFailedException("Cloneable type has no clone method",
e);
>             } catch (final IllegalAccessException e) {
>                 throw new CloneFailedException("Cannot clone Cloneable type", e);
>             } catch (final InvocationTargetException e) {
>                 throw new CloneFailedException("Exception cloning Cloneable type", e.getCause());
>             }
>         }
>         return null;
>     }
>     /**
>      * Clone an object if possible.
>      * 
>      * @param <T> the type of the object
>      * @param o the object to clone
>      * @return the clone if the object implements {@link Cloneable} otherwise the object
itself
>      * @throws CloneFailedException if the object is cloneable and the clone operation
fails
>      * @since 3.0
>      */
>     public static <T> T cloneIfPossible(final T o) {
>         final T clone = clone(o);
>         return clone == null ? o : clone;
>     }
> {code}
> Comments? Note, that the code currently introduces also a new CloneFailedException. Use
another existing one?
> Unit tests will be provided also.

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