1
votes

I'm trying to use byte Buddy with bigger application. My point for now is just to log something out on method enter/exit with @Advice. My agent attach to application correctly and is built. In logs I can also see that the transformation of pointed class is also done. Problem is when I'm sending request on needed RestEndpoint and method is called I get error :

javax.servlet.ServletException: A MultiException has 1 exceptions.  They are:
1. java.lang.NoClassDefFoundError: com/agent/MyAdviser

at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:391)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)

My agent :

LOG.info("Before Agent Builder build !!!");
          new AgentBuilder.Default()
                  .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
                    .type(is(MyClassToCatch.class))
                  .transform(
                        new AgentBuilder.Transformer.ForAdvice()
                        .include(MyAgent.class.getClassLoader())
                        .advice(ElementMatchers.any(), MyAdviser.class.getName())
                )
                .installOn(inst);

And MyAdviser.class is :

public class MyAdviser {

private static final Logger LOG = LoggerFactory.getLogger(MyAdviser.class);

@Advice.OnMethodEnter
public static void onEnterExit() {
    LOG.info("INTERCEPTED BBB <<<>>> BBB");
}

Is the problem connected to classloader somehow ? BR,

Rafael's solution helped.

EDIT: I've tried also to intercept a method, and just call it without any changes but I've ended with an error like this :

com.agent.DiscoveryAgent - On Error of : MyClassToCatch       None of [net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@7815f1c, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@a193f70f, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@d6fdc355, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@66e2275b, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@19cb065f, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@4fc13971, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@aea74e0e, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@2ac04890, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@f5eef57c, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@e1b04a0f, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$Record@f0de1c86] allows for delegation from public javax.ws.rs.core.Response MyClassToCatch.someMethod()
1

1 Answers

0
votes

The code in the advice class is copied into the target class. This means, that it needs to be able to resolve the MyAdviser class. If this is not possible, you have to:

  1. Define the Logger as a field on the transformed class via defineField and the @FieldValue annotation.
  2. Read the logger dynamically from within the advice and reference another class that is visible as the log root.