예제 #1
0
        private IHandlerMethodResolver CreateResolverForAnnotation(Type attributeType)
        {
            IList <MethodInfo> methodsWithAnnotation   = new List <MethodInfo>();
            IList <MethodInfo> defaultCandidateMethods = HandlerMethodUtils.GetCandidateHandlerMethods(_obj);

            foreach (MethodInfo method in defaultCandidateMethods)
            {
                object[] annotations = method.GetCustomAttributes(attributeType, true);
                if (annotations != null && annotations.Length > 0)
                {
                    methodsWithAnnotation.Add(method);
                }
            }
            IList <MethodInfo> candidateMethods = (methodsWithAnnotation.Count == 0) ? null : methodsWithAnnotation;

            if (candidateMethods == null)
            {
                if (logger.IsInfoEnabled)
                {
                    logger.Info("Failed to find any valid Message-handling methods with annotation ["
                                + attributeType + "] on target class [" + _obj.GetType() + "]. "
                                + "Method-resolution will be applied to all eligible methods.");
                }
                candidateMethods = defaultCandidateMethods;
            }
            if (candidateMethods.Count == 1)
            {
                return(new StaticHandlerMethodResolver(candidateMethods[0]));
            }
            return(new PayloadTypeMatchingHandlerMethodResolver(candidateMethods));
        }
        /// <summary>
        /// create a <see cref="StaticHandlerMethodResolver"/> with handling method <paramref name="method"/>
        /// </summary>
        /// <param name="method">the handling method</param>
        public StaticHandlerMethodResolver(MethodInfo method)
        {
            AssertUtils.ArgumentNotNull(method, "method must not be null");
            AssertUtils.ArgumentHasElements(method.GetParameters(), "Message-handling method [" + method + "] must accept at least one parameter.");
            AssertUtils.IsTrue(HandlerMethodUtils.IsValidHandlerMethod(method), "Invalid Message-handling method [" + method + "]");

            _method = method;
        }
        private static Type DetermineExpectedType(MethodInfo method)
        {
            Type expectedType = null;

            foreach (ParameterInfo pi in method.GetParameters())
            {
                object[] parameterAnnotations = pi.GetCustomAttributes(false);

                if (!HandlerMethodUtils.ContainsHeaderAnnotation(parameterAnnotations))
                {
                    if (expectedType != null)
                    {
                        throw new ArgumentException("Message-handling method must only have one parameter expecting a Message or Message payload."
                                                    + " Other parameters may be included but only if they have @Header or @Headers annotations.");
                    }

                    Type parameterType = pi.ParameterType;
                    if (parameterType.IsGenericType)
                    {
                        if (typeof(IMessage).IsAssignableFrom(parameterType))
                        {
                            expectedType = DetermineExpectedTypeFromParameterizedMessageType(parameterType);
                        }
                        else
                        {
                            Type[] genericArguments = parameterType.GetGenericArguments();
                            if (genericArguments.Length > 1)
                            {
                                throw new ArgumentException("Message-handling method: error on generic parameter [" + pi.ParameterType + " " + pi.Name + "]");
                            }

                            expectedType = genericArguments[0];
                        }
                    }
                    else
                    {
                        expectedType = parameterType;
                    }
                }
            }

            return(expectedType);
        }
예제 #4
0
        private IHandlerMethodResolver CreateResolverForMethodName(string methodName)
        {
            IList <MethodInfo> methodsWithName = new List <MethodInfo>();

            foreach (MethodInfo method in HandlerMethodUtils.GetCandidateHandlerMethods(_obj))
            {
                if (method.Name.Equals(methodName))
                {
                    methodsWithName.Add(method);
                }
            }
            if (methodsWithName.Count == 0)
            {
                throw new ArgumentException("Failed to find any valid Message-handling methods named '" + methodName + "' on target class [" + _obj.GetType() + "].");
            }
            if (methodsWithName.Count == 1)
            {
                return(new StaticHandlerMethodResolver(methodsWithName[0]));
            }

            return(new PayloadTypeMatchingHandlerMethodResolver(methodsWithName));
        }