Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,876 people online right now. Registration is fast and FREE... Join Now!




For your amusement: Using Reflection to do a shallow clone

 
Reply to this topicStart new topic

For your amusement: Using Reflection to do a shallow clone

Programmist
17 Sep, 2007 - 11:17 AM
Post #1

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Not sure where to put this, but thought someone might find it useful.
http://blog.null-device.com/?p=26

Been using reflection a lot more lately. It's damned useful when you're getting beans with an unknown number of fields.

This post has been edited by Programmist: 20 Dec, 2007 - 02:38 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
17 Sep, 2007 - 12:49 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Nice snippet there Programmist. Thanks for pointing it out! smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
17 Sep, 2007 - 01:14 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
that site came in very handy. Thanks.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
17 Sep, 2007 - 01:19 PM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(NickDMax @ 17 Sep, 2007 - 02:14 PM) *

that site came in very handy. Thanks.

It's my blog and you're welcome. smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
17 Sep, 2007 - 01:22 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
lol -- awsome.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
17 Sep, 2007 - 08:58 PM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Actually I think the clone method inherited from the Object class already does this (copies all fileds), so it is a bit NIH, however in the inheritance tree all classes must override the clone method - so if it is not done, or you don't have access to the given class, it might work. (See the API for the Object.clone() method)
On the other hand, if the set method does some conversion on the values it receives(and it is not written to the field as it is), than probably the clone won't be a real clone.
Anyway, apart from this disclaimer it can be a useful utility if used correctly.


User is offlineProfile CardPM
+Quote Post

Programmist
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
18 Sep, 2007 - 03:15 AM
Post #7

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(1lacca @ 17 Sep, 2007 - 09:58 PM) *

Actually I think the clone method inherited from the Object class already does this (copies all fileds), so it is a bit NIH, however in the inheritance tree all classes must override the clone method - so if it is not done, or you don't have access to the given class, it might work. (See the API for the Object.clone() method)
On the other hand, if the set method does some conversion on the values it receives(and it is not written to the field as it is), than probably the clone won't be a real clone.
Anyway, apart from this disclaimer it can be a useful utility if used correctly.

Yes, Object.clone() is a natively implemented shallow copy, but, as you said, all classes in a hierarchy must implement clone (this is a soft requirement). My method removes this requirement by recursing through all superclasses of the class passed in. But the main focus of the article was using Reflection to automate tasks like cloning. I suppose I could have used a different task, but I thought this demonstrated it well in the very limited time that I had. In fact, in the article I suggest that the reader "fidget" with the code to create a deep copy version of clone (Something witch Object.clone() does not do). I also alluded that I might blog about how to do that later, time permitting. We'll see. As far as I know, Field.set does not do any conversion that would cause a problem with clone equality. But thanks for your input.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
18 Sep, 2007 - 03:35 AM
Post #8

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Programmist! Mea maxima culpla, I've missed that you used the Field.set method, I thought it was a set function, you are absolutely right, no conversion takes place there (I guess I won't reply to programming threads right before going to bed, and I admit that javabeans are echoing in my mind even while I dream ohmy.gif ).

Btw for deepcopying I think the best (and most elegant) method is to serialize the class into a byteoutputstream and deserialize it from there, however I would really like to see how would you do it with reflection, because it might proove an interesting experiment.



User is offlineProfile CardPM
+Quote Post

Programmist
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
18 Sep, 2007 - 07:52 AM
Post #9

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(1lacca @ 18 Sep, 2007 - 04:35 AM) *

Programmist! Mea maxima culpla, I've missed that you used the Field.set method, I thought it was a set function, you are absolutely right, no conversion takes place there (I guess I won't reply to programming threads right before going to bed, and I admit that javabeans are echoing in my mind even while I dream ohmy.gif ).

Don't sweat it. You made me double-check my code, which a good thing. smile.gif

QUOTE(1lacca @ 18 Sep, 2007 - 04:35 AM) *

Btw for deepcopying I think the best (and most elegant) method is to serialize the class into a byteoutputstream and deserialize it from there, however I would really like to see how would you do it with reflection, because it might proove an interesting experiment.

Cool. I read an article about this a few months ago and I've been meaning to try it out. The guy who wrote it also makes some speed optimizations, which is pretty nice. If I get a chance, I will look at completing my blog post with a deep copy version of my clone() method. But right now there is a lot on my plate (getting married in a few days, project release to test, etc). smile.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: For Your Amusement: Using Reflection To Do A Shallow Clone
18 Sep, 2007 - 11:06 PM
Post #10

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Congrats man!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month