Sharing Advice.Locals across multiple Advices
In our project we are currently writing a Java agent and use ByteBuddy to perform some configurable instrumentation. Based on the configuration we apply one or multiple Advices to methods. The problem is now that these Advices would like to share some method local data. Whenever we decide to instrument a method, we first apply an Advice which looks like this:
public class MyContextAdvice {
@Advice.OnMethodEnter
public static void onEnter(@Advice.Local("context") bootstrap.MyContext context) {
context = bootstrap.MyContext.enterNewContext();
}
@Advice.OnMethodExit
public static void onExit(@Advice.Local("context") bootstrap.MyContex context) {
context.close();
}
}The other Advices which are then applied to the same method would like to access the "context" generated by MyContextAdvice . As I see it this is currently not possible using Advice.Local as all Advices are translated to individual ASMVisitorWrappers which have no knowledge of each other.
Is there any other way of realizing this behaviour or do we have to fallback to using ASM?
As the code is performance critical I would like to avoid "hacks" such as maintaining a custom stack using ThreadLocals.
Source: raphw/byte-buddy