lundi 27 novembre 2017

Is it still possible to use java.misc.Unsafe in Java 9?

Until Java 8 it was possible to obtain the singleton java.misc.Unsafe instance via a method like the following:

public static Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Even though it was highly suggested not to use Unsafe, many vendors and libraries use it internally.

Now, with Java 9 and Jigsaw, I think that also the way Unsafe is handled has changed. I read some posts with contrary information. Some say it has been completely hidden and it is not even retrievable, others say one has to enable a VM flag, while others write that it should be officially support now.

So: Is it possible to still use java.misc.Unsafe in Java 9, and if so, how?





Aucun commentaire:

Enregistrer un commentaire