raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

Instrumenting two methods with the same Advice class #1568

Closed rupinder10 closed 6 days ago

rupinder10 commented 10 months ago

I have a class with two similar methods but with a different set of arguments. For example these are two methods:

public static void sendMessage(String id, String message)
public static void sendMessage(String message)

The Advice class for instrumenting the first method is:

 @Advice.OnMethodEnter(suppress = Throwable.class)
  public static void onEnter(@Advice.Argument(1) String message) throws Throwable {
        //TODO: Instrumenting logic here
  }

The instrumenting logic is the same for the second method but the argument number is 2 instead of 1. Is there a way for me to use the same Advice class for both methods or do I have to duplicate the code with just a different argument number ?

raphw commented 10 months ago

You can register a custom annotation that binds the argument differently. Have a look at Advice.withCustomBinding().

rupinder10 commented 10 months ago

Did you mean Advice.withCustomMapping() ?


From: Rafael Winterhalter @.> Sent: Sunday, December 3, 2023 11:29 AM To: raphw/byte-buddy @.> Cc: Rupinder Singh @.>; Author @.> Subject: Re: [raphw/byte-buddy] Instrumenting two methods with the same Advice class (Issue #1568)

You can register a custom annotation that binds the argument differently. Have a look at Advice.withCustomBinding().

— Reply to this email directly, view it on GitHubhttps://github.com/raphw/byte-buddy/issues/1568#issuecomment-1837531593, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB56UZY7O3HDUII76NX7ZDDYHSSG5AVCNFSM6AAAAABAE2OSUCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZXGUZTCNJZGM. You are receiving this because you authored the thread.Message ID: @.***>

raphw commented 9 months ago

Yes, indeed.