Klangism

Things that were important enough at the time when they were written.

Viktor Klang is a legendary programmer, known from places like the Internet. Consider following him on Twitter.

Nov 23

Serializable dynamic Proxies

So, I found myself in a situation where I was creating JDK Proxies, and I was in control of the invocation of newProxyInstance and needed to make the Proxies transparently Serializable.

The problem is that for it to work you need writeReplace, so that you’re in control of the rematerialization of the Proxy on the deserializing side using readResolve.

However, the problem is that you need to make sure that the Proxy gets a writeReplace method, and you don’t want to force the guy providing you with what interfaces to proxy to implement writeReplace himself, because at some point someone will forget it.

The solution might seem trivial, but the solution is the following:

Define a trait/interface with the writeReplace method (with correct signature)

Happy hAkking!

Cheers, √


  1. klangism posted this