是否可以在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,然后调用子类方法"?这就是一个条件式的 "andThen"。
intercept(SuperMethodCall.INSTANCE.**andThenIfConditionFullfilled**(MethodCall.invoke(handleMethod)
不,这是不可能的,除非你实现了你自己的 Implementation
. 条件代码很快就会变得复杂。Byte Buddy的目标是尽可能少地生成代码。
可能的话,使用 Advice
的字节码模板,如果你想避免授权。