Pass an unique value per method to interceptors

33 views
Skip to first unread message

Keshavan Santhanam

unread,
Oct 9, 2023, 6:45:18 AM10/9/23
to Byte Buddy
Hi,

We need to generate an unique id for every method to be intercepted, that will be used in the interceptors (for recording code coverage).

One way to do this is in the interceptor method using Advice.Origin annotated method object. However, this creates overhead for every method intercepted that involves creating a murmur 32 hash. Even if we have to do it once and keep a map, it involves access to a map every time the intercepted method is called in addition to lots of memory.

So, the better way to execute the unique id generation logic (murmur32 hash) is during transformation in java agent and avoid spending the same effort in the interceptor methods.

This was easy to do in javassist where we just give some text built using the unique id which will be compiled and injected.

However, as an average joe, I am finding it a bit difficult to achieve the same using byte buddy. Could not find much documentation other than what is available in the source code, and a few stack overflow questions.

I need a mechanism for the transformer where-in,
1. We can iterate over declared methods in a class
2. For each method, generate an unique id.
3. Create an associated MethodCall instance that can be used to call the static interceptor method with the unique id as an argument, and associate the same with the method.

I read about multiple answers that suggested using withCustomMapping with an user defined annotation to set values. However, we do not have an API to specify a custom value at method level (or I am missing something here).

If nothing works, we have to build a clumsy approach to add constants one for every method in the class, that will be set to it's associated unique value, and refer to these constants reflectively in the interceptor method. However, I am sure there will be something more elegant than this.

Any guidance would be greatly appreciated.

Thanks for the great tool!
-Keshavan

Rafael Winterhalter

unread,
Oct 9, 2023, 7:46:57 AM10/9/23
to Keshavan Santhanam, Byte Buddy
Ypu do have. Look into the binding API. It is created during instrumentation based on the instrumented method and later fixed to the constant pool.

--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/byte-buddy/38ceca82-25b7-4f44-9182-d9a001ff237dn%40googlegroups.com.

Keshavan Santhanam

unread,
Oct 9, 2023, 1:38:36 PM10/9/23
to Byte Buddy
Thanks... that worked.
Reply all
Reply to author
Forward
0 new messages