public CircularImportProblem(ConfigurationClass configClass, Stack <ConfigurationClass> importStack, Type configurationClassType)
     : base(String.Format("A circular [Import] has been detected: " +
                          "Illegal attempt by [Configuration] class '{0}' to import class '{1}' as '{2}' is " +
                          "already present in the current import stack [{3}]",
                          importStack.Peek().SimpleName, configClass.SimpleName,
                          configClass.SimpleName, importStack),
            new Location(importStack.Peek().Resource, configurationClassType)
            )
 {
 }
Example #2
0
        private void LoadObjectDefinitionsForConfigurationClass(ConfigurationClass configClass)
        {
            LoadObjectDefinitionForConfigurationClassIfNecessary(configClass);

            foreach (ConfigurationClassMethod method in configClass.Methods)
            {
                LoadObjectDefinitionsForModelMethod(method);
            }

            LoadObjectDefinitionsFromImportedResources(configClass.ImportedResources);
        }
        /// <summary>
        /// Processes the configuration class.
        /// </summary>
        /// <param name="configurationClass">The configuration class.</param>
        protected void ProcessConfigurationClass(ConfigurationClass configurationClass)
        {
            DoProcessConfigurationClass(configurationClass);

            if (ConfigurationClasses.Contains(configurationClass) && configurationClass.ObjectName != null)
            {
                // Explicit object definition found, probably replacing an import.
                // Let's remove the old one and go with the new one.
                ConfigurationClasses.Remove(configurationClass);
            }
            ConfigurationClasses.Add(configurationClass);
        }
        /// <summary>
        /// Processes the configuration class.
        /// </summary>
        /// <param name="configurationClass">The configuration class.</param>
        protected void ProcessConfigurationClass(ConfigurationClass configurationClass)
        {
            DoProcessConfigurationClass(configurationClass);

            if (ConfigurationClasses.Contains(configurationClass) && configurationClass.ObjectName != null)
            {
                // Explicit object definition found, probably replacing an import.
                // Let's remove the old one and go with the new one.
                ConfigurationClasses.Remove(configurationClass);
            }
            ConfigurationClasses.Add(configurationClass);
        }
 private void ProcessImport(ConfigurationClass configClass, IEnumerable <Type> classesToImport)
 {
     if (_importStack.Contains(configClass))
     {
         _problemReporter.Error(new CircularImportProblem(configClass, _importStack, configClass.ConfigurationClassType));
     }
     else
     {
         _importStack.Push(configClass);
         foreach (Type classToImport in classesToImport)
         {
             ProcessConfigurationClass(new ConfigurationClass(null, classToImport));
         }
         _importStack.Pop();
     }
 }
        private void DoProcessConfigurationClass(ConfigurationClass configurationClass)
        {
            Attribute[] importAttributes = Attribute.GetCustomAttributes(configurationClass.ConfigurationClassType, typeof(ImportAttribute));

            if (importAttributes.Length > 0)
            {
                foreach (Attribute importAttribute in importAttributes)
                {
                    ImportAttribute attrib = importAttribute as ImportAttribute;

                    if (null != attrib)
                    {
                        ProcessImport(configurationClass, attrib.Types);
                    }
                }
            }

            Attribute[] importResourceAttributes = Attribute.GetCustomAttributes(configurationClass.ConfigurationClassType, typeof(ImportResourceAttribute));

            if (importResourceAttributes.Length > 0)
            {
                foreach (Attribute importResourceAttribute in importResourceAttributes)
                {
                    ImportResourceAttribute attrib = importResourceAttribute as ImportResourceAttribute;

                    if (null != attrib)
                    {
                        foreach (string resource in attrib.Resources)
                        {
                            configurationClass.AddImportedResource(resource, attrib.DefinitionReader);
                        }
                    }
                }
            }

            Collections.Generic.ISet <MethodInfo> definitionMethods = GetAllMethodsWithCustomAttributeForClass(configurationClass.ConfigurationClassType, typeof(ObjectDefAttribute));
            foreach (MethodInfo definitionMethod in definitionMethods)
            {
                configurationClass.Methods.Add(new ConfigurationClassMethod(definitionMethod, configurationClass));
            }
        }
