I was trying to find usable answer but with no luck. I wonder if it would be possible to use byte-buddy for following: Let's say we have a POJO with number of values. For some specific kind of processing i am interested only in some. I could mark them with an annotation, like @ConditionalData i could put on getters or fields. Then I could create and inteface, lets say NVPProvider which would return map field name - value. Doing this with reflection is possible but it is not too performant. I Was hoping i could use byte buddy to extend the class by the interface and implement method, but i am not really finding how to construct the Implementation to achieve that.
I have went through implementations of:
net.bytebuddy.implementation.Implementation
and tried to search for some examples around web pages, but I haven't recognized the right way to go with.
public interface NVPProvider {
Map<String, Object> getDataAsNVP();
}
public <O> Builder<O> instrumentType(Builder<O> builder) {
builder.implement(NVPProvider.class).method(net.bytebuddy.matcher.ElementMatchers.named("getDataAsNVP")).intercept( ??? );
return builder.implement(NVPProvider.class);
}
I wonder if there is a way that i would iterate the fields and getters and match the Accessible objects by reflectively getting the annotations, however based on this i would be able to compose the interface method implementation iterating the matched fields and contributing to the result map, the imaginary generated code would look like:
Map<String, Object> result = new HashMap<>();
Object obj0 = getValue001();
result.put("getValue001", obj0);
Object ob10 = accesibleField1;
result.put("ob10 ", ob10 );
Later i could add annotation attribute for better looking keys.
I have seen examples with
MethodDelegation.to(interceptor)
however, then I do now see how to do it without reflection.
I have idea how to so it with Javassist, where you can actually can compose pieces of code you later compile, but I am not sure how to do it with byte-buddy. I have used byte-budy for extension of POJO by simple getters dynamically defined in config file and it looks nice. Having just one byte-manipulation tool would be cleaner. Thanks for any advices.