简体   繁体   English

ByteBuddy静态方法拦截@Origin方法

[英]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.我在“origin”中接收了新的拦截器方法而不是原始方法,从而导致无限递归。 Am I missing something or is this a bug ?我错过了什么还是这是一个错误?

By calling the @Origin method, you are invoking the same method that is currently executing.通过调用@Origin方法,您正在调用当前正在执行的相同方法。 Byte Buddy instruments a method foo by changing a class: Byte Buddy 通过更改类来检测方法foo

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.如果您想获取原始注释,可以使用@SuperMethod注释。 It is however more recommended to use the @SuperCall or @Morph annotations.然而,更推荐使用@SuperCall@Morph注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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