Java 使用ByteBuddy的条件方法委托

Java 使用ByteBuddy的条件方法委托,java,byte-buddy,Java,Byte Buddy,是否可以在bytebuddy的方法委派调用中执行条件调用?假设我们有以下情况: Method serviceMethod = serviceHandler.getClass() .getDeclaredMethod(methodName, String.class, String.class, Object.class); this.serviceHandler= byteBuddy.subclass(serviceHandler.getClass()).meth

是否可以在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)

不,这是不可能的,除非您实现自己的实现。条件代码很快就会变得复杂。Byte Buddy的目标是生成尽可能少的代码


如果您想避免委派,可以使用字节码模板建议。

非常感谢您的回答。当Method.invoke应该为另一个类中的方法执行时,我们需要该方法的委托字段,对吗?如果不这样做,我会得到一个illegalstateexception无法调用方法。。。。。