> From: "Caleb" <[email protected]> > To: "dev" <[email protected]> > 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
