Beispiel #1
0
        private void WeaveMethods(
            TypeDefinition clazz,
            IReadOnlyList <CrossCutterN.Aspect.Metadata.ICustomAttribute> classCustomAttributes,
            MethodWeaver methodWeaver,
            IClassWeavingStatisticsBuilder statistics)
        {
            foreach (var method in clazz.Methods)
            {
                // methods without bodies can't be injected
                // property getter and setter will be handled in property phase
                if (!method.HasBody || IsPropertyMethod(method))
                {
                    continue;
                }

                var methodInfo = method.Convert(classCustomAttributes);
                var plan       = planner.MakePlan(methodInfo);
                if (!plan.IsEmpty())
                {
                    var methodStatistics = StatisticsFactory.InitializeMethodWeavingRecord(method.Name, method.FullName);
                    methodWeaver.Weave(method, plan, null, methodStatistics);
                    var methodStatisticsFinished = methodStatistics.Build();
                    if (methodStatisticsFinished != null)
                    {
                        statistics.AddMethodWeavingStatistics(methodStatisticsFinished);
                    }
                }
            }
        }
Beispiel #2
0
        private void WeaveProperties(
            TypeDefinition clazz,
            IReadOnlyList <CrossCutterN.Aspect.Metadata.ICustomAttribute> classCustomAttributes,
            MethodWeaver methodWeaver,
            IClassWeavingStatisticsBuilder statistics)
        {
            foreach (var property in clazz.Properties)
            {
                var propertyInfo = property.Convert(classCustomAttributes);
                var plan         = planner.MakePlan(propertyInfo);
                if (!plan.IsEmpty())
                {
                    var propertyStatistics = StatisticsFactory.InitializePropertyWeavingRecord(property.Name, property.FullName);
                    var getterPlan         = plan.GetterPlan;
                    var getter             = property.GetMethod;
                    if (!getterPlan.IsEmpty() && getter != null)
                    {
                        methodWeaver.Weave(getter, getterPlan, property.Name, propertyStatistics.GetterContainer);
                    }

                    var setterPlan = plan.SetterPlan;
                    var setter     = property.SetMethod;
                    if (!setterPlan.IsEmpty() && setter != null)
                    {
                        methodWeaver.Weave(setter, setterPlan, property.Name, propertyStatistics.SetterContainer);
                    }

                    var propertyStatisticsFinished = propertyStatistics.Build();
                    if (propertyStatisticsFinished != null)
                    {
                        statistics.AddPropertyWeavingStatistics(propertyStatisticsFinished);
                    }
                }
            }
        }
Beispiel #3
0
        private static void WeaveSwitches(
            ISwitchHandler switchHandler,
            TypeDefinition clazz,
            IWeavingContext context,
            IClassWeavingStatisticsBuilder statistics)
        {
            var switchData = switchHandler.GetData().ToList();

            if (switchData.Any())
            {
                SwitchInitializationWeaver.Weave(clazz, context, switchData, statistics);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Add switch registering to a class.
        /// </summary>
        /// <param name="clazz">The class to register switch.</param>
        /// <param name="context">The weaving context.</param>
        /// <param name="switchData">Data of switches.</param>
        /// <param name="statistics">Weaving statistics.</param>
        public static void Weave(TypeDefinition clazz, IWeavingContext context, IReadOnlyList <SwitchInitializingData> switchData, IClassWeavingStatisticsBuilder statistics)
        {
#if DEBUG
            if (clazz == null)
            {
                throw new ArgumentNullException("clazz");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (switchData == null || !switchData.Any())
            {
                throw new ArgumentNullException("switchData");
            }

            if (statistics == null)
            {
                throw new ArgumentNullException("statistics");
            }
#endif
            var staticConstructor      = clazz.Methods.FirstOrDefault(mthd => mthd.IsStaticConstructor());
            MethodDefinition method    = null;
            ILProcessor      processor = null;
            if (staticConstructor == null)
            {
                method = new MethodDefinition(
                    ".cctor",
                    MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
                    context.GetTypeReference(typeof(void)));
                processor = method.Body.GetILProcessor();
                method.Body.Instructions.Add(processor.Create(OpCodes.Ret));
                clazz.Methods.Add(method);
            }
            else
            {
                method    = staticConstructor;
                processor = method.Body.GetILProcessor();
            }

            var typeName     = clazz.GetFullName();
            var instructions = new List <Instruction>();
            foreach (var data in switchData)
            {
                RegisterSwitch(instructions, processor, context, data, typeName);
                statistics.AddSwitchWeavingRecord(StatisticsFactory.InitializeSwitchWeavingRecord(typeName, data.Property, data.MethodSignature, data.Aspect, data.Field.Name, data.Value));
            }

            CompleteSwitchRegistration(method, instructions, processor, context, typeName);
        }