Java serialization readResolve method and kryo

114 Views Asked by At

I'm trying to transfer a class containing a transient field (a Cache which makes no sense to serialize). In the past that field was recreated using the readResolve method offered by the standard java serialization framework.

public class MyClass implements Serializable {

  private transient LoadingCache<Key, Value> someCache;

//this works when using default java tools. I need an equivalent method in kryo framework
  Object readResolve() {
    someCache = createNewCache();
    return this;
  }
}

Kryo does not to execute that method, is there a different way to tell kryo what needs to be done with a deserialized object to reinit state?

A custom serializer is not an option, there are already many different serializers in the project which would all need adjusting.

1

There are 1 best solutions below

0
Bytewright On

Found a solution but I'm still open for something better: I created an annotation which is added on the target method of a deserialized object. Then I added a custom com.esotericsoftware.kryo.ClassResolver which scans new registrations for that annotation.

If its found the serializer for that registration is wrapped in a delegating serializer which executes the annotated method once the object is recreated.