I am just learning byte buddy, and want to create the simplest getter invoker, but, unfortunately could find how to do it. So, the problem:
I have a lot classes, that I scan and if I find in a class a field that is annotated with FooAnnotation
annotation I must be able to get the value of this field using this class instance getter method.
This is my code:
interface GetterInvoker<T, S> {
//this method must `return instance.getterMethod()`;
S getValueFrom(T instance);
}
abstract class AsbtractGetterInvoker implements GetterInvoker {
//...some logic
}
Main code
//input
Class<?> scannedClass = ...
Field annotatedField = ....
Method getterMethod = ....
//generating class
Class<?> tempClass = new ByteBuddy()
.subclass(AsbtractGetterInvoker.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS)
.method(named("getValueFrom").and(takesArguments(1)))
.intercept(???????)
.make()
.load(getClass().getClassLoader())
.getLoaded();
Could anyone say how to do it?
You'd create such an implementation using MethodCall
which allows you to do exactly this.