Skip to content
Advertisement

Is there any way to call advice() on a loaded class’s methods before the ByteBuddy agent?

I have implemented a simple agent as below. It works for my custom Foo.class, but I couldn’t assign advice for the java.net.URL class.

Example test code;

JavaScript

Is there any specific approach to binding advice‘s java.net.URL class methods so that the class is loaded before the ByteBuddy agent?

Advertisement

Answer

Rafael is right, as usual. I want to elaborate on your question a bit more, though: You are having a bootstrapping problem here:

  • The advice needs to be on the bootstrap class path, otherwise it cannot be used with the bootstrap class URL.
  • The advice needs ByteBuddy because of the annotations. So BB also has to be on the bootstrap class path.
  • If you put your sample code into a single class and also use the BB agent library from there, that one also needs to be on the bootstrap class path. I.e. if you want to run this code
JavaScript

you need to add something like this on the Java command line:

JavaScript

Then you get this output:

JavaScript

As an alternative, you can use a spring-board Java agent which dynamically puts BB and your transformer onto the bootstrap class path without directly referring to any of their classes. You can subsequently kick off transformation via reflection.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement