byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

Feature request: customize some details about static fields used to store references

Open ljnelson opened this issue 5 years ago • 1 comments

Hello; thanks for a great product. I know from personal experience how hard it is to run an open source project and this one is just fantastic. Thanks for all the time you put into it.

I had a request for what I hope is a small feature. Currently, there is a nice little feature of MethodCall where you can specify that it should be supplied with existing object references. I confess that this works like magic for me (I haven't dug into the code to see how it is accomplished, and I'm not sure how it differs from the with(Object...) call…)!

The references, it turns out, are stored in static fields inside the generated class (I was surprised to note these fields are not final…). That of course makes sense.

The fields used to store these references are named something like this: methodCall$4BJ1aAKz. I think that the name is generated via a constant prefix and RandomString#make().

While I can see that I could probably somehow supply my own ArgumentLoader machinery to customize this, it would be nice if there were an easy way in the API/DSL to specify how these reference-storing static fields should be named and decorated.

ljnelson avatar Sep 17 '20 00:09 ljnelson

Thanks, your feedback is very appreciated.

There is already a facility to change the name of synthetic methods Byte Buddy generates for dispatching. It makes sense to supply a similar mechanism for random field names. I'll need to see how this can be done in a consistent manner but I see how this makes sense and if it is meaningful to add.

What you can however do already today: Simply define a static field on the dynamic type yourself using .defineField. Then dispatch the method call to this field using the MethodCall facility. Finally, add a LoadedTypeInitializer where you set the field's value. It's three lines of code instead of one but it already allows for the flexibility you are asking for. I'll look into making this more convenient.

raphw avatar Sep 17 '20 06:09 raphw