I am writing some FFI code in Java that makes heavy use of sun.misc.Unsafe
.
In Java 9, this class will become inaccessible, and will become jdk.unsupported.Unsafe
. I would like to write my code so that it works now, but continues to work in Java 9.
What is the least hacky way to do this? I would prefer binary compatibility, but source compatibility is also okay.
Edit: I am 100% not okay with using reflection – or even virtual dispatch – every time a method on Unsafe
is called. Most of those methods compile to a single machine instruction. Therefore, performance really matters. It's okay to have wrappers – but only if I can be sure the JIT will inline them, every time.
My current plan is to load an appropriate class at runtime.