geh
geh

Reputation: 846

ByteBuddy static method interception @Origin method

I have this static method intercepted correctly

 TypeDescription commandBase = TypePool.Default.ofClassPath()
      .describe("poo.laboratoire1.Q2.Application").resolve();
 new ByteBuddy()
      .redefine(commandBase, ClassFileLocator.ForClassLoader.ofClassPath())
      .method(named("obtenirTrame"))
      .intercept(MethodDelegation.to(Mock.class))
      .make()
      .load(ClassLoader.getSystemClassLoader(),  
            ClassReloadingStrategy.Default.INJECTION);

but when I invoke the original method with this interceptor:

 public static boolean[] obtenirTrame(int i, @Origin Method origin){
     ...
    origin.invoke(null, i);
     ...
 }

I receive the new interceptor method in "origin" instead of the original method, resulting in an infinite recursion. Am I missing something or is this a bug ?

Upvotes: 1

Views: 1168

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

By calling the @Origin method, you are invoking the same method that is currently executing. Byte Buddy instruments a method foo by changing a class:

class Bar {
  void foo() { /* some code */ }
}

into

class Bar {
  void foo() { Interceptor.call( ... ); }
  void foo$original() { /* some code */ }
}

You can use the @SuperMethod annotation, if you want to get hold of the original. It is however more recommended to use the @SuperCall or @Morph annotations.

Upvotes: 2

Related Questions