Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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>
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.