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

Is it possible to lazily instantiate AllArguments? #1643

Closed dogourd closed 6 days ago

dogourd commented 4 months ago

I have a more general Advice that looks like this:

public class Advice {
    @OnMethodEnter
    public static void before(@AllArguments Object[] args) {
         if (xxxx) {
             return;
         }
         if (xxxx) {
             return;
         }
         // use the args.
         Arrays.toString(args);
    }
}

There are many logical branches inside Advice that do not pay attention to parameters information, but the generated bytecode will always copy the original parameter list into an array and assign it to the args parameter.

Can I use bytebuddy to implement lazy instantiation of AllArguments? Make sure I only actually create the target array when I'm actually going to use it.

raphw commented 6 days ago

Sorry, I missed that. The arguments are only virtual and therewith lazy by definition. Each read will create a new array. The following expression would therefore be false:

args == args