Compile-time warnings when compiling code that utilizes Unsafe

Ron Pressler ron.pressler at oracle.com
Mon May 13 13:43:50 UTC 2024



> On 6 May 2024, at 20:10, Olexandr Rotan <rotanolexandr842 at gmail.com> wrote:
> 
> Sorry to put this message in another thread, I have just subscribed to jdk-dev so dont have the initial thread in the box.
> 
> In this mail I would love to ask/propose to emit more verbose compile-time warnings when compiled code uses deprecated Unsafe methods with suggestions to replace it with corresponding alternatives in new APIs.

Deprecated API elements document their replacements in the JavaDoc documentation. Unsafe is undocumented (as it is not a supported API) but, as you can see in the PR, the documentation comments in the source (which are the current source of information about Unsafe) do refer to the relevant replacement:

https://github.com/openjdk/jdk/pull/19174/files


> 
> As for now, If I understood everything correctly, warnings are only emitted at runtime and do not contain any particular information on where and what has been called exactly. Imo, this noninformative message does not carry any particular info for people that write code using Unsafe and do not help or encourage migrating to new APIs.


The people who can replace the use of Unsafe are those who write the code directly using Unsafe. The compile-time deprecation warnings are for them, and, as I wrote above, they are helped by information on replacements in the same place where they find information about Unsafe itself.

The runtime warnings are seen not by library developers but by application developers, and the warnings are intended to make those application developers aware of the risk imposed by the libraries they consume. They should then contact the authors of those libraries to ensure that a new version of the library that does not use Unsafe is in the works. These warnings don’t contain any information on how to change the code as they are seen by those who do not control the code.

— Ron


More information about the jdk-dev mailing list