bytebuddy-添加类级注释



我正在尝试使用byte buddy创建java代理,以允许在运行时修改一些类-

FirstSeleniumTest类已经存在-我想添加如下注释:

@org.testng.annotations.Listeners(value = org.deployd.test.TestNgListener.class)
public class FirstSeleniumTest {...

这是我在代理中的预制方法:

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"对于给定的注释是正确的。

任何关于我可能缺少的东西的指针,试图用字节伙伴为已经存在的类创建类级注释。非常感谢。

如果你想定义一个数组属性,你必须这样声明:

AnnotationDescription.Builder.ofType(Listeners.class)
.define("value", new Class<?>[] { TestNgListener.class })
.build();

将起作用。Byte Buddy不像Java编程语言那样将此类数组视为varargs。

在进一步的分析中,我发现注释value排除了该类型的数组与单个值。我能够通过javassist解决这个问题,如下所示:

AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation annotation = new Annotation(Listeners.class.getName(), constPool);
MemberValue[] memberValues = new MemberValue[]{new ClassMemberValue(TestNgListener.class.getName(), constPool)};
ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constPool);
arrayMemberValue.setValue(memberValues);
annotation.addMemberValue("value", arrayMemberValue);
annotationsAttribute.addAnnotation(annotation);
ctClass.getClassFile().addAttribute(annotationsAttribute);

最新更新