Example #7
0
        private void LoadObjectDefinitionForConfigurationClassIfNecessary(ConfigurationClass configClass)
        {
            if (configClass.ObjectName != null)
            {
                // a Object definition already exists for this configuration class -> nothing to do
                return;
            }

            // no Object definition exists yet -> this must be an imported configuration class ([Import]).
            GenericObjectDefinition configObjectDef = new GenericObjectDefinition();
            String className = configClass.ConfigurationClassType.Name;

            configObjectDef.ObjectTypeName = className;
            configObjectDef.ObjectType     = configClass.ConfigurationClassType;
            if (CheckConfigurationClassCandidate(configClass.ConfigurationClassType))
            {
                String configObjectName = ObjectDefinitionReaderUtils.RegisterWithGeneratedName(configObjectDef,
                                                                                                _registry);
                configClass.ObjectName = configObjectName;
                Logger.LogDebug($"Registered object definition for imported [Configuration] class {configObjectName}");
            }
        }
        private void LoadObjectDefinitionsForConfigurationClass(ConfigurationClass configClass)
        {
            LoadObjectDefinitionForConfigurationClassIfNecessary(configClass);

            foreach (ConfigurationClassMethod method in configClass.Methods)
            {
                LoadObjectDefinitionsForModelMethod(method);
            }

            LoadObjectDefinitionsFromImportedResources(configClass.ImportedResources);
        }
        private void LoadObjectDefinitionForConfigurationClassIfNecessary(ConfigurationClass configClass)
        {
            if (configClass.ObjectName != null)
            {
                // a Object definition already exists for this configuration class -> nothing to do
                return;
            }

            // no Object definition exists yet -> this must be an imported configuration class ([Import]).
            GenericObjectDefinition configObjectDef = new GenericObjectDefinition();
            String className = configClass.ConfigurationClassType.Name;
            configObjectDef.ObjectTypeName = className;
            configObjectDef.ObjectType = configClass.ConfigurationClassType;
            if (CheckConfigurationClassCandidate(configClass.ConfigurationClassType))
            {
                String configObjectName = ObjectDefinitionReaderUtils.RegisterWithGeneratedName(configObjectDef,
                                                                                                _registry);
                configClass.ObjectName = configObjectName;
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(String.Format("Registered object definition for imported [Configuration] class {0}",
                                                configObjectName));
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the ConfigurationClassMethod class.
 /// </summary>
 /// <param name="methodInfo"></param>
 /// <param name="configurationClass"></param>
 public ConfigurationClassMethod(MethodInfo methodInfo, ConfigurationClass configurationClass)
 {
     _methodInfo = methodInfo;
     _configurationClass = configurationClass;
 }
Example #11
0
        private void LoadObjectDefinitionsForModelMethod(ConfigurationClassMethod method)
        {
            ConfigurationClass configClass = method.ConfigurationClass;
            MethodInfo         metadata    = method.MethodMetadata;

            RootObjectDefinition objDef = new ConfigurationClassObjectDefinition();

            objDef.FactoryObjectName = configClass.ObjectName;
            objDef.FactoryMethodName = metadata.Name;
            objDef.AutowireMode      = Objects.Factory.Config.AutoWiringMode.Constructor;

            // consider name and any aliases
            //Dictionary<String, Object> ObjectAttributes = metadata.getAnnotationAttributes(Object.class.getName());
            object[]      objectAttributes = metadata.GetCustomAttributes(typeof(ObjectDefAttribute), true);
            List <string> names            = new List <string>();

            foreach (object t in objectAttributes)
            {
                string[] namesAndAliases = ((ObjectDefAttribute)t).NamesToArray;

                if (namesAndAliases != null)
                {
                    names.Add(metadata.Name);
                }
                else
                {
                    namesAndAliases = new[] { metadata.Name };
                }

                names.AddRange(namesAndAliases);
            }

            string objectName = (names.Count > 0 ? names[0] : method.MethodMetadata.Name);

            for (int i = 1; i < names.Count; i++)
            {
                _registry.RegisterAlias(objectName, names[i]);
            }

            // has this already been overridden (e.g. via XML)?
            if (_registry.ContainsObjectDefinition(objectName))
            {
                IObjectDefinition existingObjectDef = _registry.GetObjectDefinition(objectName);
                // is the existing Object definition one that was created from a configuration class?
                if (!(existingObjectDef is ConfigurationClassObjectDefinition))
                {
                    // no -> then it's an external override, probably XML
                    // overriding is legal, return immediately
                    Logger.Debug(m => m("Skipping loading Object definition for {0}: a definition for object " +
                                        "'{1}' already exists. This is likely due to an override in XML.", method,
                                        objectName));
                    return;
                }
            }

            //TODO: container does not presently support the concept of Primary object definition for type resolution
            //if (Attribute.GetCustomAttribute(metadata, typeof(PrimaryAttribute)) != null)
            //{
            //    ObjectDef.isPrimary = true;
            //}

            // is this Object to be instantiated lazily?
            if (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) != null)
            {
                objDef.IsLazyInit =
                    (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) as LazyAttribute).LazyInitialize;
            }

            if (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) != null)
            {
                objDef.DependsOn =
                    (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) as DependsOnAttribute).Name;
            }

            //TODO: container does not presently support autowiring to the degree needed to support this feature as of yet
            //Autowire autowire = (Autowire) ObjectAttributes.get("autowire");
            //if (autowire.isAutowire()) {
            //	ObjectDef.setAutowireMode(autowire.value());
            //}

            if (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) != null)
            {
                objDef.InitMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) as ObjectDefAttribute).
                    InitMethod;
                objDef.DestroyMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) as ObjectDefAttribute).
                    DestroyMethod;
            }

            // consider scoping
            if (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) != null)
            {
                objDef.Scope =
                    (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) as ScopeAttribute).ObjectScope.ToString();
            }

            Logger.Debug(m => m("Registering Object definition for [ObjectDef] method {0}.{1}()",
                                configClass.ConfigurationClassType.Name, objectName));

            _registry.RegisterObjectDefinition(objectName, objDef);
        }
        private void DoProcessConfigurationClass(ConfigurationClass configurationClass)
        {

            Attribute[] importAttributes = Attribute.GetCustomAttributes(configurationClass.ConfigurationClassType, typeof(ImportAttribute));

            if (importAttributes.Length > 0)
            {
                foreach (Attribute importAttribute in importAttributes)
                {
                    ImportAttribute attrib = importAttribute as ImportAttribute;

                    if (null != attrib)
                    {
                        ProcessImport(configurationClass, attrib.Types);
                    }
                }
            }

            Attribute[] importResourceAttributes = Attribute.GetCustomAttributes(configurationClass.ConfigurationClassType, typeof(ImportResourceAttribute));

            if (importResourceAttributes.Length > 0)
            {
                foreach (Attribute importResourceAttribute in importResourceAttributes)
                {
                    ImportResourceAttribute attrib = importResourceAttribute as ImportResourceAttribute;

                    if (null != attrib)
                    {
                        foreach (string resource in attrib.Resources)
                        {
                            configurationClass.AddImportedResource(resource, attrib.DefinitionReader);
                        }
                    }
                }
            }

            Collections.Generic.ISet<MethodInfo> definitionMethods = GetAllMethodsWithCustomAttributeForClass(configurationClass.ConfigurationClassType, typeof(ObjectDefAttribute));
            foreach (MethodInfo definitionMethod in definitionMethods)
            {
                configurationClass.Methods.Add(new ConfigurationClassMethod(definitionMethod, configurationClass));

            }
        }
 public CircularImportProblem(ConfigurationClass configClass, Stack<ConfigurationClass> importStack, Type configurationClassType)
     : base(String.Format("A circular [Import] has been detected: " +
                  "Illegal attempt by [Configuration] class '{0}' to import class '{1}' as '{2}' is " +
                  "already present in the current import stack [{3}]",
                  importStack.Peek().SimpleName, configClass.SimpleName,
                  configClass.SimpleName, importStack),
           new Location(importStack.Peek().Resource, configurationClassType)
     )
 { }
 private void ProcessImport(ConfigurationClass configClass, IEnumerable<Type> classesToImport)
 {
     if (_importStack.Contains(configClass))
     {
         _problemReporter.Error(new CircularImportProblem(configClass, _importStack, configClass.ConfigurationClassType));
     }
     else
     {
         _importStack.Push(configClass);
         foreach (Type classToImport in classesToImport)
         {
             ProcessConfigurationClass(new ConfigurationClass(null, classToImport));
         }
         _importStack.Pop();
     }
 }
        private void DoProcessConfigurationClass(ConfigurationClass configurationClass)
        {
            if (Attribute.GetCustomAttribute(configurationClass.ConfigurationClassType, typeof(ImportAttribute)) != null)
            {
                ImportAttribute attrib = Attribute.GetCustomAttribute(configurationClass.ConfigurationClassType, typeof(ImportAttribute)) as ImportAttribute;
                ProcessImport(configurationClass, attrib.Types);
            }

            if (Attribute.GetCustomAttribute(configurationClass.ConfigurationClassType, typeof(ImportResourceAttribute)) != null)
            {
                ImportResourceAttribute attrib = Attribute.GetCustomAttribute(configurationClass.ConfigurationClassType, typeof(ImportResourceAttribute)) as ImportResourceAttribute;

                foreach (string resource in attrib.Resources)
                {
                    configurationClass.AddImportedResource(resource, attrib.DefinitionReader);
                }
            }

            Collections.Generic.ISet<MethodInfo> definitionMethods = GetAllMethodsWithCustomAttributeForClass(configurationClass.ConfigurationClassType, typeof(DefinitionAttribute));
            foreach (MethodInfo definitionMethod in definitionMethods)
            {
                configurationClass.Methods.Add(new ConfigurationClassMethod(definitionMethod, configurationClass));

            }
        }
 /// <summary>
 /// Initializes a new instance of the ConfigurationClassMethod class.
 /// </summary>
 /// <param name="methodInfo"></param>
 /// <param name="configurationClass"></param>
 public ConfigurationClassMethod(MethodInfo methodInfo, ConfigurationClass configurationClass)
 {
     _methodInfo         = methodInfo;
     _configurationClass = configurationClass;
 }