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
实现的接口上的默认方法