<div dir="ltr"><div>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.</div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>Do you have any suggestions? Am I overlooking something that other serialization libraries are doing here?</div><div><br></div><div><br></div><div>Thanks,</div><div>Shahan</div></div>