Clarification needed on AgentBuilder.Transformer.ForAdvice::include()
See original GitHub issueI’m trying to capture a call coming in through a servlet engine. Here’s my code:
@Advice.OnMethodEnter
public static void enter(final @Advice.AllArguments Object[] args) {
GenericIngressHandler.enter(args[0], "getHeader");
}
@Advice.OnMethodExit
public static void exit() {
GenericIngressHandler.exit();
}
public static AgentBuilder buildAgent(AgentBuilder b) {
return b.type(named("javax.servlet.http.HttpServlet"))
.transform(
new AgentBuilder.Transformer.ForAdvice()
.include(ServletIngressHandler.class.getClassLoader())
.advice(new Matchers.WildcardMethodMatcher(
"service(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)"),
ServletIngressHandler.class.getName()));
}
}
It fails when it’s trying to load the GenericIngressHandler when executed through the Open Liberty appserver. It seems that it has some custom classloader that doesn’t see my code.
So I added include(ServletIngressHandler.class.getClassLoader()) to the definition of the transformer thinking that it would somehow insert the classloader of my package into the mix. However, it still can’t see the class. Maybe I’m using include() wrong? Some clarification in the documentation would be great!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Clarification on include a classloader - java - Stack Overflow
I am trying to create an Advice for sun.nio.ch.NioSocketImpl, which is in Platform classloader, using ForAdvice#include to be able to use my own ......
Read more >AgentBuilder.Transformer (Byte Buddy (without ... - Javadoc.io
A transformer allows to apply modifications to a DynamicType . Such a modification is then applied to any instrumented type that was matched...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Any code referenced in your advice classes must be available to the class loader that is executing the advice. The include method only assures that Byte Buddy can see class files provided by class loaders while inlining the advice.
Normally, you add classes that you need to be universally visible to the bootstrap class loader. The
Instrumentationinterface provides a method for that.Good to hear, checked your doc and this approach should also allow you avoid - Xbootclassloader.