Exemple #1
0
        /// <summary>
        /// Create a new Instance of the specified type.
        ///
        /// Type should have an empty constructor or a constructor with annotation.
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="node">Configuration node.</param>
        /// <returns>Created Instance</returns>
        public static object CreateInstance(Type type, ConfigPathNode node)
        {
            object target = null;

            ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            if (constructors != null && constructors.Length > 0)
            {
                foreach (ConstructorInfo ci in constructors)
                {
                    MethodInvoke mi = (MethodInvoke)Attribute.GetCustomAttribute(ci, typeof(MethodInvoke));
                    if (mi != null)
                    {
                        ParameterInfo[] parameters = ci.GetParameters();
                        ConfigPathNode  nnode      = node;
                        if (parameters != null && parameters.Length > 0)
                        {
                            if (!String.IsNullOrWhiteSpace(mi.Path))
                            {
                                AbstractConfigNode cnode = nnode.Find(mi.Path);
                                if (cnode != null && cnode.GetType() == typeof(ConfigPathNode))
                                {
                                    nnode = (ConfigPathNode)cnode;
                                }
                            }
                            if (nnode != null)
                            {
                                ConfigParametersNode pnode = nnode.GetParameters();
                                if (pnode != null)
                                {
                                    List <object> values = FindParameters(pnode, ci.Name, parameters);
                                    if (values != null && values.Count > 0)
                                    {
                                        target = Activator.CreateInstance(type, values.ToArray());
                                    }
                                }
                            }
                        }
                        else
                        {
                            target = Activator.CreateInstance(type);
                        }
                    }
                }
            }

            if (target == null)
            {
                target = Activator.CreateInstance(type);
            }
            if (target != null)
            {
                target = ReadValues((ConfigPathNode)node, target, null);
                CallMethodInvokes((ConfigPathNode)node, target);
            }
            return(target);
        }
