Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 97877 invoked from network); 8 Feb 2010 23:13:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 8 Feb 2010 23:13:49 -0000 Received: (qmail 78086 invoked by uid 500); 8 Feb 2010 23:13:49 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 78003 invoked by uid 500); 8 Feb 2010 23:13:48 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 77993 invoked by uid 99); 8 Feb 2010 23:13:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Feb 2010 23:13:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Feb 2010 23:13:48 +0000 Received: from brutus.apache.org (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id D4390234C045 for ; Mon, 8 Feb 2010 15:13:27 -0800 (PST) Message-ID: <1409051849.134821265670807854.JavaMail.jira@brutus.apache.org> Date: Mon, 8 Feb 2010 23:13:27 +0000 (UTC) From: "Joerg Schaible (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (LANG-576) Add methods for cloneables to ObjectUtils In-Reply-To: <1529308570.1261583429902.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ 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 the type of the object > * @param o the object to clone > * @return the clone if the object implements {@link Cloneable} otherwise null > * @throws CloneFailedException if the object is cloneable and the clone operation fails > * @since 3.0 > */ > public static 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 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 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.