public object CreateClassProxy(Type classType, AspectDefinition aspect, params object[] constructorArgs) { AssertUtil.ArgumentNotNull(classType, "classType"); AssertUtil.ArgumentNotNull(aspect, "aspect"); IInvocationDispatcher dispatcher = _dispatcherFactory.Create(aspect, _engine); return CreateAndInstantiateClassProxy(classType, aspect, dispatcher, constructorArgs); }
public object CreateInterfaceProxy(Type inter, object target, AspectDefinition aspect) { AssertUtil.ArgumentNotNull(inter, "inter"); AssertUtil.ArgumentNotNull(target, "target"); AssertUtil.ArgumentNotNull(aspect, "aspect"); IInvocationDispatcher dispatcher = _dispatcherFactory.Create(aspect, _engine); return CreateAndInstantiateInterfaceProxy(inter, target, aspect, dispatcher); }
public void MatchSingle() { AspectDefinition def = new AspectDefinition(LexicalInfo.Empty, "Wilber"); TypeReference typeRef = new TypeReference(LexicalInfo.Empty, ""); typeRef.ResolvedType = typeof(Stack); def.TargetType = new TargetTypeDefinition( typeRef ); Assert.IsFalse( SingleTypeMatcher.Instance.Match( typeof(Object), def ) ); Assert.IsFalse( SingleTypeMatcher.Instance.Match( typeof(IList), def ) ); Assert.IsFalse( SingleTypeMatcher.Instance.Match( typeof(ArrayList), def ) ); Assert.IsTrue( SingleTypeMatcher.Instance.Match( typeof(Stack), def ) ); }
public void MatchNamespace() { AspectDefinition def = new AspectDefinition(LexicalInfo.Empty, "Wilber"); def.TargetType = new TargetTypeDefinition(); def.TargetType.TargetStrategy = TargetStrategyEnum.Namespace; def.TargetType.NamespaceRoot = "System.Collections"; Assert.IsFalse( NamespaceMatcher.Instance.Match( typeof(Object), def ) ); Assert.IsFalse( NamespaceMatcher.Instance.Match( typeof(HybridDictionary), def ) ); Assert.IsTrue( NamespaceMatcher.Instance.Match( typeof(IList), def ) ); Assert.IsTrue( NamespaceMatcher.Instance.Match( typeof(ArrayList), def ) ); Assert.IsTrue( NamespaceMatcher.Instance.Match( typeof(Stack), def ) ); }
protected virtual IClassMatcher ObtainClassMatcher(AspectDefinition aspect) { switch (aspect.TargetType.TargetStrategy) { case TargetStrategyEnum.SingleType: return SingleTypeMatcher.Instance; case TargetStrategyEnum.Assignable: return AssignableMatcher.Instance; case TargetStrategyEnum.Namespace: return NamespaceMatcher.Instance; case TargetStrategyEnum.Custom: return ObtainCustomMatcher(aspect.TargetType); } // There is no way we can get here - hopefully return null; }
public bool Match(Type targetType, AspectDefinition aspect) { Type type = GetTypeToCompare(aspect); if (type.IsGenericTypeDefinition && targetType.IsGenericType) { type = type.MakeGenericType(targetType.GetGenericArguments()); } if (type.IsAssignableFrom(targetType)) { return true; } return false; }
public void MatchNamespaceWithExcludes() { AspectDefinition def = new AspectDefinition(LexicalInfo.Empty, "Wilber"); def.TargetType = new TargetTypeDefinition(); def.TargetType.TargetStrategy = TargetStrategyEnum.Namespace; def.TargetType.NamespaceRoot = "System.Collections"; TypeReference typeRef = new TypeReference(LexicalInfo.Empty, ""); typeRef.ResolvedType = typeof(Stack); def.TargetType.Excludes.Add( typeRef ); Assert.IsTrue( NamespaceMatcher.Instance.Match( typeof(IList), def ) ); Assert.IsTrue( NamespaceMatcher.Instance.Match( typeof(ArrayList), def ) ); Assert.IsFalse( NamespaceMatcher.Instance.Match( typeof(Stack), def ) ); }
public void MatchAssignable() { AspectDefinition def = new AspectDefinition(LexicalInfo.Empty, "Wilber"); def.TargetType = new TargetTypeDefinition(); def.TargetType.TargetStrategy = TargetStrategyEnum.Assignable; TypeReference typeRef = new TypeReference(LexicalInfo.Empty, ""); typeRef.ResolvedType = typeof(IList); def.TargetType.AssignType = typeRef; Assert.IsFalse( AssignableMatcher.Instance.Match( typeof(Object), def ) ); Assert.IsFalse( AssignableMatcher.Instance.Match( typeof(HybridDictionary), def ) ); Assert.IsTrue( AssignableMatcher.Instance.Match( typeof(IList), def ) ); Assert.IsTrue( AssignableMatcher.Instance.Match( typeof(ArrayList), def ) ); Assert.IsFalse( AssignableMatcher.Instance.Match( typeof(Stack), def ) ); }
public bool Match(Type targetType, AspectDefinition aspect) { String namespaceRoot = aspect.TargetType.NamespaceRoot; String typeNamespace = targetType.Namespace; if (typeNamespace.Equals(namespaceRoot)) { foreach (TypeReference typeRef in aspect.TargetType.Excludes) { if (typeRef.ResolvedType.Equals(targetType)) { return false; } } return true; } else { return false; } }
public void Add(AspectDefinition aspect) { InnerList.Add(aspect); }
public AspectInstanceKey(AspectDefinition aspect, Type baseClass) { aspectName = aspect.Name; typeName = baseClass.FullName; }
private void RegisterProxyTypeInCache(AspectDefinition aspect, Type baseType, Type proxyType) { _aspect2ProxyType[new AspectInstanceKey(aspect, baseType)] = proxyType; }
private Type GetProxyTypeFromCache(AspectDefinition aspect, Type baseType) { return _aspect2ProxyType[new AspectInstanceKey(aspect, baseType)] as Type; }
private object ObtainInterfaceProxyInstance(AspectDefinition aspect, object target, Type inter, object[] mixins, IInvocationDispatcher dispatcher) { Type proxyType = GetProxyTypeFromCache(aspect, target.GetType()); if (proxyType != null) { return CreateInterfaceProxyInstance(proxyType, target, mixins, dispatcher); } object proxy = _generator.CreateProxy(inter, target, mixins, dispatcher); RegisterProxyTypeInCache(aspect, target.GetType(), proxy.GetType()); return proxy; }
protected virtual Type GetTypeToCompare(AspectDefinition aspect) { return aspect.TargetType.SingleType.ResolvedType; }
protected virtual object CreateAndInstantiateInterfaceProxy(Type inter, object target, AspectDefinition aspect, IInvocationDispatcher dispatcher, params object[] constructorArgs) { object proxy = null; object[] mixins = InstantiateMixins(aspect.Mixins); proxy = ObtainInterfaceProxyInstance(aspect, target, inter, mixins, dispatcher); InitializeMixins(proxy, mixins); return proxy; }
protected void include( AspectDefinition aspect ) //throws RecognitionException, TokenStreamException { IToken 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_); } }
public ContainerInvocationDispatcher(AspectDefinition aspect, IKernel kernel) : base(aspect) { _kernel = kernel; }
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"); if (singleType != null) { aspect.TargetType = new TargetTypeDefinition(); aspect.TargetType.SingleType = CreateTypeReference(singleType); aspect.TargetType.TargetStrategy = TargetStrategyEnum.SingleType; } XmlNode assignableType = node.SelectSingleNode("for/assignable"); if (assignableType != null) { aspect.TargetType = new TargetTypeDefinition(); aspect.TargetType.AssignType = CreateTypeReference(assignableType); aspect.TargetType.TargetStrategy = TargetStrategyEnum.Assignable; } 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 bool Match(Type targetType, AspectDefinition aspect) { throw new NotImplementedException(); }
private AspectDefinition Union(AspectDefinition[] aspects) { if (aspects.Length == 1) { return aspects[0]; } // TODO: Merge aspects return aspects[0]; }
protected void pointcut( AspectDefinition aspect ) //throws RecognitionException, TokenStreamException { IToken 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_); } }
protected virtual object CreateAndInstantiateClassProxy(Type baseClass, AspectDefinition aspect, IInvocationDispatcher dispatcher, params object[] constructorArgs) { object proxy = null; object[] mixins = InstantiateMixins(aspect.Mixins); proxy = ObtainClassProxyInstance(aspect, baseClass, mixins, dispatcher, constructorArgs); InitializeMixins(proxy, mixins); return proxy; }
public DefaultInvocationDispatcher(AspectDefinition aspect) { _aspect = aspect; _matcher = new DefaultJoinPointMatcher(aspect.PointCuts); }
protected override Type GetTypeToCompare(AspectDefinition aspect) { return aspect.TargetType.AssignType.ResolvedType; }
public IInvocationDispatcher Create(AspectDefinition aspect, AspectEngine engine) { IInvocationDispatcher dispatcher = new DefaultInvocationDispatcher(aspect); dispatcher.Init(engine); return dispatcher; }
/// <summary> /// Creates a single AspectDefinition as the merge of two or more /// AspectDefinitions /// </summary> /// <param name="aspects">The aspects to be merged</param> /// <returns>The result of the merge</returns> protected virtual AspectDefinition Union(AspectDefinition[] aspects) { if (aspects.Length == 1) { return aspects[0]; } AspectDefinition aspect = new AspectDefinition(LexicalInfo.Empty, String.Empty); foreach (AspectDefinition item in aspects) { aspect.Name += item.Name + "$"; aspect.Mixins.AddRange(item.Mixins); aspect.PointCuts.AddRange(item.PointCuts); } return aspect; }
private object ObtainClassProxyInstance(AspectDefinition aspect, Type baseClass, object[] mixins, IInvocationDispatcher dispatcher, params object[] constructorArgs) { Type proxyType = GetProxyTypeFromCache(aspect, baseClass); if (proxyType != null) { return CreateClassProxyInstance(proxyType, mixins, dispatcher, constructorArgs); } object proxy = _generator.CreateClassProxy(baseClass, mixins, dispatcher, constructorArgs); RegisterProxyTypeInCache(aspect, baseClass, proxy.GetType()); return proxy; }
public void BuildUsingCode() { CodeEngineBuilder builder = new CodeEngineBuilder(); EngineConfiguration conf = builder.GetConfiguration(); ImportDirective import = new ImportDirective(LexicalInfo.Empty, "AspectSharp.Tests.Classes"); import.AssemblyReference = new AssemblyReference(LexicalInfo.Empty, "AspectSharp.Tests"); conf.Imports.Add( import ); conf.Mixins.Add( "key", LexicalInfo.Empty ).TypeReference = new TypeReference(LexicalInfo.Empty, "DummyMixin"); conf.Interceptors.Add( "key", LexicalInfo.Empty ).TypeReference = new TypeReference(LexicalInfo.Empty, "DummyInterceptor"); AspectDefinition aspect = new AspectDefinition(LexicalInfo.Empty, "McBrother"); aspect.TargetType = new TargetTypeDefinition(); aspect.TargetType.SingleType = new TypeReference(LexicalInfo.Empty, "DummyCustomer"); conf.Aspects.Add(aspect); MixinDefinition mixin = new MixinDefinition(LexicalInfo.Empty); mixin.TypeReference = new TypeReference(LexicalInfo.Empty, "key", TargetTypeEnum.Link); aspect.Mixins.Add( mixin ); PointCutDefinition pointcut = new PointCutDefinition(LexicalInfo.Empty, PointCutFlags.Method); pointcut.Method = AllMethodSignature.Instance; InterceptorDefinition interceptor = new InterceptorDefinition(LexicalInfo.Empty); interceptor.TypeReference = new TypeReference(LexicalInfo.Empty, "key", TargetTypeEnum.Link); pointcut.Advices.Add(interceptor); aspect.PointCuts.Add(pointcut); AspectEngine engine = builder.Build(); AssertEngineConfiguration(engine); }
protected void aspects( EngineConfiguration conf ) //throws RecognitionException, TokenStreamException { IToken a = null; IToken aspectId = null; AspectDefinition aspect = null; TargetTypeDefinition target = null; TypeReference tr = null; try { // for error handling a = LT(1); match(ASPECT); aspectId = LT(1); match(ID); match(FOR); aspect = new AspectDefinition( ToLexicalInfo(a), aspectId.getText() ); conf.Aspects.Add(aspect); { switch ( LA(1) ) { case ID: { tr=type_name_def(); target = new TargetTypeDefinition( tr ); target.TargetStrategy = TargetStrategyEnum.SingleType; aspect.TargetType = target; break; } case LBRACK: { match(LBRACK); target = new TargetTypeDefinition( ); aspect.TargetType = target; String namespaceRegEx = null; { switch ( LA(1) ) { case ASSIGNFROM: { match(ASSIGNFROM); match(LCURLY); tr=type_name_def(); match(RCURLY); target.TargetStrategy = TargetStrategyEnum.Assignable; target.AssignType = tr; break; } case CUSTOMMATCHER: { match(CUSTOMMATCHER); match(LCURLY); tr=type_name_def(); match(RCURLY); target.TargetStrategy = TargetStrategyEnum.Custom; target.CustomMatcherType = tr; break; } case ID: { { namespaceRegEx=identifier(); target.TargetStrategy = TargetStrategyEnum.Namespace; target.NamespaceRoot = namespaceRegEx; { switch ( LA(1) ) { case EXCLUDES: { match(EXCLUDES); match(LCURLY); type_list(target.Excludes); match(RCURLY); break; } case RBRACK: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } } break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } match(RBRACK); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { { // ( ... )* for (;;) { if ((LA(1)==INCLUDE)) { include(aspect); } else { goto _loop33_breakloop; } } _loop33_breakloop: ; } // ( ... )* { // ( ... )* for (;;) { if ((LA(1)==POINTCUT)) { pointcut(aspect); } else { goto _loop35_breakloop; } } _loop35_breakloop: ; } // ( ... )* } match(END); } catch (RecognitionException ex) { reportError(ex); consume(); consumeUntil(tokenSet_4_); } }