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

How to intercept all the classes in a project which has isolated classloaders

Open all4you opened this issue 3 years ago • 7 comments

Hi, I am using ByteBuddy to create my SDK to monitor the business logic, the SDK will be a dependency in other applications. I have meet a problem that some application has isolated classloaders to load classes which cause the intercept failure. I know that is a classLoader issue but I don't know how to solve it.

The application has isolated classloaders like blow: image image

The SDK runs within the application(not a agent way), and the application has a container that load the platform classes with ContainerClassLoader, and the container has some service jars, each jar has a ServiceClassLoader to load the classes in the jar. The SDK classes is loaded by AppClassLoader or ContainerClassLoader.

My question is how to intercept all the classes in the Service jars in my SDK without IllegalAccessError.

all4you avatar Apr 25 '22 13:04 all4you

That it was Advice is for, it inlines the code into the targeted class. You can the call out to a dispatcher, normally a class that is injected into the bootstrap loader.

If you look for my name on YouTube, I have introduced this solution in talks.

raphw avatar Apr 25 '22 20:04 raphw

Thank you, I will now go search the content in YouTube. Can you give me the link where you talked the solution please?

all4you avatar Apr 26 '22 01:04 all4you

Hi,I have searched something that you shared in YouTube image you suggest that use AgentBuilder.Transformer.ForAdvice to support System Classloader but I have a more complex scene, I need to bind some customer annotations like blew: image How do I do that?

all4you avatar Apr 26 '22 06:04 all4you

Provide it to the constructor.

raphw avatar Apr 26 '22 06:04 raphw

I have no idea about 'Provide it to the constructor', can you please show me some code? How to provide the parameters to com.my.interceptor.MyInterceptor ?

builder.visit(new AgentBuilder.Transformer.ForAdvice()
             .include(MyPlugin.class.getClassLoader())
             .advice(elementMatcher, "com.my.interceptor.MyInterceptor")
)

all4you avatar Apr 26 '22 08:04 all4you

AgentBuilder.Transformer.ForAdvice(Advice.withCustomConfiguration());

raphw avatar Apr 26 '22 08:04 raphw

Thank you I will try it.

all4you avatar Apr 26 '22 09:04 all4you