Esempio n. 1
0
        public override void Process([NotNull] IProcessableComponent component)
        {
            if (component.Type != ProcessableComponentType.MethodParameter)
            {
                throw new InvalidOperationException("Component is expected to be method parameter.");
            }

            var parameter = (MethodParameterProcessableComponent)component;

            ModuleData moduleData;

            if (!modulesData.TryGetValue(parameter.DeclaringModule, out moduleData))
            {
                moduleData = new ModuleData();
                var codeProvider = Configuration.CustomValueHandlingCodeProvider;
                moduleData.CodeInjector      = new CodeInjector <ParameterValueHandlingCodeProviderArgument>(parameter.DeclaringModule, codeProvider);
                moduleData.StateHoldingField = PrepareStateHoldingField(codeProvider, parameter.DeclaringModule);

                modulesData.Add(parameter.DeclaringModule, moduleData);
            }

            var codeProviderArgument = new ParameterValueHandlingCodeProviderArgument(parameter, moduleData.StateHoldingField);

            if (moduleData.CodeInjector.ShouldBeCallInjected(codeProviderArgument))
            {
                moduleData.CodeInjector.InjectAtBegining(parameter.DeclaringComponent.UnderlyingComponent, codeProviderArgument, Logger);
            }
        }
Esempio n. 2
0
        public override void Process([NotNull] IProcessableComponent component)
        {
            if (component.Type != ProcessableComponentType.Method)
            {
                throw new InvalidOperationException("Component is expected to be method.");
            }

            var method          = (MethodProcessableComponent)component;
            var declaringModule = method.DeclaringModule;

            ModuleData moduleData;

            if (!modulesData.TryGetValue(declaringModule, out moduleData))
            {
                moduleData = new ModuleData();
                var codeProvider      = Configuration.CodeProvider;
                var stateInstanceName = GetStateInstanceName(method);
                moduleData.CodeInjector      = new CodeInjector <MethodCodeInjectingCodeProviderArgument>(declaringModule, codeProvider);
                moduleData.StateHoldingField = PrepareStateHoldingField(stateInstanceName, codeProvider, declaringModule);

                modulesData.Add(declaringModule, moduleData);
            }

            var codeProviderArgument = new MethodCodeInjectingCodeProviderArgument(method, moduleData.StateHoldingField);

            if (moduleData.CodeInjector.ShouldBeCallInjected(codeProviderArgument))
            {
                var injectionPlace = GetInjectionPlace(method);
                switch (injectionPlace)
                {
                case MethodInjectionPlace.Begining:
                    moduleData.CodeInjector.InjectAtBegining(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace);
                    break;

                case MethodInjectionPlace.Exit:
                    moduleData.CodeInjector.InjectBeforeExit(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace);
                    break;

                case MethodInjectionPlace.InCatchBlock:
                    moduleData.CodeInjector.InjectInCatchBlock(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace);
                    break;

                case MethodInjectionPlace.InFinallyBlock:
                    throw new InvalidOperationException($"Under construction injection place '{injectionPlace}' specified.");

                case MethodInjectionPlace.Custom:
                    if (CustomInstructionsInjection == null)
                    {
                        throw new InvalidOperationException($"If you want to use {nameof(MethodInjectionPlace)}.{nameof(MethodInjectionPlace.Custom)} you need to set value to {nameof(CustomInstructionsInjection)} property.");
                    }

                    moduleData.CodeInjector.Inject(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace, CustomInstructionsInjection);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown injection place '{injectionPlace}' specified.");
                }
            }
        }
Esempio n. 3
0
        public override IEnumerable <Type> GetAttributeMapping(IProcessableComponent component)
        {
            var CurrentAttributeTypes = new List <Type>();

            var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                         where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName
                         select t;

            var matchRaiseSite = from t in rsites
                                 where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name)))
                                 select t;

            var raiseSiteArguments = from t in matchRaiseSite
                                     select t.ConstructorArguments.ToArray();

            foreach (var raisesite in raiseSiteArguments)
            {
                var raisesitename = raisesite[0].Value;

                CurrentAttributeTypes.Add(typeof(ExceptionRaiseSiteAttribute));

                var channels = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                               where t.AttributeType.FullName == typeof(ExceptionChannelAttribute).FullName
                               select t;

                var channelsByRaiseSite = from t in channels
                                          where t.ConstructorArguments.Any(v => v.Value.ToString().Equals(raisesitename))
                                          select t;

                foreach (var channel in channelsByRaiseSite)
                {
                    CurrentAttributeTypes.Add(typeof(ExceptionChannelAttribute));
                }
            }


            var handlers = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                           where t.AttributeType.FullName == typeof(ExceptionHandlerAttribute).FullName
                           select t;

            var matchHandlers = from t in handlers
                                where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name)))
                                select t;

            foreach (var item in matchHandlers)
            {
                CurrentAttributeTypes.Add(typeof(ExceptionHandlerAttribute));
            }

            return(CurrentAttributeTypes);
        }
