This thread makes me realize that Kryo has just about all the information it needs to implement automatic deep copy. In Kryo, serializers are registered for types, so that it knows how to serialize and deserialize various types as they are encountered in an object graph. With just a new method or two, the serializers could know how to do a deep copy. This would be a real deep copy too, not object->bytes->object (which is currently possible, but not efficient). This would be pretty slick as lots of stuff gets reused: the class to serializer registration, walking the object graph, creating new instances of objects via ReflectASM/reflection/Objenesis, using ReflectASM/reflection to access fields and methods, etc.
I’ve implemented most of it, I’ll post tomorrow when it is finished and committed. Works like this…
Kryo kryo = new Kryo();
ArrayList test = new ArrayList();
test.add("one");
test.add("two");
test.add("three");
ArrayList test2 = kryo.copy(test);
assertTrue(test != test2);
assertEquals(test, test2);
ArrayList test3 = new ArrayList();
test3.add(1);
test3.add(2f);
test3.add(3d);
test3.add((byte)4);
test3.add((short)5);
test.add(test3);
test2 = kryo.copy(test);
assertTrue(test != test2);
assertEquals(test, test2);
There is also Kryo#copyShallow(Object).
Supporting deep copy for a type is easy, eg here is what I had to add to CollectionSerializer…
public Collection createCopy (Kryo kryo, Collection original) {
return kryo.newInstance(original.getClass());
}
public void copy (Kryo kryo, Collection original, Collection copy) {
for (Object element : original)
copy.add(kryo.copy(element));
}
Just like for deserialization, I have to separate create from copying nested objects in order to have the framework automatically handle multiple references to the same object / circular references.
Sorry for the thread hijack. 