private bool IsDeclarativeMethodParameter(string target, ParameterInfo methodParameter) { var declarative = false; if (_serviceProvider != null) { var targetBindable = BindingHelpers.GetBindableTarget(_serviceProvider, target); if (!methodParameter.ParameterType.IsAssignableFrom(typeof(object)) && targetBindable != null) { declarative = typeof(IMessageChannel).IsAssignableFrom(methodParameter.ParameterType); if (!declarative) { var targetBeanClass = targetBindable.GetType(); foreach (var adapter in _streamListenerParameterAdapters) { if (adapter.Supports(targetBeanClass, methodParameter)) { declarative = true; break; } } } } } else { if (!methodParameter.ParameterType.IsAssignableFrom(typeof(object))) { return(typeof(IMessageChannel).IsAssignableFrom(methodParameter.ParameterType)); } } return(declarative); }
public object[] AdaptAndRetrieveInboundArguments(MethodInfo method, string inboundName, params IStreamListenerParameterAdapter[] streamListenerParameterAdapters) { var arguments = new object[method.GetParameters().Length]; for (var parameterIndex = 0; parameterIndex < arguments.Length; parameterIndex++) { var methodParameter = method.GetParameters()[parameterIndex]; var parameterType = methodParameter.ParameterType; string targetReferenceValue = null; if (methodParameter.GetCustomAttribute <InputAttribute>() != null) { var attr = methodParameter.GetCustomAttribute <InputAttribute>(); targetReferenceValue = attr.Name; } else if (methodParameter.GetCustomAttribute <OutputAttribute>() != null) { var attr = methodParameter.GetCustomAttribute <OutputAttribute>(); targetReferenceValue = attr.Name; } else if (arguments.Length == 1 && !string.IsNullOrEmpty(inboundName)) { targetReferenceValue = inboundName; } if (targetReferenceValue != null) { var targetBean = BindingHelpers.GetBindableTarget(_serviceProvider, targetReferenceValue); // Iterate existing parameter adapters first foreach (var streamListenerParameterAdapter in streamListenerParameterAdapters) { if (streamListenerParameterAdapter.Supports(targetBean.GetType(), methodParameter)) { arguments[parameterIndex] = streamListenerParameterAdapter.Adapt(targetBean, methodParameter); break; } } if (arguments[parameterIndex] == null && parameterType.IsAssignableFrom(targetBean.GetType())) { arguments[parameterIndex] = targetBean; } if (arguments[parameterIndex] == null) { throw new ArgumentException("Cannot convert argument " + parameterIndex + " of " + method + "from " + targetBean.GetType() + " to " + parameterType); } } else { throw new InvalidOperationException(StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS); } } return(arguments); }
private void InvokeStreamListenerResultAdapter(MethodInfo method, Type implementation, string outboundName, params object[] arguments) { try { var bean = ActivatorUtilities.CreateInstance(_context.ServiceProvider, implementation); if (typeof(void).Equals(method.ReturnType)) { method.Invoke(bean, arguments); } else { var result = method.Invoke(bean, arguments); if (string.IsNullOrEmpty(outboundName)) { var parameters = method.GetParameters(); for (var parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++) { var methodParameter = parameters[parameterIndex]; var attr = methodParameter.GetCustomAttribute <OutputAttribute>(); if (attr != null) { outboundName = attr.Name; } } } var targetBean = BindingHelpers.GetBindableTarget(_context, outboundName); foreach (var streamListenerResultAdapter in _streamListenerResultAdapters) { if (streamListenerResultAdapter.Supports(result.GetType(), targetBean.GetType())) { streamListenerResultAdapter.Adapt(result, targetBean); break; } } } } catch (Exception) { // Log throw; } }