Redrield
Redrield

Reputation: 329

Rebase method intercept, completely overriding method

I'm trying to do some runtime code changing using ByteBuddy. The problem that I've run into is that when I try to rebase a method to add an intercept call at the end, the entire method gets replaced and there isn't any $original() method declared and called after.

This is the code I'm using for the proper transformation

ByteBuddy()
    .rebase(clazz)
     .method(ElementMatchers.named("onEnable"))
     .intercept(MethodDelegation.to(TestInjector()))
     .make()
     .saveIn(dataFolder)

clazz and dataFolder are defined above this and aren't the source of the problem.

This is the signature of the injector method:

public void intercept(@This JavaPlugin pl)

In analyzing the code it outputs, the class is identical to pre-transformation, but all the code that was getting called in the onEnable method has been removed, and replaced with the single method delegate. Is there a way, using a method like this, for me to keep the existing method body, but add a delegate call to the head of the method?

Upvotes: 0

Views: 387

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

If you want to invoke the original method, you can chain a SuperMethodCall to the interceptor using "andThen". This instructs Byte Buddy to invoke the original method after the delegation call.

Upvotes: 1

Related Questions