I would like to use a dynamic proxy using Proxy.newProxyInstance
for a generated classes (Swagger) and these are not implementing an interface, which brought me to ByteBuddy, using which it should allow me to build the interface required by Proxy.newProxyInstance
.
I got so far:
Class<?> restClass = RestServiceApi.class;
Builder<?> builder = byteBuddy.makeInterface().merge(Visibility.PUBLIC).name(restClass.getName() + "I");
for (Method method : restClass.getMethods()) {
builder = builder.defineMethod(method.getName(),
HOW_TO_DO_THIS);
}
Class<?> restInterface = builder.make().load(this.getClass().getClassLoader()).getLoaded();
Class<?>[] proxyInterfaces = new Class<?>[] { restInterface };
// TODO create manipulatedRestServiceApiThatImplementsRestInterfaceI
asyncServiceProxy = new AsynchronousServiceProxy<>(RestServiceApi.class, errorHandler, guiBlockingListener);
ctrAsyncService = (manipulatedRestServiceApiThatImplementsRestInterfaceI) Proxy.newProxyInstance(RestServiceApi.class.getClassLoader(), proxyInterfaces, asyncServiceProxy);
Even if can figure out, how to write the code required to to define each method, I get the feeling, that I'm not doing it in the correct way, that I'm going to write a massive amount of code to translate the reflection information of Method
to whatever ByteBudd requires and I suspect there is an easier way to build this interface class.
I would very much appreciate if anyone can point me into the correct direction.