1

I have the following code

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

I would like to add the annotation Deployment, method createDeployment, class BaseIT without changing any kind of implementation

Diego
  • 413
  • 3
  • 14

1 Answers1

2

Well, later I found Add method annotation at runtime with Byte Buddy

         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>
Diego
  • 413
  • 3
  • 14