Esempio n. 4
0
        public override void Process([NotNull] IProcessableComponent component)
        {
            if (component.Type != ProcessableComponentType.Module)
            {
                throw new InvalidOperationException("Component is expected to be module.");
            }

            var module     = (ModuleProcessableComponent)component;
            var attributes = module.CustomAttributes.Where(a => a.AttributeType.FullName == ModuleInitializerAttributeFullName);

            foreach (var attribute in attributes)
            {
                ProcessModuleInitializerAttribute(attribute, module);
            }
        }
Esempio n. 5
0
 protected override Type GetAttributeType(IProcessableComponent component, Type attr)
 {
     if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix))
     {
         return(typeof(Injected1Attribute));
     }
     else if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix))
     {
         return(typeof(Injected2Attribute));
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Esempio n. 6
0
        public void AddAttributeFromProvider(IProcessableComponent component, ILogger logger)
        {
            foreach (var customAttributeType in attributeProvider.GetAttributeMapping(component))
            {
                var attributeInfo = attributeProvider.GetAttributeInfo(component, customAttributeType);

                if (!attributeInfo.ShouldBeAttributeInjected)
                {
                    return;
                }

                logger.Notice($"Injecting attribute to {component.FullName}.");

                component.CustomAttributes.Add(attributeInfo.CustomAttribute);
            }
        }
Esempio n. 7
0
 private static void ProcessComponent(
     IProcessableComponent component,
     IEnumerable <IComponentProcessor <ComponentProcessorConfiguration> > componentProcessors,
     ILogger logger)
 {
     foreach (var processor in componentProcessors)
     {
         try
         {
             processor.Process(component);
         }
         catch (Exception e)
         {
             logger.Error($"There was an error while processing '{component.FullName}' with processor '{processor}'. CurrentDir: '{Environment.CurrentDirectory}'. Exception: {e}");
             throw new Exception();
         }
     }
 }
Esempio n. 8
0
        public AttributeProviderInjectionInfo GetAttributeInfo(IProcessableComponent component, Type att)
        {
            if (ShouldBeInjected(component, att))
            {
                var attributeArguments = GetAttributeArguments(component, att);
                if (attributeArguments == null)
                {
                    attributeArguments = new AttributeProviderAttributeArgument[0];
                }

                var attributeType = GetAttributeType(component, att);
                var attribute     = GetAndCheckCustomAttribute(attributeType, attributeArguments, component.DeclaringModule);

                return(new AttributeProviderInjectionInfo(true, attribute));
            }
            else
            {
                return(new AttributeProviderInjectionInfo(false, null));
            }
        }
Esempio n. 9
0
 protected override Type GetAttributeType(IProcessableComponent component, Type attr)
 {
     if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name))
     {
         return(typeof(ExceptionRaiseSiteAttribute));
     }
     else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name))
     {
         return(typeof(ExceptionChannelAttribute));
     }
     else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name))
     {
         return(typeof(ExceptionHandlerAttribute));
     }
     else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name))
     {
         return(typeof(ExceptionInterfaceAttribute));
     }
     else
     {
         throw new ArgumentException();
     }
 }
Esempio n. 10
0
        public override void Process([NotNull] IProcessableComponent component)
        {
            if (component.Type != ProcessableComponentType.MethodParameter)
            {
                throw new InvalidOperationException("Component is expected to be method parameter.");
            }

            var parameter           = (MethodParameterProcessableComponent)component;
            var parameterDefinition = parameter.UnderlyingComponent;

            if (parameter.CustomAttributes.Any(a => a.AttributeType.FullName == NotNullAttributeFullName))
            {
                var methodDefinition = parameter.DeclaringComponent.UnderlyingComponent;

                if (parameterDefinition.ParameterType.IsValueType && !parameterDefinition.ParameterType.IsNullableValueType())
                {
                    Logger.LogErrorWithSource(methodDefinition, $"Parameter '{parameter.Name}' of method '{methodDefinition.FullName}' cannot be non-nullable because it is a value type.");
                }
                else
                {
                    base.Process(parameter);
                }
            }
        }
Esempio n. 11
0
 protected abstract AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr);
Esempio n. 12
0
 protected abstract Type GetAttributeType(IProcessableComponent component, Type attr);
Esempio n. 13
0
 protected abstract bool ShouldBeInjected(IProcessableComponent component, Type attr);
Esempio n. 14
0
 public abstract IEnumerable <Type> GetAttributeMapping(IProcessableComponent component);
