mardi 1 octobre 2019

Guice binding reflected Class?> to a Method without knowing them at compile time

Let's say I have the follwoing as part of a Guice Module:

Method method = serviceClass.getMethod(methodName, Channel.class);
assert(Modifier.isStatic(method.getModifiers()));
Class<?> returnType = method.getReturnType();
bind(returnType).toProvider(?????)

What can I put there so that the provider calls method with the Channel parameter being injected. For example, if I knew the type at compile time, I would make something like:

@Provides
SomeStubType providesSomeStubType(Channel channel) {
    return SomeServiceClass.newStub(channel);
}

However, both SomeServiceClass, SomeStubType, and the newStub methods are known only when the module is being constructed via reflection. Further, SomeServiceClass is generated by the gRPC compiler and does not implement any special interface, although I could create a wrapper if needed.





Aucun commentaire:

Enregistrer un commentaire