Unsafe.defineAnonymousClass no longer exists in Java17!

Attila Szegedi szegedia at gmail.com
Wed Jan 24 17:42:41 UTC 2024


BTW, defineHiddenClass is not a good replacement for us, ‘cause if we used it, then JavaScript code compiled to Java bytecode will no longer show up in stack traces (as the generated classes are hidden.) With defineAnonymousClass we could still have proper stack traces when an exception is thrown from compiled JavaScript code.

I had discussions with folks at Oracle about this, but it’s not a high priority for them to fix this, sadly.

There’s not much drawback to this, BTW. Nashorn will just load the compiled code as ordinary classes; defineAnonymousClass was a bit of a performance optimization; such classes were marginally lighter and also could skip the bytecode verifier (for better or worse.)

Attila.

> On 2024. Jan 24., at 14:22, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> 
> 
> From: "Luc Chapon" <sxilderik at gmail.com>
> To: "nashorn-dev" <nashorn-dev at openjdk.org>
> Sent: Wednesday, January 24, 2024 1:02:48 PM
> Subject: Unsafe.defineAnonymousClass no longer exists in Java17!
> Hello all
> 
> First message here
> 
> We are facing a problem re Nashorn (latest) and java17
> 
> In Context.java, line 330, you try to get a handle to a no longer existent method, Unsafe.defineAnonymousClass
> 
> And fail of course.
> 
> I’m trying to rewrite this part for our own temporary urgent need according to this.
> 
> https://www.oracle.com/java/technologies/javase/17-relnote-issues.html
> core-libs
> ➜ <https://www.oracle.com/java/technologies/javase/17-relnote-issues.html#JDK-8243287> Removal of sun.misc.Unsafe::defineAnonymousClass
> sun.misc.Unsafe::defineAnonymousClass API has been removed in JDK 17. The API replacement is java.lang.invoke.MethodHandles.Lookup::defineHiddenClass and java.lang.invoke.MethodHandles.Lookup::defineHiddenClassWithClassData.
> 
> 
> Are you aware of the problem? Is there a fix already available?
> 
> Hello,
> defineAnonymousClass have been replaced by Lookup.defineHiddenClass(...) [1] which is a public API.
> 
> 
> Thank you!
> 
> regards,
> Rémi
> 
> 
> Luc Chapon
> ChapsVision
> Suresnes, France
> 
> [1] https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte[],boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption...)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/nashorn-dev/attachments/20240124/d815dabb/attachment-0001.htm>


More information about the nashorn-dev mailing list