Esempio n. 15
0
        protected override bool ShouldBeInjected(IProcessableComponent component, Type attr)
        {
            bool ShouldBeInjected = false;

            CurrentAttributeArguments = new ArrayList();

            if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name))
            {
                var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                             where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName
                             select t;

                var matchRaiseSite = from t in rsites
                                     where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name)))
                                     select t;

                var raiseSiteArguments = from t in matchRaiseSite
                                         select t.ConstructorArguments.ToArray();

                foreach (var item in raiseSiteArguments)
                {
                    for (int i = 0; i < item.Length; i++)
                    {
                        CurrentAttributeArguments.Add(item[i].Value);
                    }

                    ShouldBeInjected = true;
                }
            }
            else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name))
            {
                var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                             where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName
                             select t;

                var matchRaiseSite = from t in rsites
                                     where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name)))
                                     select t;

                var raiseSiteArguments = from t in matchRaiseSite
                                         select t.ConstructorArguments.ToArray();

                foreach (var raisesite in raiseSiteArguments)
                {
                    var raisesitename = raisesite[0].Value;

                    var channels = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                                   where t.AttributeType.FullName == typeof(ExceptionChannelAttribute).FullName
                                   select t;

                    var channelsByRaiseSite = from t in channels
                                              where t.ConstructorArguments.Any(v => v.Value.ToString().Equals(raisesitename))
                                              select t;

                    var channelArguments = from t in channelsByRaiseSite
                                           select t.ConstructorArguments.ToArray();

                    foreach (var item in channelArguments)
                    {
                        for (int i = 0; i < item.Length; i++)
                        {
                            CurrentAttributeArguments.Add(item[i].Value);
                        }

                        ShouldBeInjected = true;
                    }
                }
            }
            else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name))
            {
                var handlers = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable()
                               where t.AttributeType.FullName == typeof(ExceptionHandlerAttribute).FullName
                               select t;

                var matchHandlers = from t in handlers
                                    where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name)))
                                    select t;

                var handlersArguments = from t in matchHandlers
                                        select t.ConstructorArguments.ToArray();

                foreach (var item in handlersArguments)
                {
                    for (int i = 0; i < item.Length; i++)
                    {
                        CurrentAttributeArguments.Add(item[i].Value);
                    }

                    ShouldBeInjected = true;
                }
            }
            else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name))
            {
                throw new ArgumentException();
            }
            else
            {
                throw new ArgumentException();
            }

            return(ShouldBeInjected);
        }
Esempio n. 16
0
 protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type att)
 {
     return(new AttributeProviderAttributeArgument[]
     {
         AttributeProviderAttributeArgument.CreateParameterArgument("text", $"hello from '{component.Name}'!")
     });
 }
Esempio n. 17
0
 protected override bool ShouldBeInjected(IProcessableComponent component, Type att)
 {
     return(component.Type == ProcessableComponentType.Assembly);
 }
Esempio n. 18
0
        protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr)
        {
            var typeDefinition = component.Type == ProcessableComponentType.Type ? ((TypeDefinition)component.UnderlyingComponent) : null;

            if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString())
                });
            }
            else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("exception", CurrentAttributeArguments[1].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("raiseSite", CurrentAttributeArguments[2].ToString())
                });
            }
            else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("channel", CurrentAttributeArguments[0].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("exception", CurrentAttributeArguments[2].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("methodName", CurrentAttributeArguments[3].ToString()),
                });
            }
            else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString())
                });
            }
            else
            {
                throw new ArgumentException();
            }
        }
Esempio n. 19
0
 public override IEnumerable <Type> GetAttributeMapping(IProcessableComponent component)
 {
     throw new NotImplementedException();
 }
Esempio n. 20
0
 protected override bool ShouldBeInjected(IProcessableComponent component, Type att)
 {
     return(component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix) || component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix));
 }
Esempio n. 21
0
 public override void Process([NotNull] IProcessableComponent component)
 {
     attributeInjector.AddAttributeFromProvider(component, Logger);
 }
Esempio n. 22
0
 public virtual void Process([NotNull] IProcessableComponent component)
 {
     throw new NotImplementedException();
 }
Esempio n. 23
0
 protected override Type GetAttributeType(IProcessableComponent component, Type att)
 {
     return(typeof(CustomAssemblyInfoAttribute));
 }
Esempio n. 24
0
        protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr)
        {
            var typeDefinition = component.Type == ProcessableComponentType.Type ? ((TypeDefinition)component.UnderlyingComponent) : null;

            if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("component", component.Type),
                    AttributeProviderAttributeArgument.CreateParameterArgument("type", typeDefinition),
                    AttributeProviderAttributeArgument.CreateParameterArgument("nameHash", component.Name.GetHashCode()),
                    AttributeProviderAttributeArgument.CreateParameterArgument("name", component.Name)
                });
            }
            else if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix))
            {
                return(new AttributeProviderAttributeArgument[]
                {
                    AttributeProviderAttributeArgument.CreateParameterArgument("component", new Enum[] { component.Type, component.Type, component.Type }),
                    AttributeProviderAttributeArgument.CreateParameterArgument("type", new TypeReference[] { typeDefinition, typeDefinition, typeDefinition }),
                    AttributeProviderAttributeArgument.CreateParameterArgument("nameHash", new int[] { component.Name.GetHashCode(), component.Name.GetHashCode(), component.Name.GetHashCode() }),
                    AttributeProviderAttributeArgument.CreateParameterArgument("name", new string[] { component.Name, component.Name, component.Name })
                });
            }
            else
            {
                throw new InvalidOperationException();
            }
        }