Closed rupinder10 closed 1 month ago
After calling installOn
, you will get a ResettableClassFileTransformer
, you can remove the ClassFileTransformer and retransform the classes by calling the reset
method.
You can use AgentBuilder#with(TransformerDecorator)
to specify a decorator where you can get the byte array of the class passed to ClassFileTransformer. You can also use net.bytebuddy.dynamic.DynamicType.Builder.make().getBytes()
in the implementation of net.bytebuddy.agent.builder.AgentBuilder.Transformer#transform
to get it, depending on your needs
I will try those. Just to clarify for the reset you means something like this ?
When I want to instrument :
ResettableClassFileTransformer resettableClassFileTransformer = agentBuilder.installOn(instrumentation);
and then we I want to remove the instrumentation :
instrumentation.removeTransformer(resettableClassFileTransformer);
resettableClassFileTransformer.reset(instrumentation, RedefinitionStrategy.RETRANSFORMATION);
Another question. Is there a way for me to check the results of a transformation to determine whether the class was changed at all ? Using your suggestions I can get the bytes before and after to compare. but is there a simpler approach ?
The first line will break the second. With reset, you undo the instrumentation by transforming back the classes. By removing the transformer, you will simply no longer transform classes later.
Thanks
I have migrated from ASM to ByteBuddy and have been able to replace by ClassFileTransformer to use Advice instead. In my old code, I used to be able to add and remove instrumentation using a switch. It was roughly like this:
The corresponding code with ByteBuddy that works:
But there are two aspects I need help with: