replacements for Unsafe

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

replacements for Unsafe

Jochen Theodorou
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
Reply | Threaded
Open this post in threaded view
|

Re: replacements for Unsafe

Remi Forax
Hi Jochen,
Per the Java spec, calling an empty static method of the inner class works.

Rémi

----- Mail original -----
> De: "Jochen Theodorou" <[hidden email]>
> À: "jigsaw-dev" <[hidden email]>
> Envoyé: Mercredi 29 Avril 2020 13:23:47
> Objet: replacements for Unsafe

> 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
Reply | Threaded
Open this post in threaded view
|

Re: replacements for Unsafe

Kasper Nielsen
In reply to this post by Jochen Theodorou
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: replacements for Unsafe

Jochen Theodorou
On 29.04.20 13:39, Kasper Nielsen wrote:
> 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
> }

possibly the security manager, but it should be no real problem... and I
thought I
I tried that... looks like I did not.

Thanks, works for me!

bye Jochen

Reply | Threaded
Open this post in threaded view
|

Re: replacements for Unsafe

Jochen Theodorou
In reply to this post by Remi Forax
On 29.04.20 13:37, Remi Forax wrote:
> Hi Jochen,
> Per the Java spec, calling an empty static method of the inner class works.

but it has to work for any arbitrary inner class.

The Class.forName variant Kasper mentioned and I thought I tired already
(but did not) works for me.

Thanks

bye Jochen