Java 强制ByteBuddy MethodDelegation中的显式方法解析以允许使用lambda

Java 强制ByteBuddy MethodDelegation中的显式方法解析以允许使用lambda,java,byte-buddy,Java,Byte Buddy,我正在使用ByteBuddy在运行时实现一个标记接口和任意数量的访问器,如无参数值方法,由标记注释标识,如: interface Foo { // marker interface } // this is the kind of thing we're generating implementations of interface MyFoo extends Foo { @Value SomeClass bar(); } interface Implementation<

我正在使用ByteBuddy在运行时实现一个标记接口和任意数量的访问器,如无参数值方法,由标记注释标识,如:

interface Foo {
  // marker interface
}

// this is the kind of thing we're generating implementations of
interface MyFoo extends Foo {
  @Value
  SomeClass bar();
}
interface Implementation<F extends Foo, V> {
  @RuntimeType
  V apply(@This F foo);
}
我为实现委托提供了一个功能接口,类似于:

interface Foo {
  // marker interface
}

// this is the kind of thing we're generating implementations of
interface MyFoo extends Foo {
  @Value
  SomeClass bar();
}
interface Implementation<F extends Foo, V> {
  @RuntimeType
  V apply(@This F foo);
}
但是,如果我将lambda扩展为一个抽象类并重新注释,它会起作用:

class SomeFactory implements ImplFactory<?XYZ> {
  <F extends Foo> Implementation<F, ?XYZ> implFor(Method m) {
      return new Implementation<F, ?XYZ>() {
          @Override
          @RuntimeType
          ?XYZ apply(@This F foo) {
              /* ...shenanigans... */
          }
      }
  }
} 

class SomeFactory实现ImplFactory方法重写在运行时并不真正存在。由于许多原因,方法、字段或接口上的注释不会被继承,并且Byte Buddy不会扫描继承层次结构以完成任务

我建议您为具有注释的指令插入使用委托类,并将实际目标作为参数来解决此问题

class SomeFactory implements ImplFactory<?XYZ> {
  <F extends Foo> Implementation<F, ?XYZ> implFor(Method m) {
      return new Implementation<F, ?XYZ>() {
          @Override
          @RuntimeType
          ?XYZ apply(@This F foo) {
              /* ...shenanigans... */
          }
      }
  }
}