ByteBuddyAgent .为了保持默认实现,我使用了一种重基策略,但我不知道如何通过调用原始方法来拦截新方法。
ByteBuddyAgent
我试着用 MethodCall.invokeSuper() 和 MethodCall.invokeSelf().onDefault() ,但两者都给我一个 IllegalStateException .
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 通过这种方式,字节伙伴有机会定位实际的超级方法,即重基方法。
SuperMethodCall.INSTANCE
在您的情况下,您只会递归地调用相同的方法。此外 onDefault 配置将尝试调用由实现的接口上的默认方法 MyInterface .
onDefault
MyInterface