【问题标题】:Class not found exception with byte buddy Java agent找不到字节伙伴 Java 代理的类异常
【发布时间】:2017-07-20 15:38:18
【问题描述】:

我正在尝试将 byte 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,

Rafael 的解决方案有所帮助。

编辑:我也尝试拦截一个方法,然后直接调用它而不做任何更改,但我以这样的错误结束:

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

【问题讨论】:

    标签: javaagents byte-buddy


    【解决方案1】:

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

    1. 通过defineField@FieldValue 注释将Logger 定义为转换后的类上的字段。
    2. 从建议中动态读取记录器并引用另一个作为日志根可见的类。

    【讨论】:

    • 嗨拉斐尔!感谢您的帮助,我终于做到了,并且按预期注销了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多