【发布时间】:2021-07-02 05:27:45
【问题描述】:
我正在尝试使用 byte-buddy 创建 java 代理,以允许在运行时修改某些类 -
FirstSeleniumTest 类已经存在 - 我想添加如下注释:
@org.testng.annotations.Listeners(value = org.deployd.test.TestNgListener.class)
public class FirstSeleniumTest {...
这是我在代理中的 premain 方法:
new AgentBuilder.Default()
.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.type(ElementMatchers.nameContains("org.deployd"))
.transform((builder, typeDescription, agr3, arg4) -> builder
.annotateType(AnnotationDescription.Builder.ofType(Listeners.class)
.define("value", TestNgListener.class)
.build()))
.with(AgentBuilder.Listener.StreamWriting.toSystemOut())
.installOn(instrumentation);
在执行过程中出现以下错误:
[Byte Buddy] DISCOVERY org.deployd.test.FirstSeleniumTest
[sun.misc.Launcher$AppClassLoader@18b4aac2, null, loaded=false]
[Byte Buddy] ERROR org.deployd.test.FirstSeleniumTest
[sun.misc.Launcher$AppClassLoader@18b4aac2, null, loaded=false]
java.lang.IllegalArgumentException: class org.deployd.agent.TestNgListener cannot be
assigned to value
at net.bytebuddy.description.annotation.AnnotationDescription$Builder.define(AnnotationDescription.java:860)
at net.bytebuddy.description.annotation.AnnotationDescription$Builder.define(AnnotationDescription.java:947)
at net.bytebuddy.description.annotation.AnnotationDescription$Builder.define(AnnotationDescription.java:935)
at org.deployd.agent.TestAgent.lambda$premain$0(TestAgent.java:41)
如果我手动添加注释:
@org.testng.annotations.Listeners(value = org.deployd.test.TestNgListener.class)
public class FirstSeleniumTest {...
那么没有错误 - 意味着值'value'对于给定的注释是正确的。
关于我在尝试为已经退出的类创建带有字节伙伴的类级别注释时可能会丢失的任何指针。谢谢。
【问题讨论】:
标签: byte-buddy javaagents