replacements for Unsafe

Kasper Nielsen kasperni at gmail.com
Wed Apr 29 11:39:05 UTC 2020


Hi Jochen,

Is there anything stopping you for doing something like:

try {
    Class.forName(innerClassName, true, initalizeClass.getClassLoader());
} catch (ClassNotFoundException e) {
    throw new ExceptionInInitializerError(e); // Should never happen
}

I know it is not pretty.

/Kasper



On Wed, 29 Apr 2020 at 12:26, Jochen Theodorou <blackdrag at gmx.org> wrote:
>
> Hi,
>
> when jigsaw started there was a lot of talk about providing alternatives
> to what Unsafe offers. Today I am facing a problem I cannot solve
> anymore it seems with this.
>
> My problem is that I need to ensure the static block of an inner class
> has been executed before I do something, because the static block
> contains setup data the class will need and influence what I do with the
> class. Of course this might be not the best choice, I am just trying to
> keep certain functionality.
>
> In the past I would have used Unsafe#ensureClassInitialized, but this
> method has not found a replacement yet if I read JDK-8235521 :
> Replacement API for Unsafe::ensureClassInitialized correctly.
>
> Also because of Unsafe#getUnsafe, checking for the platform loader for
> the caller (which is not the case for me) I cannot get the unsafe object
> itself "properly".
>
> Now I am wondering what alternatives exist. Are there any?
>
> bye Jochen


More information about the jigsaw-dev mailing list