2016-12-03 35 views
1

我想在运行时使用ByteBuddyAgent注释默认方法。为了保持默认实现,我正在使用重定位策略,但我无法弄清楚如何通过调用原始方法来拦截新方法。如何重新绑定ByteBuddy的接口默认方法?

我试过使用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() 
... 

回答

1

您需要使用SuperMethodCall.INSTANCE。这样,Byte Buddy有机会找到实际的超级方法,这是重新设计的方法。

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