You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Caleb <ca...@gmail.com> on 2026/04/12 20:09:35 UTC
[JVM Question] Making Friends with Ad-Hoc Classes
Hi! This isn't really related to Groovy, but I'm doing weird things with
the JVM for a compiler plugin and I've hit a knowledge barrier I can't
pass. Since you guys are the experts on "doing weird stuff with the JVM",
I was hoping you might be willing to help?
My goal is to copy existing methods, transform them, and link to those
transformed versions with InvokeDynamic. Basically the template system
from C++, but done at runtime.
I've got all of the groundwork laid to do this, but I'm having trouble with
access violations in my clones.
Since I'm *cloning* methods, they'll be generated into a class other than
the one being cloned. But then since I'm cloning *methods*, they need to
be able to access PRIVATE MEMBERS of the original instance.
I tried generating public bridge methods, but private types still break
that. If possible, I'd like to just make my clone a "friend" of the
original class like in C++, but I don't know how.
I fully control the classes that clones can be made from, so I can mark
anything else as an "inner class" or "nestmate" as needed. I just can't
figure out what each of those ALLOW on the JVM from the specification docs
alone. And you know how helpful google is nowadays; I couldn't find
anything no matter how I phrased it.
Does anyone know how to do this?
Thank you so much for your help!
Re: [JVM Question] Making Friends with Ad-Hoc Classes
Posted by Remi Forax <fo...@univ-mlv.fr>.
> From: "Caleb" <ca...@gmail.com>
> To: "dev" <de...@groovy.apache.org>
> Sent: Monday, April 13, 2026 3:11:55 PM
> Subject: Re: [JVM Question] Making Friends with Ad-Hoc Classes
> I know how to emit ad-hoc classes - I also know that hidden classes specifically
> don't have an entry in the symbol table
you mean their name is not registered inside a classloader,
> and can be GC'd,
that depends on the ClassOptions you provide (see ClassOption.STRONG)
> making them particularly useful for single-function classes - I just don't know
> how you'd make a class you don't know the name of a friend/innerclass.
defineHiddenClass takes a lookup as parameter and if you use ClassOption.NESTMATE then the defined class is a nestmate of the lookupClass of the Lookup.
Caleb, if you want to know more, email me directly, because it starts to be OT for the goovy-dev mailing list.
regards,
Rémi
> On Mon, Apr 13, 2026, 3:13 AM Remi Forax < [ mailto:forax@univ-mlv.fr |
> forax@univ-mlv.fr ] > wrote:
>>> From: "Caleb" < [ mailto:calebb2048@gmail.com | calebb2048@gmail.com ] >
>>> To: "dev" < [ mailto:dev@groovy.apache.org | dev@groovy.apache.org ] >
>>> Sent: Sunday, April 12, 2026 10:09:35 PM
>>> Subject: [JVM Question] Making Friends with Ad-Hoc Classes
>>> Hi! This isn't really related to Groovy, but I'm doing weird things with the JVM
>>> for a compiler plugin and I've hit a knowledge barrier I can't pass. Since you
>>> guys are the experts on "doing weird stuff with the JVM", I was hoping you
>>> might be willing to help?
>>> My goal is to copy existing methods, transform them, and link to those
>>> transformed versions with InvokeDynamic. Basically the template system from
>>> C++, but done at runtime.
>>> I've got all of the groundwork laid to do this, but I'm having trouble with
>>> access violations in my clones.
>>> Since I'm *cloning* methods, they'll be generated into a class other than the
>>> one being cloned. But then since I'm cloning *methods*, they need to be able to
>>> access PRIVATE MEMBERS of the original instance.
>>> I tried generating public bridge methods, but private types still break that. If
>>> possible, I'd like to just make my clone a "friend" of the original class like
>>> in C++, but I don't know how.
>>> I fully control the classes that clones can be made from, so I can mark anything
>>> else as an "inner class" or "nestmate" as needed. I just can't figure out what
>>> each of those ALLOW on the JVM from the specification docs alone. And you know
>>> how helpful google is nowadays; I couldn't find anything no matter how I
>>> phrased it.
>>> Does anyone know how to do this?
>> The class that encapsulates your clone should be an hidden class (this is the
>> mechanism used to load lambda bodies)
>> [
>> https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte[],boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption..
>> |
>> https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte[],boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption..
>> ] .)
>>> Thank you so much for your help!
>> regards,
>> Rémi
Re: [JVM Question] Making Friends with Ad-Hoc Classes
Posted by Caleb <ca...@gmail.com>.
I know how to emit ad-hoc classes - I also know that hidden classes
specifically don't have an entry in the symbol table and can be GC'd,
making them particularly useful for single-function classes - I just don't
know how you'd make a class you don't know the name of a friend/innerclass.
On Mon, Apr 13, 2026, 3:13 AM Remi Forax <fo...@univ-mlv.fr> wrote:
>
>
> ------------------------------
>
> *From: *"Caleb" <ca...@gmail.com>
> *To: *"dev" <de...@groovy.apache.org>
> *Sent: *Sunday, April 12, 2026 10:09:35 PM
> *Subject: *[JVM Question] Making Friends with Ad-Hoc Classes
>
> Hi! This isn't really related to Groovy, but I'm doing weird things with
> the JVM for a compiler plugin and I've hit a knowledge barrier I can't
> pass. Since you guys are the experts on "doing weird stuff with the JVM",
> I was hoping you might be willing to help?
>
>
> My goal is to copy existing methods, transform them, and link to those
> transformed versions with InvokeDynamic. Basically the template system
> from C++, but done at runtime.
> I've got all of the groundwork laid to do this, but I'm having trouble
> with access violations in my clones.
>
> Since I'm *cloning* methods, they'll be generated into a class other than
> the one being cloned. But then since I'm cloning *methods*, they need to
> be able to access PRIVATE MEMBERS of the original instance.
>
> I tried generating public bridge methods, but private types still break
> that. If possible, I'd like to just make my clone a "friend" of the
> original class like in C++, but I don't know how.
>
> I fully control the classes that clones can be made from, so I can mark
> anything else as an "inner class" or "nestmate" as needed. I just can't
> figure out what each of those ALLOW on the JVM from the specification docs
> alone. And you know how helpful google is nowadays; I couldn't find
> anything no matter how I phrased it.
>
> Does anyone know how to do this?
>
>
> The class that encapsulates your clone should be an hidden class (this is
> the mechanism used to load lambda bodies)
>
>
> https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte[],boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption..
> .)
>
>
>
> Thank you so much for your help!
>
>
> regards,
> Rémi
>
>
Re: [JVM Question] Making Friends with Ad-Hoc Classes
Posted by Remi Forax <fo...@univ-mlv.fr>.
> From: "Caleb" <ca...@gmail.com>
> To: "dev" <de...@groovy.apache.org>
> Sent: Sunday, April 12, 2026 10:09:35 PM
> Subject: [JVM Question] Making Friends with Ad-Hoc Classes
> Hi! This isn't really related to Groovy, but I'm doing weird things with the JVM
> for a compiler plugin and I've hit a knowledge barrier I can't pass. Since you
> guys are the experts on "doing weird stuff with the JVM", I was hoping you
> might be willing to help?
> My goal is to copy existing methods, transform them, and link to those
> transformed versions with InvokeDynamic. Basically the template system from
> C++, but done at runtime.
> I've got all of the groundwork laid to do this, but I'm having trouble with
> access violations in my clones.
> Since I'm *cloning* methods, they'll be generated into a class other than the
> one being cloned. But then since I'm cloning *methods*, they need to be able to
> access PRIVATE MEMBERS of the original instance.
> I tried generating public bridge methods, but private types still break that. If
> possible, I'd like to just make my clone a "friend" of the original class like
> in C++, but I don't know how.
> I fully control the classes that clones can be made from, so I can mark anything
> else as an "inner class" or "nestmate" as needed. I just can't figure out what
> each of those ALLOW on the JVM from the specification docs alone. And you know
> how helpful google is nowadays; I couldn't find anything no matter how I
> phrased it.
> Does anyone know how to do this?
The class that encapsulates your clone should be an hidden class (this is the mechanism used to load lambda bodies)
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte[],boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption...)
> Thank you so much for your help!
regards,
Rémi