JEP 8323072 and Serialization

Shahan Yang shahan at google.com
Wed Mar 27 23:20:44 UTC 2024


My apologies if this is the wrong place for this kind of question. I
maintain an Unsafe-heavy serialization library and I am weighing options in
light of Unsafe memory access deprecation.

It appears that the recommended replacement is to use VarHandles. This
seems reasonable at first glance, but it looks like VarHandles do not
support writes to final fields.

The problem is that there's a large codebase with a lot of application
logic objects to be serialized. The presence of final fields on those
objects aids in their readability. Currently, those fields can just be set
using Unsafe.put* methods during deserialization, but that isn't possible
with VarHandles. I don't think it's reasonable to ask developers to make
all of the application object fields non-final for deserialization. It
would also be an infeasibly large amount of toil to add custom code to
serialize and deserialize all those objects, relative to what is possible
with reflection.

I can think of a number of alternatives, but so far I haven't come up with
anything that avoids harming the readability of the code, increasing toil,
making serialization more error prone or slower (just using regular Field
reflection instead of Unsafe to set final fields?). I'm less worried about
records because the tight contract between their constructors and their
fields makes a code generation-based solution quite clean.

Do you have any suggestions? Am I overlooking something that other
serialization libraries are doing here?


Thanks,
Shahan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240327/ba78f166/attachment.htm>


More information about the amber-dev mailing list