You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Christian (SerpentMage)" <ma...@devspace.com> on 2003/05/22 12:02:15 UTC

[beanutils]Why the shallow clone?

I have been playing around with the clone from BeanUtils.clone and noticed 
that the clone is a shallow clone only.

Am I doing something wrong here?

If so, I noticed from a previous email that it was implemented that way to 
reflect how Java does a clone.  I can understand that, but then my question 
is how do you hook into the cloning process?  When java does a clone you 
can call a clone while the clone is executing.  However, because beanutils 
is managing the entire process a hook would appear necessary since 
otherwise you need to manually go through all the classes and do a 
BeanUtils.clone.  And that I think would seem to be beside the point since 
the point is to let BeanUtils do everything for you.

Am I missing something here?  Thanks

Christian Gross


Re: [beanutils] Why the shallow clone?

Posted by "Christian (SerpentMage)" <ma...@devspace.com>.
At 09:49 24/05/2003 +0100, you wrote:
>hi Christian
>
>(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 http://jakarta.apache.org/commons/patches.html and 
>http://jakarta.apache.org/site/getinvolved.html. otherwise, you could add 
>this as an enhancement to bugzilla and hope that someone will create a 
>patch for you.

Thanks...  I will consider getting a patch written.  (Need it for my own 
purposes).

Christian


Re: [beanutils] Why the shallow clone?

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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 http://jakarta.apache.org/commons/patches.html and 
http://jakarta.apache.org/site/getinvolved.html. 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...
>
> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


[beanutils] -RowSetDynaClass-->DynaBean Oracle

Posted by rajesh kalluri <ra...@yahoo.com>.
Hi,

I am using RowSetDynaClass to transfer data from my Oracle resultset to  A commons DynaBean, Numeric fields in my DynaBean are java.lang.Integer's, but numeric columns transfer data to a java.math.bigdecimal, what is the best way to transfer data from my BigDecimal to Integer.

I also have some scenarios where i hav to transfer Timestamps to Date types.

Thanks,


---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

Re: [beanutils] Why the shallow clone?

Posted by "Christian (SerpentMage)" <ma...@devspace.com>.
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


Re: [beanutils] Why the shallow clone?

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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.

> Am I doing something wrong here?

dunno.

> If so, I noticed from a previous email that it was implemented that way 
> to reflect how Java does a clone.  I can understand that, but then my 
> question is how do you hook into the cloning process?  When java does a 
> clone you can call a clone while the clone is executing.  However, 
> because beanutils is managing the entire process a hook would appear 
> necessary since otherwise you need to manually go through all the classes 
> and do a BeanUtils.clone.  And that I think would seem to be beside the 
> point since the point is to let BeanUtils do everything for you.

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.

> Am I missing something here?  Thanks

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?

- robert