Deep Copying, Shallow Copying and Reflection in Java

 

Deep Copying, Shallow Copying and Reflection in Java Part 2

 

We will now test this utilities ability to clone objects. We will create simple classes with objects composed in them. This way we will demonstrate shallow copying and deep copying.

Now we test this utility using two classes Cloner.java and Cloner2.java.

The first class Cloner.java is as follows:

public class Cloner implements Cloneable{

public NestedObject nestedObject;

public static void main(String… arhs){

Cloner clonerFirstObj = new Cloner();

try {

clonerFirstObj.setNestedObject(new NestedObject());

Cloner clonerSecondObj = (Cloner) clonerFirstObj.clone();

clonerFirstObj.nestedObject.setIntMember(250);

clonerFirstObj.nestedObject.setDoubleMember(200.400);

System.out.println("Second Object before resetting firstobj value = "+clonerSecondObj.nestedObject.getIntMember()+ "doubleMember = "+clonerSecondObj.nestedObject.doubleMember);

CloneUtility cu = new CloneUtility<Cloner>();

Cloner clonerThirdObj = (Cloner) cu.cloneObject(clonerFirstObj);

clonerFirstObj.nestedObject.setIntMember(300);

clonerFirstObj.nestedObject.setDoubleMember(400.400);

System.out.println("Second Object after resetting firstobj value = "+clonerSecondObj.nestedObject.getIntMember()+ "doubleMember = "+clonerSecondObj.nestedObject.doubleMember);

System.out.println("Third Object after resetting firstobj value = "+clonerThirdObj.nestedObject.getIntMember()+ "doubleMember = "+clonerThirdObj.nestedObject.doubleMember);

} catch (CloneNotSupportedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void setNestedObject(NestedObject nestedObject) {

this.nestedObject = nestedObject;

}

public NestedObject getNestedObject() {

return nestedObject;

}

}

class NestedObject{

public int intMember = 100;

public double doubleMember = 100.200;

//ToDo more

public void setIntMember(int intMember) {

this.intMember = intMember;

}

public int getIntMember() {

return intMember;

}

public void setDoubleMember(Double doubleMember) {

this.doubleMember = doubleMember;

}

public Double getDoubleMember() {

return doubleMember;

}

}

Executing Cloner.java gives the following output:

Second Object before resetting firstobj value = 250doubleMember = 200.4

Second Object after resetting firstobj value = 300doubleMember = 400.4

Third Object after resetting firstobj value = 250doubleMember = 200.4

This shows the the SecondObject using Java’s Object.clone() method has objects that are referenced by the FirstObject hence shallow cloning.

The ThirdObject has deep copying because the objects it has are not referenced by the FirstObject

Now Cloner2.java has objects at a deep level and will give a nice illustration of our tool:

public class Cloner2 implements Cloneable{

public SecondNestedObject nestedObject;

public static void main(String… arhs){

Cloner2 clonerFirstObj = new Cloner2();

try {

clonerFirstObj.setSecondNestedObject(new SecondNestedObject());

clonerFirstObj.nestedObject.setNestedNestedObject(new SecondNestedNestedObject());

Cloner2 clonerSecondObj = (Cloner2) clonerFirstObj.clone();

clonerFirstObj.nestedObject.setIntMember(250);

clonerFirstObj.nestedObject.nestedNestedObject.setIntMember(450);

System.out.println("Second Object before resetting firstobj value = "+clonerSecondObj.nestedObject.getIntMember()+ " nestedMember = "+clonerSecondObj.nestedObject.nestedNestedObject.getIntMember());

CloneUtility cu = new CloneUtility<Cloner>();

Cloner2 clonerThirdObj = (Cloner2) cu.cloneObject(clonerFirstObj);

clonerFirstObj.nestedObject.setIntMember(300);

clonerFirstObj.nestedObject.nestedNestedObject.setIntMember(500);

System.out.println("Second Object after resetting firstobj value = "+clonerSecondObj.nestedObject.getIntMember()+ " nestedMember = "+clonerSecondObj.nestedObject.nestedNestedObject.getIntMember());

System.out.println("Third Object after resetting firstobj value = "+clonerThirdObj.nestedObject.getIntMember()+ " nestedMember = "+clonerThirdObj.nestedObject.nestedNestedObject.getIntMember());

} catch (CloneNotSupportedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void setSecondNestedObject( SecondNestedObject nestedObject) {

this.nestedObject = nestedObject;

}

public SecondNestedObject getSecondNestedObject() {

return nestedObject;

}

}

class SecondNestedObject{

public SecondNestedNestedObject nestedNestedObject;

public int intMember = 100;

public void setIntMember(int intMember) {

this.intMember = intMember;

}

public int getIntMember() {

return intMember;

}

public void setNestedNestedObject(SecondNestedNestedObject nestedNestedObject) {

this.nestedNestedObject = nestedNestedObject;

}

public SecondNestedNestedObject getNestedNestedObject() {

return nestedNestedObject;

}

}

class SecondNestedNestedObject{

public int intMember = 100;

public void setIntMember(int intMember) {

this.intMember = intMember;

}

public int getIntMember() {

return intMember;

}

}

Executing Cloner2.java gives the following output:

Second Object before resetting firstobj value = 250 nestedMember = 450

Second Object after resetting firstobj value = 300 nestedMember = 500

Third Object after resetting firstobj value = 250 nestedMember = 450

Please note I used public variables to short the code and illustrate the concept but this can easily be modified to accommodate private members by using a few more reflective methods. Whew! there we have it a utility that can perform deepcopying. Of course to make it ful fledged one would need to include classes like String and make alot of additional changes but at least its much more then a skeleton piece of code!

Advertisements

About gjcbell

I'm a software designer and integration specialist in Cape Town, South Africa. I work for SPF at SGH. I develop applications in Java, Cplusplus and Python and JavaScript. I design websites and web applications.
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s