| I went through Enhancer and I think it should not be overly difficult to make this part of the BytecodeProvider SPI as in BytecodeProvider#getEnhancer. Furthermore I believe it should be pretty straightforward (though probably a lot of work) to create a Byte Buddy-backed Enhancer impl. Of course the devil is in the details, but it looks like the actual runtime code for enhancement is pretty well isolated from Javassist. One thing we'd have to work out is that as-is Enhancer is kind of open-ended. We simply ask the Enhancer to enhance this class or that class. It is up to the Enhancer itself to decide on what type(s) of enhancement to do as well as how to perform that enhancement. I wonder if instead we should design this such that we have another contract into the enhancement process that comes from the BytecodeProvider. Enhancer would then act as the coordinator, calling out to that BytecodeProvider delegate as needed. For the sake of illustrating, let's call this delegate EnhancementCapabilities. The idea would be that there are certain capabilities we need to know how to enhance into the class; e.g. if we are adding lazy loading capabilities we need the ability to:
- add a field to hold the LazyPropertyInitializer
- add a "getter" to be able to inject the LazyPropertyInitializer
- add a "setter" to be able to access the associated LazyPropertyInitializer
- mutate any getter and setter calls for persistent "state fields" to use the LazyPropertyInitializer
Each of those would be a "capability" exposed by EnhancementCapabilities. E.g.:
public interface EnhancementCapabilities {
SomeFieldPointer addLazyPropertyInitializerField();
SomeMethodPointer addLazyPropertyInitializerGetter();
SomeMethodPointer addLazyPropertyInitializerSetter();
void mutatePersistentStateFieldAcess(SomeFieldPointer lazyPropertyInitializerField, ... )
...
}
That is pretty straight-forward in terms of implementing that in Javassist. As I understand it though, Byte Buddy works on top of ASM and so I wonder if this approach is reasonably doable in a ByteBuddyBytecodeProvider. Again the idea is to isolate just the differences between using Javassist and Byte Buddy for enhancement; the actual coordination of enhancement would ideally be common. |