Sam Pullara Sam Pullara - 1 year ago 81
Java Question

How do I add Advice to "goto" byte codes in Byte Buddy?

I'm building a system that needs to run 3rd party code in Java 8 Nashorn and I'd like to monitor / control CPU and memory use. I've got a Byte Buddy agent that adds advice around Javascript method enters and exits but because loops are compiled down to byte code, I need to also augment any goto bytecode that goes backwards. It doesn't seem like something it currently supports trivially, but I can imagine that it has the capability to do it. Should I use it or write the low-level ASM code to do it myself?

Answer Source

I am the developer of Byte Buddy and I do not plan to support something like in-method byte code manipulation and here are the reasons:

For manipulating byte code within a method, Byte Buddy exposes its underlying ASM API which (to me) feels like the best way of dealing with such detailed editing. The Advice component makes some assumptions such as an empty operand stack and overridable frames for local variables besides the method's parameters. All this makes it non-trivial to port the Advice component to be triggered within a method rather than before and after it.

I consider to add some other component for replacing any in-method method call with a dispatcher invocation at some point (similar to Javassists limited in-method replacement) but not in the near future.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download