Esempio n. 1
0
        public static BeanInstantiator MakeInstantiator(BeanEventType beanEventType, EngineImportService engineImportService)
        {
            // see if we use a factory method
            if (beanEventType.FactoryMethodName != null)
            {
                return(ResolveFactoryMethod(beanEventType, engineImportService));
            }

            // find public ctor
            EngineImportException ctorNotFoundEx;

            try
            {
                engineImportService.ResolveCtor(beanEventType.UnderlyingType, new Type[0]);
                if (beanEventType.FastClass != null)
                {
                    return(new BeanInstantiatorByNewInstanceFastClass(beanEventType.FastClass));
                }
                else
                {
                    return(new BeanInstantiatorByNewInstanceReflection(beanEventType.UnderlyingType));
                }
            }
            catch (EngineImportException ex)
            {
                ctorNotFoundEx = ex;
            }

            // not found ctor, see if FastClass can handle
            if (beanEventType.FastClass != null)
            {
                var fastClass = beanEventType.FastClass;
                try
                {
                    fastClass.NewInstance();
                    return(new BeanInstantiatorByNewInstanceFastClass(beanEventType.FastClass));
                }
                catch (TargetInvocationException e)
                {
                    string message = string.Format(
                        "Failed to instantiate class '{0}', define a factory method if the class has no suitable constructors: {1}",
                        fastClass.TargetType.FullName,
                        (e.InnerException ?? e).Message);
                    Log.Debug(message, e);
                }
                catch (ArgumentException e)
                {
                    string message =
                        string.Format(
                            "Failed to instantiate class '{0}', define a factory method if the class has no suitable constructors",
                            fastClass.TargetType.FullName);
                    Log.Debug(message, e);
                }
            }

            throw new EventBeanManufactureException(
                      "Failed to find no-arg constructor and no factory method has been configured and cannot use reflection to instantiate object of type " +
                      beanEventType.UnderlyingType.Name, ctorNotFoundEx);
        }
Esempio n. 2
0
        public static Pair <FastConstructor, ExprEvaluator[]> GetManufacturer(Type targetClass, EngineImportService engineImportService, ExprEvaluator[] exprEvaluators, object[] expressionReturnTypes)
        {
            var ctorTypes  = new Type[expressionReturnTypes.Length];
            var evaluators = new ExprEvaluator[exprEvaluators.Length];

            for (var i = 0; i < expressionReturnTypes.Length; i++)
            {
                var columnType = expressionReturnTypes[i];

                if (columnType is Type || columnType == null)
                {
                    ctorTypes[i]  = (Type)expressionReturnTypes[i];
                    evaluators[i] = exprEvaluators[i];
                    continue;
                }

                if (columnType is EventType)
                {
                    var columnEventType = (EventType)columnType;
                    var returnType      = columnEventType.UnderlyingType;
                    var inner           = exprEvaluators[i];
                    evaluators[i] = new ProxyExprEvaluator
                    {
                        ProcEvaluate = evaluateParams =>
                        {
                            var theEvent = (EventBean)inner.Evaluate(evaluateParams);
                            if (theEvent != null)
                            {
                                return(theEvent.Underlying);
                            }
                            return(null);
                        },

                        ProcReturnType = () =>
                        {
                            return(returnType);
                        },
                    };
                    ctorTypes[i] = returnType;
                    continue;
                }

                // handle case where the select-clause contains an fragment array
                if (columnType is EventType[])
                {
                    var columnEventType     = ((EventType[])columnType)[0];
                    var componentReturnType = columnEventType.UnderlyingType;

                    var inner = exprEvaluators[i];
                    evaluators[i] = new ProxyExprEvaluator
                    {
                        ProcEvaluate = evaluateParams =>
                        {
                            var result = inner.Evaluate(evaluateParams);
                            if (!(result is EventBean[]))
                            {
                                return(null);
                            }
                            var events = (EventBean[])result;
                            var values = Array.CreateInstance(componentReturnType, events.Length);
                            for (var jj = 0; jj < events.Length; jj++)
                            {
                                values.SetValue(events[jj].Underlying, jj);
                            }
                            return(values);
                        },

                        ProcReturnType = () =>
                        {
                            return(componentReturnType);
                        },
                    };
                    continue;
                }

                var message = "Invalid assignment of expression " + i + " returning type '" + columnType +
                              "', column and parameter types mismatch";
                throw new ExprValidationException(message);
            }

            FastConstructor fctor;

            try
            {
                var ctor      = engineImportService.ResolveCtor(targetClass, ctorTypes);
                var fastClass = FastClass.Create(targetClass);
                return(new Pair <FastConstructor, ExprEvaluator[]>(fastClass.GetConstructor(ctor), evaluators));
            }
            catch (EngineImportException ex)
            {
                throw new ExprValidationException("Failed to find a suitable constructor for type '" + targetClass.Name + "': " + ex.Message, ex);
            }
        }
Esempio n. 3
0
 public ConstructorInfo ResolveCtor(Type clazz, Type[] paramTypes)
 {
     return(_engineImportService.ResolveCtor(clazz, paramTypes));
 }