commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <>
Subject Re: [beanutils] Why the shallow clone?
Date Sat, 24 May 2003 08:49:24 GMT
hi Christian

changing cloneBean to work as a deep clone rather than a shallow clone 
would be a major change in the symantics of the method and would therefore 
break backwards compatibility. therefore, this change is unlikely to be 
committed. what i have done is committed a change to the javadocs which 
emphasis that the cloneBean method creates a shallow clone.

what would be more acceptable is another method deepCloneBean (perhaps) 
that produced a beanutils-style deep clone (rather than the shallow clone 
produced by cloneBean). this could (and should) probably be done without 
having to modify the symantics of exists methods.

(of course, i can't speak for other committers) but this change isn't a 
priority for me. so, realistically (given the outstanding backlog of 
beanutils work) it's unlikely to happen by itself. if you really want to 
see this feature in beanutils, then you might like to contribute a patch. 
for more details see and otherwise, you could add 
this as an enhancement to bugzilla and hope that someone will create a 
patch for you.

- robert

On Thursday, May 22, 2003, at 07:52 PM, Christian (SerpentMage) wrote:

> At 17:27 22/05/2003 +0100, you wrote:
>> On Thursday, May 22, 2003, at 11:02 AM, Christian (SerpentMage) wrote:
>>> I have been playing around with the clone from BeanUtils.clone and 
>>> noticed that the clone is a shallow clone only.
>> yes, BeanUtils.clone is a shallow clone.
> Aha...
>> what cloneBean does is to create a new instance and then use 
>> PropertyUtils.
>> copyProperties to set the properties on the new instances to the values 
>> got from the original properties. this will produce a shallow clone.
>> not sure. maybe you are and maybe you aren't :)
>> cloneBean produces a shallow clone of the exposed properties. so it's a 
>> bean-centric cloning (rather than data centric). what did you want 
>> cloneBean to do?
> Well here is where I am thinking if this is an ideal solution.  Lets say 
> that I have a data object.  And within the data object I reference 
> another object.  A clone of the object would expect a deep clone.  
> Granted you then have to implement the clone method.  And here is the 
> problem.
> If I do....
> obj = old.clone();
> I can in the clone implementation do a deep clone.
> obj = BeanUtils.clone( old);
> But here I cannot implement a deep clone, nor define it.  What I would 
> have to do is implement a method to go through the objects to do a deep 
> clone.  If I have to do that I might as well implement the clone method 
> myself and call it a day.
> So here is what I am proposing...
>  Line 264 ----
>             PropertyDescriptor origDescriptors[] =
>                 getPropertyDescriptors(orig);
>             for (int i = 0; i < origDescriptors.length; i++) {
>                 String name = origDescriptors[i].getName();
>                 if (isReadable(orig, name)) {
>                     if (dest instanceof DynaBean) {
>                         if (isWriteable(dest, name)) {
>                             Object value = getSimpleProperty(orig, name);
>                             ((DynaBean) dest).set(name, value);
>                         }
>                     } else {
>                         if (isWriteable(dest, name)) {
>                             Object value = getSimpleProperty(orig, name);
>                             // Start add
>                             if( deepClone == true && isObjectType
> ( origDescriptors[i].propertyType) == true) {
>                                 value = BeanUtils.clone( value);
>                             }
>                             // End add
>                             setSimpleProperty(dest, name, value);
>                         }
>                     }
>                 }
>             }
> I added a reference to a proposed function isObjectType, which checks to 
> see if the property type is a simple property type (int, long, String, 
> Integer) or an object type.  If it is an object type then a clone should 
> be executed.  Of course ideally a flag should exist so that then end 
> programmer could decide if a deep clone should occur or not.  (BTW this 
> is not complete implementation change, just a suggestion to start the 
> discussion).
> Comments?
> Christian
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

View raw message