@Test @Timeout(value = 2) void testCreateGatewayEqualsAndHashCode() { CompleteGateway gateway2 = testSubject.createGateway(CompleteGateway.class); assertNotSame(gateway, gateway2); assertNotEquals(gateway, gateway2); }
@SuppressWarnings("unchecked") public <T> T createGateway(Class<T> gatewayInterface) { Map<Method, InvocationHandler> dispatchers = new HashMap<>(); for (Method gatewayMethod : gatewayInterface.getMethods()) { MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameters()); final Class<?>[] arguments = gatewayMethod.getParameterTypes(); InvocationHandler dispatcher = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(true) .build(); if (!Arrays.asList(CompletableFuture.class, Future.class, CompletionStage.class) .contains(gatewayMethod.getReturnType())) { if (arguments.length >= 3 && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1]) && (Long.TYPE.isAssignableFrom(arguments[arguments.length - 2]) || Integer.TYPE.isAssignableFrom(arguments[arguments.length - 2]))) { dispatcher = wrapToReturnWithTimeoutInArguments(dispatcher, arguments.length - 2, arguments.length - 1); } else { Map<String, Object> timeout = findAnnotationAttributes(gatewayMethod, Timeout.class).orElse( findAnnotationAttributes(gatewayMethod.getDeclaringClass(), Timeout.class) .orElse(null) ); if (timeout != null) { dispatcher = wrapToReturnWithFixedTimeout(dispatcher, (int) timeout.get("timeout"), (TimeUnit) timeout.get("unit")); } else if (!Void.TYPE.equals(gatewayMethod.getReturnType()) || gatewayMethod.getExceptionTypes().length > 0) { dispatcher = wrapToWaitForResult(dispatcher); } else if (commandCallbacks.isEmpty() && !hasCallbackParameters(gatewayMethod)) { DispatchOnInvocationHandler fireAndForgetHandler = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(false) .build(); dispatcher = wrapToFireAndForget(fireAndForgetHandler); } } Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes(); dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions); } dispatchers.put(gatewayMethod, dispatcher); } GatewayInvocationHandler gatewayInvocationHandler = GatewayInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .dispatchers(dispatchers) .build(); return gatewayInterface.cast(Proxy.newProxyInstance( gatewayInterface.getClassLoader(), new Class[]{gatewayInterface}, gatewayInvocationHandler )); }
CommandGatewayFactory { @SuppressWarnings("unchecked") public <T> T createGateway(Class<T> gatewayInterface) { Map<Method, InvocationHandler> dispatchers = new HashMap<>(); for (Method gatewayMethod : gatewayInterface.getMethods()) { MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameters()); final Class<?>[] arguments = gatewayMethod.getParameterTypes(); InvocationHandler dispatcher = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(true) .build(); if (!Arrays.asList(CompletableFuture.class, Future.class, CompletionStage.class) .contains(gatewayMethod.getReturnType())) { if (arguments.length >= 3 && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1]) && (Long.TYPE.isAssignableFrom(arguments[arguments.length - 2]) || Integer.TYPE.isAssignableFrom(arguments[arguments.length - 2]))) { dispatcher = wrapToReturnWithTimeoutInArguments(dispatcher, arguments.length - 2, arguments.length - 1); } else { Map<String, Object> timeout = findAnnotationAttributes(gatewayMethod, Timeout.class).orElse( findAnnotationAttributes(gatewayMethod.getDeclaringClass(), Timeout.class) .orElse(null) ); if (timeout != null) { dispatcher = wrapToReturnWithFixedTimeout(dispatcher, (int) timeout.get("timeout"), (TimeUnit) timeout.get("unit")); } else if (!Void.TYPE.equals(gatewayMethod.getReturnType()) || gatewayMethod.getExceptionTypes().length > 0) { dispatcher = wrapToWaitForResult(dispatcher); } else if (commandCallbacks.isEmpty() && !hasCallbackParameters(gatewayMethod)) { DispatchOnInvocationHandler fireAndForgetHandler = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(false) .build(); dispatcher = wrapToFireAndForget(fireAndForgetHandler); } } Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes(); dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions); } dispatchers.put(gatewayMethod, dispatcher); } GatewayInvocationHandler gatewayInvocationHandler = GatewayInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .dispatchers(dispatchers) .build(); return gatewayInterface.cast(Proxy.newProxyInstance( gatewayInterface.getClassLoader(), new Class[]{gatewayInterface}, gatewayInvocationHandler )); } }
CommandGatewayFactory { @SuppressWarnings("unchecked") public <T> T createGateway(Class<T> gatewayInterface) { Map<Method, InvocationHandler> dispatchers = new HashMap<>(); for (Method gatewayMethod : gatewayInterface.getMethods()) { MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameters()); final Class<?>[] arguments = gatewayMethod.getParameterTypes(); InvocationHandler dispatcher = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(true) .build(); if (!Arrays.asList(CompletableFuture.class, Future.class, CompletionStage.class) .contains(gatewayMethod.getReturnType())) { if (arguments.length >= 3 && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1]) && (Long.TYPE.isAssignableFrom(arguments[arguments.length - 2]) || Integer.TYPE.isAssignableFrom(arguments[arguments.length - 2]))) { dispatcher = wrapToReturnWithTimeoutInArguments(dispatcher, arguments.length - 2, arguments.length - 1); } else { Map<String, Object> timeout = findAnnotationAttributes(gatewayMethod, Timeout.class).orElse( findAnnotationAttributes(gatewayMethod.getDeclaringClass(), Timeout.class) .orElse(null) ); if (timeout != null) { dispatcher = wrapToReturnWithFixedTimeout(dispatcher, (int) timeout.get("timeout"), (TimeUnit) timeout.get("unit")); } else if (!Void.TYPE.equals(gatewayMethod.getReturnType()) || gatewayMethod.getExceptionTypes().length > 0) { dispatcher = wrapToWaitForResult(dispatcher); } else if (commandCallbacks.isEmpty() && !hasCallbackParameters(gatewayMethod)) { DispatchOnInvocationHandler fireAndForgetHandler = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(false) .build(); dispatcher = wrapToFireAndForget(fireAndForgetHandler); } } Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes(); dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions); } dispatchers.put(gatewayMethod, dispatcher); } GatewayInvocationHandler gatewayInvocationHandler = GatewayInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .dispatchers(dispatchers) .build(); return gatewayInterface.cast(Proxy.newProxyInstance( gatewayInterface.getClassLoader(), new Class[]{gatewayInterface}, gatewayInvocationHandler )); } protected  CommandGatewayFactory(Builder builder); }
CommandGatewayFactory { @SuppressWarnings("unchecked") public <T> T createGateway(Class<T> gatewayInterface) { Map<Method, InvocationHandler> dispatchers = new HashMap<>(); for (Method gatewayMethod : gatewayInterface.getMethods()) { MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameters()); final Class<?>[] arguments = gatewayMethod.getParameterTypes(); InvocationHandler dispatcher = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(true) .build(); if (!Arrays.asList(CompletableFuture.class, Future.class, CompletionStage.class) .contains(gatewayMethod.getReturnType())) { if (arguments.length >= 3 && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1]) && (Long.TYPE.isAssignableFrom(arguments[arguments.length - 2]) || Integer.TYPE.isAssignableFrom(arguments[arguments.length - 2]))) { dispatcher = wrapToReturnWithTimeoutInArguments(dispatcher, arguments.length - 2, arguments.length - 1); } else { Map<String, Object> timeout = findAnnotationAttributes(gatewayMethod, Timeout.class).orElse( findAnnotationAttributes(gatewayMethod.getDeclaringClass(), Timeout.class) .orElse(null) ); if (timeout != null) { dispatcher = wrapToReturnWithFixedTimeout(dispatcher, (int) timeout.get("timeout"), (TimeUnit) timeout.get("unit")); } else if (!Void.TYPE.equals(gatewayMethod.getReturnType()) || gatewayMethod.getExceptionTypes().length > 0) { dispatcher = wrapToWaitForResult(dispatcher); } else if (commandCallbacks.isEmpty() && !hasCallbackParameters(gatewayMethod)) { DispatchOnInvocationHandler fireAndForgetHandler = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(false) .build(); dispatcher = wrapToFireAndForget(fireAndForgetHandler); } } Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes(); dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions); } dispatchers.put(gatewayMethod, dispatcher); } GatewayInvocationHandler gatewayInvocationHandler = GatewayInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .dispatchers(dispatchers) .build(); return gatewayInterface.cast(Proxy.newProxyInstance( gatewayInterface.getClassLoader(), new Class[]{gatewayInterface}, gatewayInvocationHandler )); } protected  CommandGatewayFactory(Builder builder); static Builder builder(); @SuppressWarnings("unchecked") T createGateway(Class<T> gatewayInterface); CommandGatewayFactory registerCommandCallback(CommandCallback<C, R> callback,
                                                                ResponseType<R> responseType); CommandGatewayFactory registerDispatchInterceptor(
            MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor); }
