OnJava8-Examples/references/CheckCloneable.java

113 lines
2.8 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// references/CheckCloneable.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Check to see if a reference can be cloned
2015-06-15 17:47:35 -07:00
// Can't clone this -- doesn't override clone():
class Ordinary {}
// Overrides clone, doesn't implement Cloneable:
class WrongClone extends Ordinary {
@Override public Object clone()
throws CloneNotSupportedException {
return super.clone(); // Throws exception
}
}
// Does all the right things for cloning:
class IsCloneable extends Ordinary
implements Cloneable {
@Override public Object clone()
throws CloneNotSupportedException {
return super.clone();
}
}
// Turn off cloning by throwing the exception:
class NoMore extends IsCloneable {
@Override public Object clone()
throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
class TryMore extends NoMore {
@Override public Object clone()
throws CloneNotSupportedException {
// Calls NoMore.clone(), throws exception:
return super.clone();
}
}
class BackOn extends NoMore {
private BackOn duplicate(BackOn b) {
// Somehow make a copy of b and return that
// copy. A dummy copy, just to make a point:
return new BackOn();
}
@Override public Object clone() {
2015-06-15 17:47:35 -07:00
// Doesn't call NoMore.clone():
return duplicate(this);
}
}
// You can't inherit from this, so you can't
// override clone() as you can in BackOn:
final class ReallyNoMore extends NoMore {}
public class CheckCloneable {
public static
Ordinary tryToClone(Ordinary ord) {
String id = ord.getClass().getName();
System.out.println("Attempting " + id);
Ordinary x = null;
if(ord instanceof Cloneable) {
try {
2016-01-25 18:05:55 -08:00
x = (Ordinary)((IsCloneable)ord).clone();
2015-06-15 17:47:35 -07:00
System.out.println("Cloned " + id);
} catch(CloneNotSupportedException e) {
System.out.println(
"Could not clone " + id);
}
} else {
2016-01-25 18:05:55 -08:00
System.out.println("Doesn't implement Cloneable");
2015-06-15 17:47:35 -07:00
}
return x;
}
public static void main(String[] args) {
// Upcasting:
Ordinary[] ord = {
new IsCloneable(),
new WrongClone(),
new NoMore(),
new TryMore(),
new BackOn(),
new ReallyNoMore(),
};
Ordinary x = new Ordinary();
2016-01-25 18:05:55 -08:00
// This won't compile because
2015-06-15 17:47:35 -07:00
// clone() is protected in Object:
2015-12-18 11:28:19 -08:00
//- x = (Ordinary)x.clone();
2016-01-25 18:05:55 -08:00
// Checks first to see if the class
2015-06-15 17:47:35 -07:00
// implements Cloneable:
for(Ordinary ord1 : ord) {
tryToClone(ord1);
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Attempting IsCloneable
Cloned IsCloneable
Attempting WrongClone
Doesn't implement Cloneable
Attempting NoMore
Could not clone NoMore
Attempting TryMore
Could not clone TryMore
Attempting BackOn
Cloned BackOn
Attempting ReallyNoMore
Could not clone ReallyNoMore
2015-09-07 11:44:36 -06:00
*/