6

In Java there is the Unsafe class in the java.lang.unsafe package that provides low-level access to operations.

Now it seems to me that the JVM needs to support all of the methods available in the Unsafe class in order to be compliant to the JLS, example methods can be found here.

Examples of the operations Unsafe provides and JVMs need to support in order to be compliant to the JLS would be:

  • Working with objects
  • Storing data (of type int, long, byte, etc.)
  • Retrieving data
  • Working with monitors
  • Working with memory fences
  • etc.

My question now is: Why can we not find the Unsafe class in a java.* package? Is there a specific reason why it would be better that every JVM provider creates their own sun.misc.unsafe.Unsafe variant instead of writing an implementation for a class defined in the java.* package?

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • 4
    What makes you conclude `sun.misc.unsafe` is a part of the JLS in the first place? If anything, [Oracle wanted to remove it](http://www.infoworld.com/article/2957203/java/java-devs-rejoice-as-private-apis-stay-in-for-now.html). – Jeroen Vannevel Aug 12 '15 at 18:08
  • @JeroenVannevel I don't (intend) to claim that `sun.misc.unsafe` is a part of the JLS, only that the operations `Unsafe` supports are a part of the JLS. – skiwi Aug 12 '15 at 18:09
  • 1
    @skiwi: what part of the JLS specifies that exactly? I can't immediately find it. In the end a language specification doesn't specify what should be available in a library. https://docs.oracle.com/javase/specs/jls/se8/html/index.html – Jeroen Vannevel Aug 12 '15 at 18:09
  • 1
    http://openjdk.java.net/jeps/260 – Sotirios Delimanolis Aug 12 '15 at 18:14
  • 1
    The obvious answer would be "because it's unsafe". – Mast Aug 12 '15 at 18:25

2 Answers2

9

Making Unsafe part of the core API, and implementing the JLS are separate issues.

Unsafe is not part of the Java API because it's unsafe, and Oracle didn't want developers to use it.

However, they are well aware 1) that many good developers have used it successfully, and that 2) many bad developers have abused it. So, there is a long term plan to expose the useful features of Unsafe through the core Java platform in a safe, sane, and supportable way.

You can view a presentation from Mark Reinhold that explains in more detail how Unsafe will be migrated and eventually go way.

However, even when Unsafe APIs are moved into some java (or javax) package, providers will still have internal classes and native code to implement those APIs. For example, Oracle might have something like oracle.internal.misc.StillUnsafe with a bunch of native methods in it. This class won't be governed by the JLS; Oracle will be free to implement it how they like and change it when they like. But, with this approach, they would also provide implementations of the core Java APIs that delegate to their internal class.

Declaring the API in a java.* package does not mean that authors of Java runtimes don't have to provide the code to back it up; many Java APIs are abstract and must be given an implementation by a provider.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • The video I linked uses [this question](http://stackoverflow.com/q/24429777/3474) as an example of "naive" developers wanting to use `Unsafe` in risky ways. Laughter ensues. – erickson Aug 12 '15 at 18:56
3

The Java unsafe was package was only supposed to be used by core Java Classes see here. Also Java did not want individuals using the sun.misc.unsafe class at, in fact, they wanted to remove it. Oracle wanted to discourage the use of unsafe class, while limiting the class to only support trusted code.

There is a proposal to add Unsafe to supported Java Classes, but not how it exists today. Instead Unsafe will be encapsulated within the modules that define and use them. This is a formal proposal, which arose due to the outcry Oracle recieved when they stated intentions to remove Unsafe.

Turtle
  • 1,369
  • 1
  • 17
  • 32