Advice & constructors - how to copy code right after the super call with @onMethodEnter
Hi,
Can the Advice wrappers from byte buddy be used to add code in a constructor after the super call, on @MethodEnter? Looks like the reference to "This" would always be null in a method enter advice for a constructor. I'd have expected the advice method to be copied after the super call when it is mapping the this reference of the instrumented method.
The AdviceAdapter method visitor from asm.commons let me add an instance method call right after the super call by overriding "onMethodEnter()", but it isn't shaded in bytebuddy. I may be wrong, but visitors from the asm package can't be wrapped with byte buddy's AsmVisitorWrappers because of the package relocation, right ?
Is there a way in byte buddy of enhancing a constructor to add an Implementation between the super call and the original constructor body ? Or should I just shade those classes from asm.commons as well, to be compatible with the asm dependency packaged by byte buddy ?
This is currently not possible, but I would like to add it sometime. The problems I face are as follows:
- The stack might not be empty at this point (solvable by releasing the stack prior to running code, but it's a nightmare to get right, I rage-quit multiple times before).
- The super code might be invoked in a branch of any complexity. (I have not even touched this.)
ASM recomputes the frames to apply this advice. This is rather expensive, often causes class loading, so I hoped that I could avoid it, and conceptually this is possible but I have not yet accomplished it, also since it is not a high-demand feature.
You can use byte-buddy-dep, include your own ASM bits and shade yourself.
Sorry, I did not think of including directly byte-buddy-dep and ASM seperately, this will do for me.
Sounds like hard to implement indeed - the ASM AdviceAdapter works as intended for me, I do not really need this to be implemented in byte buddy in the end.
Thank you for your hard work on byte buddy!