CommandGatewayFactory { @SuppressWarnings("unchecked") public <T> T createGateway(Class<T> gatewayInterface) { Map<Method, InvocationHandler> dispatchers = new HashMap<>(); for (Method gatewayMethod : gatewayInterface.getMethods()) { MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameters()); final Class<?>[] arguments = gatewayMethod.getParameterTypes(); InvocationHandler dispatcher = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(true) .build(); if (!Arrays.asList(CompletableFuture.class, Future.class, CompletionStage.class) .contains(gatewayMethod.getReturnType())) { if (arguments.length >= 3 && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1]) && (Long.TYPE.isAssignableFrom(arguments[arguments.length - 2]) || Integer.TYPE.isAssignableFrom(arguments[arguments.length - 2]))) { dispatcher = wrapToReturnWithTimeoutInArguments(dispatcher, arguments.length - 2, arguments.length - 1); } else { Map<String, Object> timeout = findAnnotationAttributes(gatewayMethod, Timeout.class).orElse( findAnnotationAttributes(gatewayMethod.getDeclaringClass(), Timeout.class) .orElse(null) ); if (timeout != null) { dispatcher = wrapToReturnWithFixedTimeout(dispatcher, (int) timeout.get("timeout"), (TimeUnit) timeout.get("unit")); } else if (!Void.TYPE.equals(gatewayMethod.getReturnType()) || gatewayMethod.getExceptionTypes().length > 0) { dispatcher = wrapToWaitForResult(dispatcher); } else if (commandCallbacks.isEmpty() && !hasCallbackParameters(gatewayMethod)) { DispatchOnInvocationHandler fireAndForgetHandler = DispatchOnInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .metaDataExtractors(extractors) .commandCallbacks(commandCallbacks) .forceCallbacks(false) .build(); dispatcher = wrapToFireAndForget(fireAndForgetHandler); } } Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes(); dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions); } dispatchers.put(gatewayMethod, dispatcher); } GatewayInvocationHandler gatewayInvocationHandler = GatewayInvocationHandler.builder() .commandBus(commandBus) .retryScheduler(retryScheduler) .dispatchInterceptors(dispatchInterceptors) .dispatchers(dispatchers) .build(); return gatewayInterface.cast(Proxy.newProxyInstance( gatewayInterface.getClassLoader(), new Class[]{gatewayInterface}, gatewayInvocationHandler )); } protected  CommandGatewayFactory(Builder builder); static Builder builder(); @SuppressWarnings("unchecked") T createGateway(Class<T> gatewayInterface); CommandGatewayFactory registerCommandCallback(CommandCallback<C, R> callback,
                                                                ResponseType<R> responseType); CommandGatewayFactory registerDispatchInterceptor(
            MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor);  }