padawan
padawan

Reputation: 1315

How to Copy Everything Using Serializable in Java

I have lots of classes and circular references among them (e.g. an in class A, I have a set of objects from class B and class B has an attribute as an object of class A etc.)

When I try to copy everything from an object to another and modify the base object, because of the lists, I lose information.

Is there a way to copy every single bit from an object to another?

Edit:

Consider the following classes

class Book
{
    private Set<Page> pages;
    String title;
}

class Page
{
    private Set<String> lines;
    private int number;
    private int numberOfLines;
    private Book book;
}

should I use Cloneable for both or using just for Book is sufficient?

Could you please help me to write a copy constructor (or a method to copy) for Book class?

Upvotes: 0

Views: 892

Answers (3)

Giovanni Botta
Giovanni Botta

Reputation: 9816

Can't you just create a "copy constructor" as you said? It might be more verbose but it's explicit:

class A{
  public A(A other){ 
    bs = new ArrayList<>();
    for(B b : other.bs) bs.add(new B(b));
    /* copy other members */
  }
  private List<B> bs;
}

class B{
  public B(B other){ /* copy all members */ }
}

The advantage is that the intent is explicit. The disadvantage is a lot of boiler plate. As per the alternatives, cloning is not recommended, and serialization incurs quite a performance penalty.

This is a long shot, but you could consider using a tool like ANTLR4 to write a tool to generate the "copy constructors" for you.

Upvotes: 0

Guillaume Darmont
Guillaume Darmont

Reputation: 5018

If your object graph contains only Serializable classes, a common way to do a deep clone is to serialize the initial object and deserialize the result.

You can do this using ObjectOutputStream and ObjectInputStream classes.

Upvotes: 3

Angular University
Angular University

Reputation: 43087

There is no standard out of the box mechanism. The usual way is to implement interface Cloneable or use apache commons utilities - have a look at this answer, or even simply create your own copy constructor manually.

Upvotes: 2

Related Questions