javajava-9sun

Is sun.misc.Unsafe become public in JDK9?


I just tried JDK9 and found out that sun.misc.Unsafe is now containing not a native method, but delegates them to some jdk.internal.misc.Unsafe, for example:

@ForceInline
public int getInt(Object o, long offset) {
    return theInternalUnsafe.getInt(o, offset);
}

The latest, in turn, looks actually like the old sun.misc.Unsafe, but now the methods are annotated with some annotation:

@HotSpotIntrinsicCandidate
public native void putObject(Object o, long offset, Object x);

So, is it "safe" to use Unsafe starting JDK9? Is it a public official API now?


Solution

  • Here is a good explanation:

    https://adtmag.com/blogs/watersworks/2015/08/java-9-hack.aspx

    What to do with sun.misc.Unsafe in Java 9? One side says it's simply an awful hack from the bad old days that should be gotten rid of; the other side says its heavy use is responsible for the rise of Java in the infrastructure space and popular tools still need it. The problem is, both sides are right. ...

    Writing on the OpenJDK mailing list, Reinhold proposed encapsulating unsupported, internal APIs, including sun.misc.Unsafe, within modules that define and use them. That proposal is now a formal Java Enhancement Proposals (JEP). Posted this week, JEP 260 ("Encapsulate Most Internal APIs") aims to "make most of the JDK's internal APIs inaccessible by default, but leave a few critical, widely used internal APIs accessible, until supported replacements exist for all or most of their functionality."

    Short answer: you should NOT use it in any new application you're developing from scratch.