messageType = ReflectionUtil.findSubClassParameterType(callback, 0); if (messageType == null) { throw new IllegalStateException("Could not determine EventReceivedCallback message type");
@SuppressWarnings("unchecked") private Message convertJsonToProtobuf(ServiceMethodHandler handler, JsonRpcRequest rpcRequest) throws RpcCallException { try { Class<?> requestKlass = findSubClassParameterType(handler, 0); return ProtobufUtil.jsonToProtobuf(rpcRequest.getParams(), (Class<? extends Message>) requestKlass); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Reflection for handler " + handler.getClass() + " failed"); } catch (RuntimeException ex) { throw new RpcCallException(RpcCallException.Category.BadRequest, "Invalid request"); } }
ServiceMethodHandler handler = registeredHandlers.get(key); Class<? extends Message> requestClass = (Class<? extends Message>) findSubClassParameterType(handler, 0); Class<? extends Message> responseClass = (Class<? extends Message>) findSubClassParameterType(handler, 1); sb.append(requestClass.getSimpleName()); sb.append("\",\"type\":\"");
@Test public void verifyReflection() throws ClassNotFoundException { TestServiceMethodHandler handler = new TestServiceMethodHandler(); assertThat(ReflectionUtil.findSubClassParameterType(handler, 0)). isEqualTo(RpcEnvelope.Request.class); assertThat(ReflectionUtil.findSubClassParameterType(handler, 1)). isEqualTo(RpcEnvelope.Response.class); assertThat(ReflectionUtil.findSubClassParameterType(handler, 2)).isNull(); }
@Test public void verifyReflectionWorksWithAChild() throws ClassNotFoundException { ChildTestServiceMethodHandler handler = new ChildTestServiceMethodHandler(); assertThat(ReflectionUtil.findSubClassParameterType(handler, 0)). isEqualTo(RpcEnvelope.Request.class); assertThat(ReflectionUtil.findSubClassParameterType(handler, 1)). isEqualTo(RpcEnvelope.Response.class); assertThat(ReflectionUtil.findSubClassParameterType(handler, 2)).isNull(); }
@Test public void test_interceptor() throws ClassNotFoundException { Injector injector = Guice.createInjector((Module) binder -> binder.bindInterceptor( Matchers.identicalTo(TestedRpcHandler.class), Matchers.any(), new RpcHandlerMethodInterceptor() )); TestedRpcHandler methodHandler = injector.getInstance(TestedRpcHandler.class); methodHandler.handleRequest(RpcEnvelope.Request.newBuilder().build(), new OrangeContext()); SameGuiceModuleAssertionOnInterceptorInvokation sameGuiceModuleAssertionOnInterceptorInvokation = injector.getInstance(SameGuiceModuleAssertionOnInterceptorInvokation.class); sameGuiceModuleAssertionOnInterceptorInvokation.test_that_intercepted_rpc_handler_still_verified(); assertThat(ReflectionUtil.findSubClassParameterType(methodHandler, 0)). isEqualTo(RpcEnvelope.Request.class); assertThat(ReflectionUtil.findSubClassParameterType(methodHandler, 1)). isEqualTo(RpcEnvelope.Response.class); }
ReflectionUtil.findSubClassParameterType(handler, 0);