byte-buddy
byte-buddy copied to clipboard
Set values of fields
Using ByteBuddy I'd like to generate method that would look like this:
public static class TestClass {
public void init(Entity instance, int firstParameter, String secondParameter, ...) {
instance.firstField = firstParameter;
instance.secondField = secondParameter;
instance.<...> = ...
}
}
but I don't really know how to achieve this...
First argument is always instance and rest of arguments is always values that should be assigned to fields (and they're in same order as fields). Normally I would use interceptor, but I would have to set these fields using Reflections. - the whole point is to set these fields values natively like it would be set using normal code
You can use FieldAccessor for this. For example, FieldAccessor.ofField("firstField").setsArgumentAt(1) for the first line.
You can then compose these statements via andThen() on the result.