简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM