使用byte buddy Java代理找不到类的类

时间:2017-03-01 10:42:37

标签: javaagents byte-buddy

我正在尝试使用更大的应用程序的字节Buddy。我现在的观点是仅使用@Advice在方法输入/退出时记录一些内容。我的代理正确附加到应用程序并构建。在日志中我还可以看到指向类的转换也已完成。问题是当我在所需的RestEndpoint上发送请求并调用方法时我得到错误:

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)

我的经纪人:

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);

MyAdviser.class是:

public class MyAdviser {

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

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

问题是否以某种方式连接到类加载器? BR,

拉斐尔的解决方案有所帮助。

编辑:我也尝试拦截一个方法,只是在没有任何更改的情况下调用它,但我最终得到了这样的错误:

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 个答案:

答案 0 :(得分:0)

advice类中的代码被复制到目标类中。这意味着,它需要能够解析MyAdviser类。如果无法做到这一点,您必须:

  1. 通过LoggerdefineField注释将@FieldValue定义为已转换类的字段。
  2. 从通知中动态读取记录器,并引用另一个作为日志根目录可见的类。