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

Support for accessing fiels on fields/method calls

Open avshenuk opened this issue 4 years ago • 3 comments

Currently, there is no way to do a chain of field accesses. Like it is possible with method calls e.g.

.withMethodCall(MethodCall.invoke(...).onMethodCall())

So for instance, if I want to do something like

.withField("someFieldName").onMethodCall()

or

.withField("someFieldName").onField()

There is an option to provide a FieldLocator.Factory but it only operates within the current class.

There is one way it can be done in my mind, it's by using FieldAccessor e.g. like

.withMethodCall(FieldAccessor.ofField("someFieldName"))

and hence it can be chained as any other MethodCall. But the problem is that FieldAccessor is an Implementation, not MethodCall :)

avshenuk avatar Nov 11 '21 14:11 avshenuk

Yeah, I have been experimenting a bit with better DSLs but the complexity quickly explodes, so I am always wondering if a custom built StackManipulation is not an easier option in many of the more complex cases. Often, a MethodDelegation can also be a good substitute as it is easier to debug.

I'll review this another time, though, to see if there are some common patterns that are not supported. I cannot promise you a time-line for now, though, so I'd recommend you a custom stack manipulation or delegation for now.

raphw avatar Nov 11 '21 20:11 raphw

@raphw Yeah, no rush. Thanks for spending time on this.

I will look into writing some custom stack manipulation. Am I right in assuming that it's easiest by making my own Implementation and ByteCodeAdapter?

avshenuk avatar Nov 11 '21 21:11 avshenuk

Exactly. It really depends on how dynamic you want those things to be. You can create a custom implementation and resolve names and properties from each instrumented method. Or you can hardcode it in the stack manipulations using Implementation.Simple.

raphw avatar Nov 11 '21 21:11 raphw