[英]Conditional method delegation using ByteBuddy
是否可以在 bytebuddy 的方法委托調用中進行條件調用? 假設我們有以下情況:
Method serviceMethod = serviceHandler.getClass()
.getDeclaredMethod(methodName, String.class, String.class, Object.class);
this.serviceHandler= byteBuddy.subclass(serviceHandler.getClass()).method(ElementMatchers.named("handleService"))
.intercept(SuperMethodCall.INSTANCE.andThen(MethodCall.invoke(handleMethod).withArgument(0, 1, 2))).make().load(getClass().getClassLoader()).getLoaded().newInstance();
我們可以做類似“只有當超級方法調用返回true然后調用子類方法”之類的事情? 那將是一個有條件的“然后”:
intercept(SuperMethodCall.INSTANCE.**andThenIfConditionFullfilled**(MethodCall.invoke(handleMethod)
不,這是不可能的,除非您實現自己的Implementation
。 條件代碼很快變得復雜。 Byte Buddy 的目標是生成盡可能少的代碼。
如果您想避免委托,可以使用Advice
作為字節碼模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.