使用 byte buddy 为静态方法添加注解
add annotation to static method with byte buddy
我有以下代码
public class InstrumentedArquillian extends BlockJUnit4ClassRunner {
static {
net.bytebuddy.agent.ByteBuddyAgent.install();
new ByteBuddy()
.redefine(BaseIT.class)
.method(named("createDeployment"))
.intercept(???)
.annotateMethod(AnnotationDescription.Builder.ofType(Deployment.class).build())
.make()
.load(InstrumentedArquillian.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()).getLoaded();
}
}
public class BaseIT {
public static WebArchive createDeployment() {
return DeploymentBuilder.war();
}
}
我想添加注释 Deployment
、方法 createDeployment
、class BaseIT
而不更改任何类型的实现
嗯,后来发现
net.bytebuddy.agent.ByteBuddyAgent.install();
Method existingMethod = BaseIT.class.getMethod("createDeployment");
AnnotationDescription annotationDescription = AnnotationDescription.Builder.ofType(Deployment.class).build();
AsmVisitorWrapper visit = new MemberAttributeExtension.ForMethod().annotateMethod(annotationDescription).on(ElementMatchers.anyOf(existingMethod));
new ByteBuddy()
.redefine(BaseIT.class)
.visit(visit)
.make()
.load(InstrumentedArquillian.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()).getLoaded();
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.10.0</version>
</dependency>
我有以下代码
public class InstrumentedArquillian extends BlockJUnit4ClassRunner {
static {
net.bytebuddy.agent.ByteBuddyAgent.install();
new ByteBuddy()
.redefine(BaseIT.class)
.method(named("createDeployment"))
.intercept(???)
.annotateMethod(AnnotationDescription.Builder.ofType(Deployment.class).build())
.make()
.load(InstrumentedArquillian.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()).getLoaded();
}
}
public class BaseIT {
public static WebArchive createDeployment() {
return DeploymentBuilder.war();
}
}
我想添加注释 Deployment
、方法 createDeployment
、class BaseIT
而不更改任何类型的实现
嗯,后来发现
net.bytebuddy.agent.ByteBuddyAgent.install();
Method existingMethod = BaseIT.class.getMethod("createDeployment");
AnnotationDescription annotationDescription = AnnotationDescription.Builder.ofType(Deployment.class).build();
AsmVisitorWrapper visit = new MemberAttributeExtension.ForMethod().annotateMethod(annotationDescription).on(ElementMatchers.anyOf(existingMethod));
new ByteBuddy()
.redefine(BaseIT.class)
.visit(visit)
.make()
.load(InstrumentedArquillian.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()).getLoaded();
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.10.0</version>
</dependency>