byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

Graal support - reflection and runtime loading

Open raphw opened this issue 4 years ago • 8 comments

Add support for running Byte Buddy on Graal native image by offering a runtime mode where generated classes are cached in a location and retreived in a different runtime mode such that no runtime code generation needs to take place.

The final API should ideally not require any changes to make it automatically applicable for any Byte Buddy user, direct or indirect.

Also, files for required reflection should be added as a convenience to support the runtime.

raphw avatar Nov 21 '19 09:11 raphw

Probably the target milestone is no longer valid. Do you mind changing it or removing it altogether?

gruenich avatar Aug 04 '21 12:08 gruenich

I abuse the field to note the report version. Sorry for the confusion.

raphw avatar Aug 04 '21 16:08 raphw

Hi Rafael,

I was also looking for a similar solution, as we are having a lot of Mockito tests that we are trying to run with Graal's new JUnit native support. Mockito uses ByteBuddy to generate classes at runtime. So, I found this issue here.

Investigating further, I saw that dynamic class loading is a PR in the Graal project that has recently been merged:

https://github.com/oracle/graal/pull/2442

So maybe, ByteBuddy can leverage the upcoming functionality of Graal to realize this feature. What do you think?

dthommes avatar Aug 11 '21 11:08 dthommes

Interesting. This would require that the default naming strategy is deterministic what is certainly possible, but it would likely also require reflection to work but this should be picked up by the agent. I will certainly look into this!

raphw avatar Aug 11 '21 11:08 raphw

Byte Buddy can now kind of work with this, as long as class names are deterministic. There are still some rough edges but we are moving forward, at least!

raphw avatar Nov 02 '21 22:11 raphw

@raphw Pretty awesome to see this working out of the box if collecting predefined classes using an agent run 🎉

Though, I noticed predefined classes are not collected if the dynamic type is loaded using ClassLoadingStrategy.UsingLookup.of(MethodHandles$Lookup). Is there any way to get this to work or continue using the old INJECTION strategy on Java 11 with the GraalVM agent?

mosche avatar Dec 06 '22 10:12 mosche

This would be a bug in Graal if so. Byte Buddy only adjiusts its default class naming strategy to work with the Graal agent out of the box. It's Graal that is responsible of collecting class files.

raphw avatar Dec 07 '22 10:12 raphw

Thanks @raphw! In deed, classes injected into the system class loader are skipped (https://github.com/oracle/graal/issues/4248). Unfortunately, that's what will typically happen when defining a class using a lookup :/

mosche avatar Dec 07 '22 14:12 mosche