commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian (SerpentMage)" <mail...@devspace.com>
Subject Re: [beanutils] Why the shallow clone?
Date Thu, 22 May 2003 18:52:50 GMT
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...

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


Mime
View raw message