Exemple #2
0
        /// <summary>
        /// Process the annotated field and set the values from the configuration attributes.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="node">Configuration Node</param>
        /// <param name="target">Target Type instance.</param>
        /// <param name="field">Property to update</param>
        /// <param name="attr">Config attribute annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigAttribute attr, List <string> valuePaths)
        {
            string pname = attr.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = field.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(attr.Path))
            {
                AbstractConfigNode nnode = node.Find(attr.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }

            if (node != null)
            {
                ConfigAttributesNode pnode = node.GetAttributes();
                if (pnode != null)
                {
                    ConfigValueNode vn = pnode.GetValue(pname);
                    if (vn != null)
                    {
                        value = vn.GetValue();
                    }
                    if (valuePaths != null)
                    {
                        valuePaths.Add(pnode.GetSearchPath());
                    }
                }
            }
            if (!String.IsNullOrWhiteSpace(value))
            {
                object v = GetValue <T>(pname, value, attr.Function, field.FieldType, target, attr.Required);
                if (v != null)
                {
                    TypeUtils.CallSetter(field, target, v);
                }
            }
            else if (attr.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }
Exemple #3
0
        /// <summary>
        /// Process the annotated property and set the values from the configuration parameters.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="node">Configuration Node</param>
        /// <param name="target">Target Type instance.</param>
        /// <param name="property">Property to update</param>
        /// <param name="param">Config param annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessProperty <T>(ConfigPathNode node, T target, PropertyInfo property, ConfigParam param, List <string> valuePath)
        {
            string pname = param.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = property.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(param.Path))
            {
                AbstractConfigNode nnode = node.Find(param.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }
            if (node != null)
            {
                ConfigParametersNode pnode = node.GetParameters();
                if (pnode != null)
                {
                    ConfigValueNode vn = pnode.GetValue(pname);
                    if (vn != null)
                    {
                        value = vn.GetValue();
                    }
                    if (valuePath != null)
                    {
                        valuePath.Add(pnode.GetSearchPath());
                    }
                }
            }
            if (!String.IsNullOrWhiteSpace(value))
            {
                object v = GetValue <T>(pname, value, param.Function, property.PropertyType, target, param.Required);
                if (v != null)
                {
                    property.SetValue(target, v);
                }
            }
            else if (param.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }
Exemple #4
0
        /// <summary>
        /// Check and Invoke annotated methods for this type.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="node">Configuration node.</param>
        /// <param name="target">Target Type instance</param>
        private static void CallMethodInvokes <T>(ConfigPathNode node, T target)
        {
            Type type = target.GetType();

            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
            if (methods != null)
            {
                foreach (MethodInfo method in methods)
                {
                    MethodInvoke mi = (MethodInvoke)Attribute.GetCustomAttribute(method, typeof(MethodInvoke));
                    if (mi != null)
                    {
                        bool            invoked    = false;
                        ParameterInfo[] parameters = method.GetParameters();
                        ConfigPathNode  nnode      = node;
                        if (parameters != null && parameters.Length > 0)
                        {
                            if (!String.IsNullOrWhiteSpace(mi.Path))
                            {
                                AbstractConfigNode cnode = nnode.Find(mi.Path);
                                if (cnode != null && cnode.GetType() == typeof(ConfigPathNode))
                                {
                                    nnode = (ConfigPathNode)cnode;
                                }
                            }
                            if (nnode != null)
                            {
                                ConfigParametersNode pnode = nnode.GetParameters();
                                if (pnode != null)
                                {
                                    List <object> values = FindParameters(pnode, method.Name, parameters);
                                    if (values != null && values.Count > 0)
                                    {
                                        method.Invoke(target, values.ToArray());
                                        invoked = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            method.Invoke(target, null);
                            invoked = true;
                        }

                        if (!invoked)
                        {
                            throw new AnnotationProcessorException(String.Format("Error Invoking Method : [mehtod={0}][node={1}]",
                                                                                 method.Name, node.GetSearchPath()));
                        }
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Load the defined processes for the pipeline.
        /// </summary>
        /// <param name="pipeline">Parent pipeline</param>
        /// <param name="node">Configuration node.</param>
        /// <param name="name">Pipeline name</param>
        private void LoadProcessors(object pipeline, ConfigPathNode node, string name)
        {
            AbstractConfigNode pnode = node.Find(CONFIG_NODE_PROCESSORS);

            Conditions.NotNull(pnode);
            if (pnode.GetType() == typeof(ConfigPathNode))
            {
                LoadProcessor(pipeline, (ConfigPathNode)pnode, name);
            }
            else if (pnode.GetType() == typeof(ConfigElementListNode))
            {
                ConfigElementListNode nodes = (ConfigElementListNode)pnode;
                foreach (ConfigElementNode elem in nodes.GetValues())
                {
                    if (elem.GetType() == typeof(ConfigPathNode) && elem.Name == CONFIG_NODE_PROCESSOR)
                    {
                        LoadProcessor(pipeline, (ConfigPathNode)elem, name);
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Process the annotated field and set the values from the configuration value.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="node">Configuration Node</param>
        /// <param name="target">Target Type instance.</param>
        /// <param name="field">Property to update</param>
        /// <param name="configValue">Config value annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigValue configValue, List <string> valuePaths)
        {
            string pname = configValue.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = field.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(configValue.Path))
            {
                AbstractConfigNode nnode = node.Find(configValue.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }

            if (node != null)
            {
                AbstractConfigNode cnode = node.GetChildNode(pname);
                if (cnode != null)
                {
                    if (valuePaths != null)
                    {
                        valuePaths.Add(cnode.GetSearchPath());
                    }
                    if (cnode.GetType() == typeof(ConfigValueNode))
                    {
                        ConfigValueNode vn = (ConfigValueNode)cnode;
                        if (vn != null)
                        {
                            value = vn.GetValue();
                        }
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            object v = GetValue <T>(pname, value, configValue.Function, field.FieldType, target, configValue.Required);
                            if (v != null)
                            {
                                TypeUtils.CallSetter(field, target, v);
                            }
                        }
                    }
                    else
                    {
                        if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(List <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertListFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                        else if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(HashSet <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertSetFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            object ov = TypeUtils.CallGetter(field, target);

            if (ov == null && configValue.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }
Exemple #7
0
        /// <summary>
        /// Create a new Instance of the specified type.
        ///
        /// Type should have an empty constructor or a constructor with annotation.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="type">Type</param>
        /// <param name="node">Configuration node.</param>
        /// <returns>Created Instance</returns>
        public static T CreateInstance <T>(Type type, ConfigPathNode node)
        {
            T target = default(T);

            ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            if (constructors != null && constructors.Length > 0)
            {
                foreach (ConstructorInfo ci in constructors)
                {
                    MethodInvoke mi = (MethodInvoke)Attribute.GetCustomAttribute(ci, typeof(MethodInvoke));
                    if (mi != null)
                    {
                        ParameterInfo[] parameters = ci.GetParameters();
                        ConfigPathNode  nnode      = node;
                        if (parameters != null && parameters.Length > 0)
                        {
                            if (!String.IsNullOrWhiteSpace(mi.Path))
                            {
                                AbstractConfigNode cnode = nnode.Find(mi.Path);
                                if (cnode != null && cnode.GetType() == typeof(ConfigPathNode))
                                {
                                    nnode = (ConfigPathNode)cnode;
                                }
                            }
                            if (nnode != null)
                            {
                                ConfigParametersNode pnode = nnode.GetParameters();
                                if (pnode != null)
                                {
                                    List <object> values = FindParameters(pnode, ci.Name, parameters);
                                    if (values != null && values.Count > 0)
                                    {
                                        target = (T)Activator.CreateInstance(type, values.ToArray());
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            target = Activator.CreateInstance <T>();
                            break;
                        }
                    }
                }
            }

            if (ReflectionUtils.IsNull(target))
            {
                target = Activator.CreateInstance <T>();
            }
            if (!ReflectionUtils.IsNull(target))
            {
                target = ReadValues((ConfigPathNode)node, target, null);
                CallMethodInvokes((ConfigPathNode)node, target);
            }
            else
            {
                throw new AnnotationProcessorException(String.Format("Error creating instance of Type: [path={0}][type={1}]", node.GetSearchPath(), type.FullName));
            }
            return(target);
        }