Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Wanted: generic deep copy for clone()

Reply
Thread Tools

Wanted: generic deep copy for clone()

 
 
Tim Tyler
Guest
Posts: n/a
 
      07-08-2003
Object.clone() does a shallow copy.

Nine times out of ten I want a deep copy.

I'm fed up with writing my own deep copy code for each class.

What I want is never to have to write deep copy clone methods again.

Has anyone got some generic Object deep copy code - that will deep
copy an arbitrary object - using reflection?

I know that some objects might in principle require bizzare
constructor or factory calls to copy.

The hoped-for deep copy code is welcome to expect primitives, objects
with clone methods - or collections or arrays of the above - and
is welcome to throw some CloneChallengedException if it finds things
are beyond its scope.

When you're done with that I'll take another method that calcutates
hash codes - by a combination of feeding leaf primitives through
a CRC/Cr# - and calling object hash code methods recursively, and
#ing the results together
--
__________
|im |yler http://timtyler.org/ http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      07-08-2003
Tim Tyler wrote:

> Has anyone got some generic Object deep copy code - that will deep
> copy an arbitrary object - using reflection?


I have done a simple implementation of this.
The code assumes that fields to copy have
appropriate get/set methods and find and
call those using inspection.

I cannot give you the code as it is commercial.

 
Reply With Quote
 
 
 
 
Tim Tyler
Guest
Posts: n/a
 
      07-08-2003
Jacob <(E-Mail Removed)> wrote:
: Tim Tyler wrote:

:> Has anyone got some generic Object deep copy code - that will deep
:> copy an arbitrary object - using reflection?

: I have done a simple implementation of this.

: The code assumes that fields to copy have
: appropriate get/set methods and find and
: call those using inspection.

I guess that's not unreasonable.

Public fields would be nice as well, though

: I cannot give you the code as it is commercial.

OK - anyone else?
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed)
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      07-08-2003
http://www.google.com/search?hl=en&l...=Google+Search

On Tue, 8 Jul 2003 12:43:58 GMT, Tim Tyler <(E-Mail Removed)> wrote:

>Jacob <(E-Mail Removed)> wrote:
>: Tim Tyler wrote:
>
>:> Has anyone got some generic Object deep copy code - that will deep
>:> copy an arbitrary object - using reflection?
>
>: I have done a simple implementation of this.
>
>: The code assumes that fields to copy have
>: appropriate get/set methods and find and
>: call those using inspection.
>
>I guess that's not unreasonable.
>
>Public fields would be nice as well, though
>
>: I cannot give you the code as it is commercial.
>
>OK - anyone else?



~ If emailing, please use: Steve_A_Haigh
~ @
~ hotmail.com
~
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-08-2003
On Tue, 8 Jul 2003 07:37:00 GMT, Tim Tyler <(E-Mail Removed)> wrote or
quoted :

>Has anyone got some generic Object deep copy code - that will deep
>copy an arbitrary object - using reflection?


What might also work is something that generates code you compile.
That would be far faster than using reflection every time you have to
clone.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Harald Hein
Guest
Posts: n/a
 
      07-08-2003
"Tim Tyler" wrote:

> Object.clone() does a shallow copy.


Not exactly. The default behavior of Object.clone() is a shallow
copy. But what exactly happens in the clone() method of a subclass
entirely depends on the implementation in the subclasses. Some people
choose to implement deep copying in clone(), others don't.

The problem with clone() is that its semantics is "defined" as

Quote from the API spec:

The precise meaning of "copy" may depend on the class of the
object. The general intent is ... but these are not
absolute requirements. While it is typically the case that:
..., this is not an absolute requirement.

In other words, nothing, absolutely nothing, is specified.

HH
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      07-08-2003
Harald Hein <(E-Mail Removed)> wrote:
: "Tim Tyler" wrote:

:> Object.clone() does a shallow copy.

: Not exactly. The default behavior of Object.clone() is a shallow
: copy. But what exactly happens in the clone() method of a subclass
: entirely depends on the implementation in the subclasses. [...]

When I referred to "Object.clone()" I meant the clone method in
the "Object" class.

[please don't anyone tell me that Object.clone is not static :->]
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed)
 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      07-08-2003
Tim Tyler wrote

> Has anyone got some generic Object deep copy code - that will deep
> copy an arbitrary object - using reflection?


If the objects you want to clone all supports serialization you should be
able to use that mechanism for a deep copy. For example something along the
following lines:

public Object deepCopy(Object objectTree) throws IOException,
ClassNotFoundException {
ByteArrayOutputStream outStore = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOuputStream(outStore);
out.writeObject(objectTree);
ByteArrayInputStream inStore = new
ByteArrayInputStream(outStore.toByteArray());
ObjectInputStream in = new ObjectInputStream(inStore);
return in.readObject();
}



--
Filip Larsen


 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      07-08-2003
Filip Larsen <(E-Mail Removed)> wrote:
: Tim Tyler wrote

:> Has anyone got some generic Object deep copy code - that will deep
:> copy an arbitrary object - using reflection?

: If the objects you want to clone all supports serialization you should be
: able to use that mechanism for a deep copy. For example something along the
: following lines:

: public Object deepCopy(Object objectTree) throws IOException,
: ClassNotFoundException {
: ByteArrayOutputStream outStore = new ByteArrayOutputStream();
: ObjectOutputStream out = new ObjectOuputStream(outStore);
: out.writeObject(objectTree);
: ByteArrayInputStream inStore = new
: ByteArrayInputStream(outStore.toByteArray());
: ObjectInputStream in = new ObjectInputStream(inStore);
: return in.readObject();
: }

That makes me smile.

I feel like it has just move the goalposts, though.

Now to execute my plan I need a means of serialising an object
automatically - using reflection to access the fields...
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed)
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      07-09-2003
Tim Tyler <(E-Mail Removed)> wrote:
: Jacob <(E-Mail Removed)> wrote:

: : The code assumes that fields to copy have
: : appropriate get/set methods and find and
: : call those using inspection.

: I guess that's not unreasonable.

: Public fields would be nice as well, though

In fact all fields should be doable - in principle - I believe.

One problem might be that the reflection involved would mean
that the code would no longer work in certain SecurityManagers
that prohibit reflection operations.

I suspect this would be a bit of an irritation.
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deep Copy smart pointer not requiring virtual copy constructor Nindi73@yahoo.co.uk C++ 11 11-14-2006 02:23 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
How to overload operator= to have both deep copy and shallow copy semantics bluekite2000@gmail.com C++ 1 06-24-2005 05:28 PM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments