Example #1
0
        private void EnsureSingletonField(AspectDefinition aspect)
        {
            var ts = aspect.Host.Module.GetTypeSystem();

            var singletonField = aspect.Host.Fields.FirstOrDefault(m => m.Name == Constants.AspectGlobalField);

            if (singletonField == null)
            {
                singletonField = new FieldDefinition(Constants.AspectGlobalField, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.InitOnly, ts.Import(aspect.Host));
                aspect.Host.Fields.Add(singletonField);

                var cctor = aspect.Host.Methods.FirstOrDefault(c => c.IsConstructor && c.IsStatic);

                if (cctor == null)
                {
                    cctor = new MethodDefinition(".cctor",
                                                 MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
                                                 ts.Void);

                    aspect.Host.Methods.Add(cctor);

                    cctor.GetEditor().Instead(i => i.Return());
                }

                cctor.GetEditor().OnInit(i => i.Store(singletonField, aspect.CreateAspectInstance));
            }

            aspect.Host.IsBeforeFieldInit = false;
        }
Example #2
0
 public InjectionInfo(AspectDefinition aspect, ushort priority, PropagateTo propagation, Regex filter)
 {
     this.aspect      = aspect;
     this.priority    = priority;
     this.propagation = propagation;
     this.filter      = filter;
 }
Example #3
0
        public void Cache(AspectDefinition aspect)
        {
            var assets = GetAssets(aspect.Host.Module);

            assets.Aspects.RemoveAll(a => a.Host == aspect.Host);
            assets.Aspects.Add(aspect);
        }
        public void ParsingAspectWithAFewMixinDeclarations()
        {
            AspectParser parser = CreateParser(
                "aspect XPTO for MyNamespace.MyType \r\n" +
                "" +
                "  include MyNamespace.Type1 in MyAssembly1 " +
                "  include MyNamespace.Type2 in MyAssembly2 " +
                "  include MyNamespace.Type3 in MyAssembly3 " +
                "" +
                "" +
                "end");
            EngineConfiguration conf = parser.Parse();
            AspectDefinition    def  = conf.Aspects[0];

            Assert.AreEqual(3, def.Mixins.Count);

            MixinDefinition typeName = def.Mixins[0];

            Assert.AreEqual(TargetTypeEnum.Type, typeName.TypeReference.TargetType);
            Assert.AreEqual("MyNamespace.Type1", typeName.TypeReference.TypeName);
            Assert.AreEqual("MyAssembly1", typeName.TypeReference.AssemblyReference.AssemblyName);

            typeName = def.Mixins[1];
            Assert.AreEqual(TargetTypeEnum.Type, typeName.TypeReference.TargetType);
            Assert.AreEqual("MyNamespace.Type2", typeName.TypeReference.TypeName);
            Assert.AreEqual("MyAssembly2", typeName.TypeReference.AssemblyReference.AssemblyName);

            typeName = def.Mixins[2];
            Assert.AreEqual(TargetTypeEnum.Type, typeName.TypeReference.TargetType);
            Assert.AreEqual("MyNamespace.Type3", typeName.TypeReference.TypeName);
            Assert.AreEqual("MyAssembly3", typeName.TypeReference.AssemblyReference.AssemblyName);
        }
Example #5
0
        private void EnsureSingletonField(AspectDefinition aspect)
        {
            var singletonField = aspect.Host.Fields.FirstOrDefault(m => m.Name == Constants.AspectGlobalField);

            if (singletonField == null)
            {
                singletonField = new FieldDefinition(Constants.AspectGlobalField, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.InitOnly, aspect.Host);
                aspect.Host.Fields.Add(singletonField);

                var cctor = aspect.Host.Methods.FirstOrDefault(c => c.IsConstructor && c.IsStatic);

                if (cctor == null)
                {
                    cctor = new MethodDefinition(".cctor",
                                                 MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
                                                 aspect.Host.Module.ImportReference(StandardTypes.Void));

                    aspect.Host.Methods.Add(cctor);

                    cctor.Body.Instead(i => i.Return());
                }

                cctor.Body.AfterEntry(i => i.Store(singletonField, val => val.CreateAspectInstance(aspect)));
            }

            aspect.Host.IsBeforeFieldInit = false;
        }
Example #6
0
    protected void include(
        AspectDefinition aspect
        ) //throws RecognitionException, TokenStreamException
    {
        Token i = null;

        TypeReference   tr = null;
        MixinDefinition md;


        try {              // for error handling
            i = LT(1);
            match(INCLUDE);

            md = new MixinDefinition(ToLexicalInfo(i));

            tr = type_name_or_ref();

            md.TypeReference = tr;
            aspect.Mixins.Add(md);
        }
        catch (RecognitionException ex)
        {
            reportError(ex);
            consume();
            consumeUntil(tokenSet_10_);
        }
    }
Example #7
0
        public AspectDefinition Read(TypeDefinition type)
        {
            if (!_cache.TryGetValue(type, out var aspectDef))
            {
                var effects = ExtractEffects(type).ToList();
                var aspect  = ExtractAspectAttribute(type);

                if (aspect != null)
                {
                    aspectDef = new AspectDefinition
                    {
                        Host    = type,
                        Scope   = aspect.GetConstructorValue <Scope>(0),
                        Factory = aspect.GetPropertyValue <TypeReference>(nameof(Aspect.Factory)),
                        Effects = effects
                    }
                }
                ;
                else if (effects.Any())
                {
                    _log.Log(EffectRules.EffectMustBePartOfAspect, type, type.Name);
                }

                _cache.AddOrUpdate(type, aspectDef, (k, o) => aspectDef);
            }

            return(aspectDef);
        }
Example #8
0
        private static void AssertEngineConfiguration(AspectEngine engine)
        {
            Assert.IsNotNull(engine);
            Assert.IsNotNull(engine.Configuration);
            Assert.AreEqual(1, engine.Configuration.Imports.Count);
            Assert.AreEqual(1, engine.Configuration.Mixins.Count);
            Assert.AreEqual(1, engine.Configuration.Interceptors.Count);
            Assert.AreEqual(1, engine.Configuration.Aspects.Count);

            AspectDefinition aspect = engine.Configuration.Aspects[0];

            Assert.AreEqual("McBrother", aspect.Name);
            Assert.AreEqual(typeof(DummyCustomer), aspect.TargetType.SingleType.ResolvedType);

            Assert.AreEqual(1, aspect.Mixins.Count);
            MixinDefinition mixin = aspect.Mixins[0];

            Assert.AreEqual(typeof(DummyMixin), mixin.TypeReference.ResolvedType);

            Assert.AreEqual(1, aspect.PointCuts.Count);
            PointCutDefinition pointcut = aspect.PointCuts[0];

            Assert.AreEqual(AllMethodSignature.Instance, pointcut.Method);

            Assert.AreEqual(1, pointcut.Advices.Count);
            InterceptorDefinition advice = pointcut.Advices[0];

            Assert.AreEqual(typeof(DummyInterceptor), advice.TypeReference.ResolvedType);
        }
Example #9
0
        public AspectDefinition Read(TypeDefinition type)
        {
            if (!_cache.TryGetValue(type, out var aspectDef))
            {
                var effects = ExtractEffects(type).ToList();
                var aspect  = ExtractAspectAttribute(type);

                if (aspect != null)
                {
                    aspectDef = new AspectDefinition
                    {
                        Host    = type,
                        Scope   = aspect.GetConstructorValue <Aspect.Scope>(0),
                        Factory = aspect.GetPropertyValue <Aspect>(au => au.Factory) as TypeReference,
                        Effects = effects
                    }
                }
                ;
                else if (effects.Any())
                {
                    _weaver.LogError($"Type {type.FullName} has effects, but is not marked as an aspect. Concider using [Aspect] attribute.");
                }

                _cache.AddOrUpdate(type, aspectDef, (k, o) => aspectDef);
            }

            return(aspectDef);
        }
Example #10
0
    protected void pointcut(
        AspectDefinition aspect
        ) //throws RecognitionException, TokenStreamException
    {
        Token p = null;

        PointCutDefinition pointcut = null;
        PointCutFlags      flags    = PointCutFlags.Unspecified;


        try {              // for error handling
            p = LT(1);
            match(POINTCUT);
            flags = pointcutflags();

            pointcut = new PointCutDefinition(ToLexicalInfo(p), flags);
            aspect.PointCuts.Add(pointcut);

            pointcuttarget(pointcut);
            advices(pointcut);
            match(END);
        }
        catch (RecognitionException ex)
        {
            reportError(ex);
            consume();
            consumeUntil(tokenSet_11_);
        }
    }
Example #11
0
        protected override void SetupAspect(AspectDefinition aspect)
        {
            advice = null;
            var pointcut = CreatePointcuts <PropertyMethodPointcut>(aspect, "SheepPoint", "Getter & Name:'SomeProperty'");

            aspect.Advise(new AroundAdvice(pointcut, GetAspectMethod("MockAdvice")));
        }
Example #12
0
        protected override void SetupAspect(AspectDefinition aspect)
        {
            advice = null;
            var pointcut = CreatePointcuts <MethodPointcut>(aspect, "SheepPoint", "Name: ('SimpleConcat'|'Generic*') & InType:Name:'MethodTestTarget*'");

            aspect.Advise(new AroundAdvice(pointcut, GetAspectMethod("MockAdvice")));
        }
 public MixinWeaveProcess(TypeDefinition target, AspectDefinition aspect, MixinEffect effect)
 {
     _target = target;
     _aspect = aspect;
     _effect = effect;
     _ts     = target.Module.GetTypeSystem();
 }
Example #14
0
        public void RegisterAdvices(AspectDefinition aspect)
        {
            var perFlowAspect = typeof(PerFlowLifecycleAspect <>).MakeGenericType(aspect.Type);

            aspect.Advise(new AroundAdvice(_pointcutRefs.SelectMany(x => GetPointcuts(aspect, x)), perFlowAspect.GetMethod("WrapFlowScope")));
            //aspect.Advise(new PerFlowAdvice(_pointcutRefs.SelectMany(aspect.GetPointcuts), aspect.WeavedType));
        }
Example #15
0
        protected override void SetupAspect(AspectDefinition aspect)
        {
            advice = null;
            var pointcut = CreatePointcuts <GetFieldPointcut>(aspect, "SheepPoint", "Field: (Name: '_some*')");

            aspect.Advise(new AroundAdvice(pointcut, GetAspectMethod("MockAdvice")));
        }
Example #16
0
        protected override void CustomizeContext(GeneratorContext context, IKernel kernel,
                                                 ComponentModel model, object[] arguments)
        {
            AspectDefinition aspect = (AspectDefinition)model.ExtendedProperties["aop.aspect"];

            if (aspect == null)
            {
                return;
            }

            MixinDefinitionCollection mixins = aspect.Mixins;

            foreach (MixinDefinition definition in mixins)
            {
                Type mixinType = definition.TypeReference.ResolvedType;

                try
                {
                    context.AddMixinInstance(Activator.CreateInstance(mixinType));
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Could not instantiate mixin " + mixinType.FullName, e);
                }
            }
        }
Example #17
0
        public override IPointcut RegisterPointcut(AspectDefinition aspect, string pointcutName)
        {
            var pointcut = CreatePointcut(aspect, pointcutName);

            PointcutBuilder.Instance.BuildFromSaql(aspect, _saql, pointcut);
            return(pointcut);
        }
Example #18
0
 protected override void SetupAspect(AspectDefinition aspect)
 {
     interfaceMock = new Mock <ITestInterface>();
     aspect.Advise(
         new DeclareMixinFromMethodAdvice(CreatePointcuts <TypePointcut>(aspect, "TestPointcut", "Name:'TestTarget'"),
                                          GetAspectMethod("MixinTestAdvice"), null, true));
 }
Example #19
0
        private void LoadAspects()
        {
            XmlNodeList aspects = _node.SelectNodes("aspect");

            foreach (XmlNode node in aspects)
            {
                String           name   = GetRequiredAttribute(node, "name");
                AspectDefinition aspect = new AspectDefinition(LexicalInfo.Empty, name);
                Configuration.Aspects.Add(aspect);

                XmlNode singleType = node.SelectSingleNode("for/singletype");
                aspect.TargetType            = new TargetTypeDefinition();
                aspect.TargetType.SingleType = CreateTypeReference(singleType);

                XmlNodeList mixins = node.SelectNodes("mixin");
                foreach (XmlNode inner in mixins)
                {
                    MixinDefinition def = new MixinDefinition(LexicalInfo.Empty);
                    def.TypeReference = CreateTypeReference(inner);
                    aspect.Mixins.Add(def);
                }

                XmlNodeList pointcuts = node.SelectNodes("pointcut");
                foreach (XmlNode inner in pointcuts)
                {
                    PointCutDefinition def = CreatePointCutDefinition(inner);
                    aspect.PointCuts.Add(def);
                }
            }
        }
        public override bool Validate(AspectDefinition aspect, ILogger log)
        {
            ValidateSupportedArguments(aspect, log);

            var result = true;

            if (Method.IsStatic)
            {
                log.Log(EffectRules.AdviceMustHaveValidSingnature, Method, Method.Name, EffectRules.Literals.IsStatic);
                result = false;
            }

            if (!Method.IsPublic)
            {
                log.Log(EffectRules.AdviceMustHaveValidSingnature, Method, Method.Name, EffectRules.Literals.IsNotPublic);
                result = false;
            }

            if (Method.HasGenericParameters)
            {
                log.Log(EffectRules.AdviceMustHaveValidSingnature, Method, Method.Name, EffectRules.Literals.IsGeneric);
                result = false;
            }

            return(result);
        }
Example #21
0
        private AspectDefinition BuildAspect(Type type, IEnumerable <KeyValuePair <MemberInfo, IAdviceProvider> > memberAdvices)
        {
            var aspect = new AspectDefinition(type);

            Parallel.ForEach(type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), m =>
            {
                foreach (
                    var p in
                    m.GetCustomAttributes(typeof(IPointcutProvider), true).
                    Cast <IPointcutProvider>())
                {
                    p.RegisterPointcut(aspect, m.Name);
                }
            });

            Parallel.ForEach(memberAdvices, a => a.Value.RegisterAdvice(aspect, a.Key));

            var lifecycleProvider = GetLifecycleProvider(type);

            if (lifecycleProvider != null)
            {
                lifecycleProvider.RegisterAdvices(aspect);
            }

            return(aspect);
        }
        public IInvocationDispatcher Create(AspectDefinition aspect, AspectEngine engine)
        {
            IInvocationDispatcher dispatcher = new DefaultInvocationDispatcher(aspect);

            dispatcher.Init(engine);
            return(dispatcher);
        }
 protected void LogWrongArgs(AdviceArgument[] wrongArgs, AspectDefinition aspectDefinition, ILogger log)
 {
     foreach (var arg in wrongArgs)
     {
         log.Log(EffectRules.ArgumentIsAlwaysNull, Method, arg.Parameter.Name, Kind.ToString());
     }
 }
Example #24
0
        public void AddDefinition(AspectDefinition aspect, ILifecycleProvider lifecycleProvider)
        {
            lifecycleProvider.RegisterAdvices(aspect);

            aspects.Add(aspect.Type, aspect);
            lifecycleProviders[aspect.Type] = lifecycleProvider;
        }
Example #25
0
        public MethodPointcut Pointcut(string saql)
        {
            var aspect   = new AspectDefinition(GetType());
            var pointcut = aspect.CreatePointcut <MethodPointcut>(saql);

            new PointcutBuilder().BuildFromSaql(aspect, saql, pointcut);
            return(pointcut);
        }
Example #26
0
 public AdviceAfterProcess(BaseModuleWeaver weaver, MethodDefinition target, AspectDefinition aspect, AfterAdviceEffect effect)
     : base(weaver, target, effect, aspect)
 {
     if (_target.ReturnType.FullName != WellKnownTypes.Void && effect.Arguments.Any(a => a.Source == Broker.Advice.Argument.Source.ReturnValue))
     {
         _retvar = GetOrCreateRetVar();
     }
 }
Example #27
0
 public AdviceWeaveProcessBase(ILogger log, MethodDefinition target, InjectionDefinition injection)
 {
     _log       = log;
     _target    = target;
     _effect    = (TEffect)injection.Effect;
     _injection = injection;
     _aspect    = injection.Source;
 }
 public AdviceAfterProcess(ILogger log, MethodDefinition target, AspectDefinition aspect, AfterAdviceEffect effect)
     : base(log, target, effect, aspect)
 {
     if (!_target.ReturnType.IsTypeOf(_ts.Void) && effect.Arguments.Any(a => a.Source == Broker.Advice.Argument.Source.ReturnValue))
     {
         _retvar = GetOrCreateRetVar();
     }
 }
        public AdviceWeaveProcessBase(ILogger log, MethodDefinition target, TEffect effect, AspectDefinition aspect)
        {
            _log    = log;
            _target = target;
            _effect = effect;
            _aspect = aspect;

            _ts = target.Module.GetTypeSystem();
        }
Example #30
0
        public PointcutDefinitionException(AspectDefinition aspect, IPointcut pointcut, string message, Exception innerException)
            : base(FormatMessage(aspect, pointcut, message), innerException)
        {
            Contract.Requires(aspect != null);
            Contract.Requires(pointcut != null);

            Aspect   = aspect;
            Pointcut = pointcut;
        }
		public virtual void OnAspectDefinition(AspectDefinition aspect)
		{
			if (EnterAspectDefinition(aspect))
			{
				OnTargetTypeDefinition(aspect.TargetType);
				aspect.Mixins.Accept(this);
				aspect.PointCuts.Accept(this);

				LeaveAspectDefinition(aspect);
			}
		}
		protected virtual void LeaveAspectDefinition(AspectDefinition aspect)
		{
		}
		protected virtual bool EnterAspectDefinition(AspectDefinition aspect)
		{
			return true;
		}
Example #34
0
		public override void OnAspectDefinition(AspectDefinition aspect)
		{
			Push( Document.CreateNode(XmlNodeType.Element, "aspect", null) );

			XmlAttribute att = Document.CreateAttribute("name");
			att.Value = aspect.Name;
			Current.Attributes.Append( att );

			Push( Document.CreateNode(XmlNodeType.Element, "for", null) );
			SerializeTargetType( aspect.TargetType );
			Pop();

			base.OnAspectDefinition (aspect);
			Pop();
		}