You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Joerg Schaible (JIRA)" <ji...@apache.org> on 2009/12/23 16:50:29 UTC

[jira] Created: (LANG-576) Add methods for cloneables to ObjectUtils

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


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
     */
    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
     */
    public static <T> T cloneIfPossible(final T o) {
        final T clone = clone(o);
        return clone == null ? o : clone;
    }
{code}

Comments?

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.


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-576?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12836558#action_12836558 ] 

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

It's a convenience method when working with immutable objects in concurrency situations that do not have to be cloned at all.

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-576?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831185#action_12831185 ] 

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

Looking at the comments of LANG-307 I am with Paul. This clone functionality is simple and basic enough (relies completely on the JDK definition of the Cloenable) to be part of ObjectUtils. The clone functionality in CloneUtils tries to do a lot smarter things. Therefore I'd like to commit this for 3.0 if nobody objects.
When we add CloneUtils later, we can either delegate from ObjectUtils to CloneUtils.cloneByReflection or vice versa.

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment: Cloneable.diff

> 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
>
>
> 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
>      */
>     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
>      */
>     public static <T> T cloneIfPossible(final T o) {
>         final T clone = clone(o);
>         return clone == null ? o : clone;
>     }
> {code}
> Comments?
> 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.


[jira] Reopened: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell reopened LANG-576:
--------------------------------


Not convinced by cloneIfPossible, feels like a very undefined state to get into "I may have a clone". Especially given it is the same as 'instanceof Cloneable'. 

I'd like to remove it - thoughts?

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell updated LANG-576:
-------------------------------

    Fix Version/s:     (was: 3.0)
                   3.1

> 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.1
>
>         Attachments: Cloneable.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.


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment:     (was: Cloneable.diff)

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment: Cloneable.diff

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment: CloneableTest.diff

Unit tests for the new methods.

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

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

  was:
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
     */
    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
     */
    public static <T> T cloneIfPossible(final T o) {
        final T clone = clone(o);
        return clone == null ? o : clone;
    }
{code}

Comments?

Unit tests will be provided also.


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


[jira] Closed: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell closed LANG-576.
------------------------------

    Resolution: Fixed

Thanks Jörg. 

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell commented on LANG-576:
------------------------------------

Thanks. I think it's fair to keep the method, but some more javadoc to explain the intended use case would be good. 

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-576?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12850692#action_12850692 ] 

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

Javadoc improved

{noformat}
commit -m "Improve javadoc" lang/src/main/java/org/apache/commons/lang3/ObjectUtils.java
    Sending        lang/src/main/java/org/apache/commons/lang3/ObjectUtils.java
    Transmitting file data ...
    Committed revision 928453.
{noformat}

I'm no native speaker, feel free to correct ;-)

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment: Cloneable.diff

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Attachment:     (was: Cloneable.diff)

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-576?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12794483#action_12794483 ] 

Oliver Heger commented on LANG-576:
-----------------------------------

I would like to see support for cloning in lang, too. LANG-307 suggests a {{CloneUtils}} class. {{CloneUtils}} also provides a method that tries to invoke {{clone()}} through reflection. So I think LANG-307 is a super set of this proposal. Maybe the implementations in these proposals can be merged?

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell updated LANG-576:
-------------------------------


No backwards compat issues, so moving to 3.1. If resolved before 3.0 is released please assign to 3.0.

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


[jira] Updated: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible updated LANG-576:
--------------------------------

    Fix Version/s:     (was: 3.1)
                   3.0

Include into 3.0

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


[jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils

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

Henri Yandell commented on LANG-576:
------------------------------------

How so? Do you have an example?

Basically I'm wondering when I would clone something, yet not care if it was actually cloned or not.

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


[jira] Resolved: (LANG-576) Add methods for cloneables to ObjectUtils

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

Joerg Schaible resolved LANG-576.
---------------------------------

    Resolution: Fixed

$ svn ci -m "Add methods for Cloneables to ObjectUtils (LANG-576)."
Sending        src/main/java/org/apache/commons/lang3/ObjectUtils.java
Adding         src/main/java/org/apache/commons/lang3/exception/CloneFailedException.java
Sending        src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Transmitting file data ...
Committed revision 908745.


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