Java Class Clone

Posted on November 18, 2012. Filed under: java/j2EE |


clone demo #mkdir cloneDemo
clone demo #cd cloneDemo/
clone demo #vi Driver.java
clone demo #vi PrototypeA.java
clone demo #vi PrototypeAA.java
clone demo #vi PrototypeAAA.java
clone demo #cat Driver.java
public class Driver {
 public PrototypeA prototypeA = null;
 public static void main(String[] args) throws CloneNotSupportedException {
 Driver driver = new Driver();
 driver.prototypeA = new PrototypeA();
 PrototypeA prototypeAClone = (PrototypeA)driver.prototypeA.clone();
 //change original object name
 driver.prototypeA.name = new String("newA");
 driver.prototypeA.prototypeAA.name = new String("newAA");
 driver.prototypeA.prototypeAA.prototypeAAA.name = new String("newAAA");
 //printout the cloned object names
 System.out.println("prototypeAClone.name: "+prototypeAClone.name);
 System.out.println("prototypeAClone.prototypeAA.name: "+prototypeAClone.prototypeAA.name);
 System.out.println("prototypeAClone.prototypeAA.prototypeAAA.name: "+prototypeAClone.prototypeAA.prototypeAAA.name);
 }
}

clone demo #
clone demo #cat PrototypeA.java
public class PrototypeA implements Cloneable{
 public String name = null;
 public PrototypeAA prototypeAA = null;
 public PrototypeA() throws CloneNotSupportedException{
 name = new String("A");
 prototypeAA = new PrototypeAA();
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeA clone method");
 PrototypeA returnPrototype = (PrototypeA)super.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #cat PrototypeAA.java
public class PrototypeAA implements Cloneable{
 public String name = null;
 public PrototypeAAA prototypeAAA = null;
 public PrototypeAA() throws CloneNotSupportedException{
 name = new String("AA");
 prototypeAAA = new PrototypeAAA();
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeAA clone method");
 PrototypeAA returnPrototype = (PrototypeAA)super.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #cat PrototypeAAA.java
public class PrototypeAAA implements Cloneable{
 public String name = null;
 public PrototypeAAA() throws CloneNotSupportedException{
 name = new String("AAA");
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeAAA clone method");
 return super.clone();
 }
}

clone demo #
clone demo #echo "In the above shallow clone example, the class Driver has a field that is object (prototypeA). \The class PrototypeA has a field that is object (prototypeAA)
> The class PrototypeAA has a field that is object (protytypeAAA). \
> We made a shallow clone of object prototypeA in Driver.java by calling the clone method of the class PrototypeA. \
> It is called shallow clone because in the clone method of PrototypeA, all we did is to call the super.clone() to clone itself. \
> The super class of PrototypeA is class Object. By default, Object.clone() only makes shallow copy the member fields -- if a field is an object, only the reference is copied.\> As a result, the prototypeAA field in bothe prototypeA and prototypeAClone both reference the same object. Changes in the prototypeAA field is visible to both prototypeA and prototypeAClone\
> Of course, this is not what we want, we want to make a real clone of prototypeA, don't want to share any fields with original object prototypeA.">/dev/nullclone demo #
clone demo #javac Driver.java PrototypeA.java PrototypeAA.java PrototypeAAA.java
clone demo #java Driver
in the prototypeA clone method
prototypeAClone.name: A
prototypeAClone.prototypeAA.name: newAA
prototypeAClone.prototypeAA.prototypeAAA.name: newAAA
clone demo #
clone demo #echo "In the following example, what we will make a deep clone of object prototypeA in Driver.java.\> In order to deep clone, we need to modify the clone method of class PrototypeA, we need to recursively deep copy its member field prototypeAA.\ > In this case, we replace the reference to prototypeAA with the reference to prototypeAA's clone.\
> Of course, prototypeAA's clone has to be deep clone also, means in the cloned object, the member field prototypeAAA has to reference to original prototypeAA's member field prototypeAAA's clone.">/dev/null
clone demo #
clone demo #vi PrototypeA.java
clone demo #vi PrototypeAA.java
clone demo #cat PrototypeA.java
public class PrototypeA implements Cloneable{
 public String name = null;
 public PrototypeAA prototypeAA = null;
 public PrototypeA() throws CloneNotSupportedException{
 name = new String("A");
 prototypeAA = new PrototypeAA();
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeA clone method");
 PrototypeA returnPrototype = (PrototypeA)super.clone();
 returnPrototype.prototypeAA = (PrototypeAA)prototypeAA.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #cat PrototypeAA.java
public class PrototypeAA implements Cloneable{
 public String name = null;
 public PrototypeAAA prototypeAAA = null;
 public PrototypeAA() throws CloneNotSupportedException{
 name = new String("AA");
 prototypeAAA = new PrototypeAAA();
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeAA clone method");
 PrototypeAA returnPrototype = (PrototypeAA)super.clone();
 returnPrototype.prototypeAAA = (PrototypeAAA)prototypeAAA.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #javac PrototypeA.java PrototypeAA.java
clone demo #java Driver
in the prototypeA clone method
in the prototypeAA clone method
in the prototypeAAA clone method
prototypeAClone.name: A
prototypeAClone.prototypeAA.name: AA
prototypeAClone.prototypeAA.prototypeAAA.name: AAA
clone demo #
clone demo #
clone demo #echo "The above clone example is not secure, to show why, we will write an EvileDriver.java and SpoofPrototypeA.java, which overwrite the clone method, and nullify all the deep clone effort" > /dev/null
clone demo #vi EvileDriver.java
clone demo #vi SpoofPrototypeA.java
clone demo #cat EvileDriver.java
public class EvilDriver {
 public SpoofPrototypeA prototypeA = null;
 public static void main(String[] args) throws CloneNotSupportedException {
 //Driver.main(null);
 Driver driver = new Driver();
 //EvilDriver driver = new EvilDriver();
 driver.prototypeA = new SpoofPrototypeA();
 PrototypeA prototypeAClone = (PrototypeA)driver.prototypeA.clone();
 //change original object name
 driver.prototypeA.name = new String("newA");
 driver.prototypeA.prototypeAA.name = new String("newAA");
 driver.prototypeA.prototypeAA.prototypeAAA.name = new String("newAAA");
 //printout the cloned object names
 System.out.println("prototypeAClone.name: "+prototypeAClone.name);
 System.out.println("prototypeAClone.prototypeAA.name: "+prototypeAClone.prototypeAA.name);
 System.out.println("prototypeAClone.prototypeAA.prototypeAAA.name: "+prototypeAClone.prototypeAA.prototypeAAA.name);
 //Driver.main(null);
 }
}

clone demo #
clone demo #cat SpoofPrototypeA.java
public class SpoofPrototypeA extends PrototypeA{
 public String name = null;
 public PrototypeAA prototypeAA = null;

public SpoofPrototypeA() throws CloneNotSupportedException {
 super();
 }

 public Object clone() throws CloneNotSupportedException {
 System.out.println("in the SpoofPrototypeA clone method");
 PrototypeA returnPrototype = this;
 //returnPrototype.prototypeAA = (PrototypeAA)super.prototypeAA.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #javac EvileDriver.java SpoofPrototypeA.java
EvileDriver.java:1: class EvilDriver is public, should be declared in a file named EvilDriver.java
public class EvilDriver {
 ^
1 error
clone demo #mv EvileDriver.java EvilDriver.java
clone demo #javac EvileDriver.java SpoofPrototypeA.java
javac: file not found: EvileDriver.java
Usage: javac <options> <source files>
use -help for a list of possible options
clone demo #javac EvilDriver.java SpoofPrototypeA.java
clone demo #java EvilDriver
in the SpoofPrototypeA clone method
prototypeAClone.name: newA
prototypeAClone.prototypeAA.name: newAA
prototypeAClone.prototypeAA.prototypeAAA.name: newAAA
clone demo #
clone demo #
clone demo #echo "the cure of the above problem is to make the clone method final.">/dev/null
clone demo #vi PrototypeA.java
clone demo #vi PrototypeAA.java
clone demo #vi PrototypeAAA.java
clone demo #cat PrototypeA.java
public class PrototypeA implements Cloneable{
 public String name = null;
 public PrototypeAA prototypeAA = null;
 public PrototypeA() throws CloneNotSupportedException{
 name = new String("A");
 prototypeAA = new PrototypeAA();
 }

 public final Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeA clone method");
 PrototypeA returnPrototype = (PrototypeA)super.clone();
 returnPrototype.prototypeAA = (PrototypeAA)prototypeAA.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #cat PrototypeAA.java
public class PrototypeAA implements Cloneable{
 public String name = null;
 public PrototypeAAA prototypeAAA = null;
 public PrototypeAA() throws CloneNotSupportedException{
 name = new String("AA");
 prototypeAAA = new PrototypeAAA();
 }

 public final Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeAA clone method");
 PrototypeAA returnPrototype = (PrototypeAA)super.clone();
 returnPrototype.prototypeAAA = (PrototypeAAA)prototypeAAA.clone();
 return returnPrototype;
 }
}

clone demo #
clone demo #cat PrototypeAAA.java
public class PrototypeAAA implements Cloneable{
 public String name = null;
 public PrototypeAAA() throws CloneNotSupportedException{
 name = new String("AAA");
 }

 public final Object clone() throws CloneNotSupportedException {
 System.out.println("in the prototypeAAA clone method");
 return super.clone();
 }
}

clone demo #
clone demo #echo "now EvilDriver won't work because the clone method is no longer overwritable" > /dev/null
clone demo #javac Driver.java PrototypeA.java PrototypeAA.java PrototypeAAA.java
clone demo #javac EvilDriver.java
clone demo #java Driver
in the prototypeA clone method
in the prototypeAA clone method
in the prototypeAAA clone method
prototypeAClone.name: A
prototypeAClone.prototypeAA.name: AA
prototypeAClone.prototypeAA.prototypeAAA.name: AAA
clone demo #java EvilDriver
Exception in thread "main" java.lang.VerifyError: class SpoofPrototypeA overrides final method clone.()Ljava/lang/Object;
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
 at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
clone demo #

Advertisements

Make a Comment

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

One Response to “Java Class Clone”

RSS Feed for IT Certifications Comments RSS Feed

Hello, sir i would like to ask that what is the scope of java training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course http://www.wiziq.com/course/1779-core-and-advance-java-concepts of core and advance java online ?? or tell me any other guidance…
would really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: