Once upon a time there was a class:
public class Scope<C extends Cloneable & Comparable<C>> implements Comparable<Scope<C>>, Cloneable, Serializable {
private C starts;
private C ends;
...
@SuppressWarnings("unchecked")
@Override
public Object clone() {
Scope<C> scope;
try {
scope = (Scope<C>) super.clone();
scope.setStarts((C) starts.clone()); // The method clone() from the type Object is not visible
scope.setEnds((C) ends.clone()); // The method clone() from the type Object is not visible
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported");
}
return scope;
}
}
In Object we have:
protected native Object clone() throws CloneNotSupportedException;
And Cloneable interface is:
public interface Cloneable {
}
How should I clone this?
I think the current green answer is bad , why you might ask?
- It adds a lot of code
- It requires you to list all fields to be copied and do this
- This will not work for Lists when using clone() (This is what clone() for HashMap says: Returns a shallow copy of this HashMap instance: the keys and valuesthemselves are not cloned.) so you end up doing it manually (this makes me cry)
Oh and by the way serialization is also bad, you might have to add Serializable all over the place (this also makes me cry).
So what is the solution:
Java Deep-Cloning library The cloning library is a small, open source (apache licence) java library which deep-clones objects. The objects don't have to implement the Cloneable interface. Effectivelly, this library can clone ANY java objects. It can be used i.e. in cache implementations if you don't want the cached object to be modified or whenever you want to create a deep copy of objects.
Cloner cloner=new Cloner();
XX clone = cloner.deepClone(someObjectOfTypeXX);
Check it out at http://code.google.com/p/cloning/
This is one reason why no one likes Cloneable. It's supposed to be a marker interface, but it's basically useless because you can't clone an arbitrary Cloneable object without reflection.
Pretty much the only way to do this is to create your own interface with a public clone() method (it doesn't have to be called "clone()"). Here's an example from another StackOverflow question.
Hopefully I've resolved the problem of generic cloning in Java:
public class Generic<T> {
private T data;
public Generic() {
// ...
}
@SuppressWarnings("unchecked")
@Override
public Object clone() {
Generic<T> cloned = new Generic<T>();
try {
cloned.data = (T) data.getClass().getMethod("clone").invoke(data);
} catch (Exception e) {
// ...
}
return cloned;
}
}
Slightly OT, but you could save yourself a lot of future grief with this:
catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported", e);
}
So that when you get a stack trace you know which object caused the problem.
To answer the core question, your own interface that implements a public clone() as mmyers wrote and require that C extend that as well.
As a general comment, avoid using Object.clone() whenever possible. If you have control over the code in question, implement a copy constructor instead. See here for information.
As you see, if a class tries to implement Cloneable and you want a deep clone, then all of your constituent objects needs to be immutable, primitive, or need to also be Cloneable.
Often, a better and easier approach is to create a copy constructor.
public class Scope<C extends Comparable<C>> implements Comparable<Scope<C>>, Serializable {
private C starts;
private C ends;
public Scope(final Scope original) {
starts = new C(original.starts);
ends = new C(original.ends);
// initialize all my other fields from "original"
}
}
and of course you need a copy constructor on C that is capable of handling polymorphism.
If you have no access or ability to modify the source to C, then any method of copying, no matter what the method, will be very difficult and potentially impossible. For example, it is not possible to make a copy of an enum instance.
来源:https://stackoverflow.com/questions/803971/cloning-with-generics