Bernado
Bernado

Reputation: 628

Conditional method delegation using ByteBuddy

Is it possible to do a conditional call within bytebuddy's method delegation call? Suppose we have the following case:

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();

Can we do something like "only if super method call returns true then call subclasses method"? That would be a conditioned "andThen":

intercept(SuperMethodCall.INSTANCE.**andThenIfConditionFullfilled**(MethodCall.invoke(handleMethod)

Upvotes: 0

Views: 278

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44067

No, it is not possible, unless if you implement your own Implementation. Conditional code quickly gets complicated. Byte Buddy is aiming for generating as little code as possible.

Possibly, use Advice for a byte code template if you wanted to avoid delegation.

Upvotes: 1

Related Questions