Java 如何使用ByteBuddy重新设置接口默认方法的基础?

Java 如何使用ByteBuddy重新设置接口默认方法的基础?,java,bytecode,instrumentation,byte-buddy,Java,Bytecode,Instrumentation,Byte Buddy,我试图在运行时使用ByteBuddyAgent注释默认方法。为了保持默认实现,我使用了重定基址策略,但我不知道如何通过调用原始方法来拦截新方法 我尝试使用MethodCall.invokeSuper()和MethodCall.invokeSelf().onDefault(),但两者都给我一个IllegalStateException new ByteBuddy() .subclass(MyInterface.class) .method(isDeclaredBy(typeDescription)

我试图在运行时使用
ByteBuddyAgent
注释默认方法。为了保持默认实现,我使用了重定基址策略,但我不知道如何通过调用原始方法来拦截新方法

我尝试使用
MethodCall.invokeSuper()
MethodCall.invokeSelf().onDefault()
,但两者都给我一个
IllegalStateException

new ByteBuddy()
.subclass(MyInterface.class)
.method(isDeclaredBy(typeDescription).and(isDefaultMethod()))
    .intercept(MethodCall.invokeSelf().onDefault())
    .annotateMethod(AnnotationDescription.Builder
        .ofType(MyAnnotation.class).build())
.make()
...

您需要使用
SuperMethodCall.INSTANCE
。这样,Byte Buddy就有机会找到实际的超级方法,即重基方法

在您的情况下,您只会递归地调用相同的方法。另外,
onDefault
配置将尝试调用由
MyInterface
实现的接口上的默认方法