public void SingletonInstancesAreEqual() { ITestObject test1 = (ITestObject)factory.GetObject("test1"); ITestObject test1_1 = (ITestObject)factory.GetObject("test1"); Assert.AreEqual(test1, test1_1, "Singleton instances =="); test1.Age = 25; Assert.AreEqual(test1.Age, test1_1.Age); test1.Age = 250; Assert.AreEqual(test1.Age, test1_1.Age); IAdvised pc1 = (IAdvised)test1; IAdvised pc2 = (IAdvised)test1_1; Assert.AreEqual(pc1.Advisors, pc2.Advisors); int oldLength = pc1.Advisors.Count; NopInterceptor di = new NopInterceptor(); pc1.AddAdvice(1, di); Assert.AreEqual(pc1.Advisors, pc2.Advisors); Assert.AreEqual(oldLength + 1, pc2.Advisors.Count, "Now have one more advisor"); Assert.AreEqual(di.Count, 0); test1.Age = (5); Assert.AreEqual(test1_1.Age, test1.Age); Assert.AreEqual(3, di.Count); }
/// <summary> /// Initialization method. /// </summary> /// <param name="advised">The proxy configuration.</param> /// <param name="proxy"> /// The current <see cref="Spring.Aop.Framework.IAopProxy"/> implementation. /// </param> protected void Initialize(IAdvised advised, IAopProxy proxy) { this.m_advised = advised; this.m_targetType = advised.TargetSource.TargetType; // initialize target if (advised.TargetSource.IsStatic) { this.m_targetSourceWrapper = new StaticTargetSourceWrapper(advised.TargetSource); } else { this.m_targetSourceWrapper = new DynamicTargetSourceWrapper(advised.TargetSource); } // initialize introduction advice this.m_introductions = new IAdvice[advised.Introductions.Length]; for (int i = 0; i < advised.Introductions.Length; i++) { this.m_introductions[i] = advised.Introductions[i].Advice; // set target proxy on introduction instance if it implements ITargetAware if (this.m_introductions[i] is ITargetAware) { ((ITargetAware)this.m_introductions[i]).TargetProxy = proxy; } } }
public void ProxyTransparentProxy() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues(); ctorArgs.AddNamedArgumentValue("objectType", typeof(ITestObject)); of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(TransparentProxyFactory), ctorArgs, null)); TestAutoProxyCreator apc = new TestAutoProxyCreator(of); of.AddObjectPostProcessor(apc); ITestObject o = of.GetObject("bar") as ITestObject; Assert.IsTrue(AopUtils.IsAopProxy(o)); // ensure interceptors get called o.Foo(); Assert.AreEqual(1, apc.NopInterceptor.Count); IAdvised advised = (IAdvised)o; // ensure target was called object target = advised.TargetSource.GetTarget(); Assert.AreEqual(1, TransparentProxyFactory.GetRealProxy(target).Count); }
/// <summary> /// Gets the list of /// <see langword="static"/> interceptors and dynamic interception /// advice that may apply to the supplied <paramref name="method"/> /// invocation. /// </summary> /// <param name="config">The proxy configuration.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method to evaluate interceptors for. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// A <see cref="System.Collections.IList"/> of /// <see cref="AopAlliance.Intercept.IMethodInterceptor"/> (if there's /// a dynamic method matcher that needs evaluation at runtime). /// </returns> public static IList CalculateInterceptors( IAdvised config, object proxy, MethodInfo method, Type targetType) { IList interceptors = new ArrayList(config.Advisors.Length); foreach (IAdvisor advisor in config.Advisors) { if (advisor is IPointcutAdvisor) { IPointcutAdvisor pointcutAdvisor = (IPointcutAdvisor) advisor; if (pointcutAdvisor.Pointcut.TypeFilter.Matches(targetType)) { IMethodInterceptor interceptor = (IMethodInterceptor) GlobalAdvisorAdapterRegistry.Instance.GetInterceptor(advisor); IMethodMatcher mm = pointcutAdvisor.Pointcut.MethodMatcher; if (mm.Matches(method, targetType)) { if (mm.IsRuntime) { // Creating a new object instance in the GetInterceptor() method // isn't a problem as we normally cache created chains... interceptors.Add(new InterceptorAndDynamicMethodMatcher(interceptor, mm)); } else { interceptors.Add(interceptor); } } } } } return interceptors; }
private SimpleBeforeAdviceImpl GetAdviceImpl(ITestObject to) { IAdvised advised = (IAdvised)to; IAdvisor advisor = advised.Advisors[0]; return((SimpleBeforeAdviceImpl)advisor.Advice); }
public void ReplaceAdvisor() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); IAdvisor advisor1 = new DefaultPointcutAdvisor(cba1); IAdvisor advisor2 = new DefaultPointcutAdvisor(cba2); pf.AddAdvisor(advisor1); pf.AddAdvice(nop); ITestObject proxied = (ITestObject)pf.GetProxy(); // Use the type cast feature // Replace etc methods on advised should be same as on ProxyFactory IAdvised advised = (IAdvised)proxied; proxied.Age = 5; Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(0, cba2.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(advised.ReplaceAdvisor(null, null)); Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2)); Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null)); Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2)); Assert.AreEqual(advisor2, pf.Advisors[0]); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(1, cba2.GetCalls()); Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); }
public void CanGetFactoryReferenceAndManipulate() { ITestObject to = (ITestObject)factory.GetObject("test1"); // no exception string dummy = to.Name; IAdvised config = (IAdvised)to; Assert.AreEqual(1, config.Advisors.Count, "Object should have only one advisors"); Exception ex = new NotSupportedException("Invoke"); // Add evil interceptor to head of list config.AddAdvice(0, new EvilMethodInterceptor(ex)); Assert.AreEqual(2, config.Advisors.Count, "The advisor count is wrong after adding an advisor programmatically."); try { // evil interceptor should throw exception dummy = to.Name; Assert.Fail("Evil interceptor added programmatically should fail all method calls, but it didn't"); } catch (Exception thrown) { Assert.AreEqual(thrown, ex, "The thrown exception is not the one we were looking for."); } }
/// <summary> /// If possible, checks for advisor duplicates on the supplied <paramref name="advisedSupport"/> and /// eliminates them. /// </summary> protected virtual void EliminateDuplicateAdvisors(AdvisedSupport advisedSupport) { if (advisedSupport.TargetSource is SingletonTargetSource && IsAopProxyType(advisedSupport.TargetSource)) { IAdvised innerProxy = (IAdvised)advisedSupport.TargetSource.GetTarget(); // eliminate duplicate advisors List <IAdvisor> thisAdvisors = new List <IAdvisor>(advisedSupport.Advisors); foreach (IAdvisor innerAdvisor in innerProxy.Advisors) { foreach (IAdvisor thisAdvisor in thisAdvisors) { if (ReferenceEquals(thisAdvisor, innerAdvisor) || (thisAdvisor.GetType() == typeof(DefaultPointcutAdvisor) && thisAdvisor.Equals(innerAdvisor) ) ) { advisedSupport.RemoveAdvisor(thisAdvisor); } } } // elimination of duplicate introductions is not necessary // since they do not propagate to nested proxy anyway } }
public void SupportsTransparentProxyAsTarget() { AppDomain domain = null; try { AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = Environment.CurrentDirectory; domain = AppDomain.CreateDomain("Spring", new Evidence(AppDomain.CurrentDomain.Evidence), setup); object command = domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName, typeof(RemotableCommand).FullName); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.AddInterface(typeof(ICommand)); fac.AddAdvice(new NopInterceptor()); fac.Target = command; IAdvised advised = fac.GetObject() as IAdvised; Assert.IsNotNull(advised); ICommand cmd = fac.GetObject() as ICommand; Assert.IsNotNull(cmd); cmd.Execute(); } finally { AppDomain.Unload(domain); } }
/// <summary> /// Deserialization constructor. /// </summary> /// <param name="info">Serialization data.</param> /// <param name="context">Serialization context.</param> protected AdvisedProxy(SerializationInfo info, StreamingContext context) { m_advised = (IAdvised)info.GetValue("advised", typeof(IAdvised)); m_introductions = (IAdvice[])info.GetValue("introductions", typeof(IAdvice[])); m_targetSource = (ITargetSource)info.GetValue("targetSource", typeof(ITargetSource)); m_targetType = (Type)info.GetValue("targetType", typeof(Type)); }
/// <summary> /// Gets the list of /// <see langword="static"/> interceptors and dynamic interception /// advice that may apply to the supplied <paramref name="method"/> /// invocation. /// </summary> /// <param name="config">The proxy configuration.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method to evaluate interceptors for. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// A <see cref="System.Collections.IList"/> of /// <see cref="AopAlliance.Intercept.IMethodInterceptor"/> (if there's /// a dynamic method matcher that needs evaluation at runtime). /// </returns> public static IList <object> CalculateInterceptors( IAdvised config, object proxy, MethodInfo method, Type targetType) { IList <object> interceptors = new List <object>(config.Advisors.Count); foreach (IAdvisor advisor in config.Advisors) { if (advisor is IPointcutAdvisor) { IPointcutAdvisor pointcutAdvisor = (IPointcutAdvisor)advisor; if (pointcutAdvisor.Pointcut.TypeFilter.Matches(targetType)) { IMethodInterceptor interceptor = (IMethodInterceptor)GlobalAdvisorAdapterRegistry.Instance.GetInterceptor(advisor); IMethodMatcher mm = pointcutAdvisor.Pointcut.MethodMatcher; if (mm.Matches(method, targetType)) { if (mm.IsRuntime) { // Creating a new object instance in the GetInterceptor() method // isn't a problem as we normally cache created chains... interceptors.Add(new InterceptorAndDynamicMethodMatcher(interceptor, mm)); } else { interceptors.Add(interceptor); } } } } } return(interceptors); }
/// <summary> /// Creates a new instance of the /// <see cref="CompositionAopProxyTypeBuilder"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> public CompositionAopProxyTypeBuilder(IAdvised advised) { this.advised = advised; Name = PROXY_TYPE_NAME; BaseType = typeof(BaseCompositionAopProxy); TargetType = advised.TargetSource.TargetType.IsInterface ? typeof(object) : advised.TargetSource.TargetType; Interfaces = GetProxiableInterfaces(advised.Interfaces); ProxyTargetAttributes = advised.ProxyTargetAttributes; }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { IList cached = (IList)this.methodCache[method]; if (cached == null) { // recalculate... cached = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); this.methodCache[method] = cached; } return cached; }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList<object> GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { IList<object> cached; if (!this.methodCache.TryGetValue(method, out cached)) { // recalculate... cached = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); this.methodCache[method] = cached; } return cached; }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { IList cached = (IList)this.methodCache[method]; if (cached == null) { // recalculate... cached = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); this.methodCache[method] = cached; } return(cached); }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList <object> GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { IList <object> cached; if (!this.methodCache.TryGetValue(method, out cached)) { // recalculate... cached = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); this.methodCache[method] = cached; } return(cached); }
public void CanAddAndRemoveAspectInterfacesOnSingletonByCasting() { ITestObject it = (ITestObject)factory.GetObject("test1"); IAdvised pc = (IAdvised)it; object name = it.Age; NopInterceptor di = new NopInterceptor(); pc.AddAdvice(0, di); Assert.AreEqual(0, di.Count); it.Age = 25; Assert.AreEqual(25, it.Age); Assert.AreEqual(2, di.Count); }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList <object> GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { if (!methodCache.TryGetValue(method, out var interceptors)) { lock (methodCache) { if (!methodCache.TryGetValue(method, out interceptors)) { interceptors = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); methodCache[method] = interceptors; } } } return(interceptors); }
private void CheckWillTranslateExceptions(object o) { IAdvised advised = o as IAdvised; Assert.IsNotNull(advised); foreach (IAdvisor advisor in advised.Advisors) { PersistenceExceptionTranslationAdvisor peta = advisor as PersistenceExceptionTranslationAdvisor; if (peta != null) { return; } } Assert.Fail("No translation"); }
public void IndexOfMethods() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); IAdvisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); IAdvised advised = (IAdvised)pf.GetProxy(); // Can use advised and ProxyFactory interchangeably advised.AddAdvice(nop); pf.AddAdvisor(advisor); Assert.AreEqual(-1, pf.IndexOf((IInterceptor)null)); Assert.AreEqual(-1, pf.IndexOf(new NopInterceptor())); Assert.AreEqual(0, pf.IndexOf(nop)); Assert.AreEqual(-1, advised.IndexOf((IAdvisor)null)); Assert.AreEqual(1, pf.IndexOf(advisor)); Assert.AreEqual(-1, advised.IndexOf(new DefaultPointcutAdvisor(null))); }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Oragon.Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Oragon.Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList <object> GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { #if !NET_4_0 IList <object> cached; cacheLock.EnterReadLock(); try { if (this.methodCache.TryGetValue(method, out cached)) { return(cached); } } finally { cacheLock.ExitReadLock(); } // Apparently not in the cache - calculate the value outside of any locks then enter upgradeable read lock and check again IList <object> calculated = AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, method, targetType); cacheLock.EnterUpgradeableReadLock(); try { if (!this.methodCache.TryGetValue(method, out cached)) { // Still not in the cache - enter write lock and add the pre-calculated value cacheLock.EnterWriteLock(); try { cached = calculated; this.methodCache[method] = cached; } finally { cacheLock.ExitWriteLock(); } } } finally { cacheLock.ExitUpgradeableReadLock(); } return(cached); #else return(methodCache.GetOrAdd(method, m => AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, m, targetType))); #endif }
/// <summary> /// Initialization method. /// </summary> /// <param name="advised">The proxy configuration.</param> /// <param name="proxy"> /// The current <see cref="Spring.Aop.Framework.IAopProxy"/> implementation. /// </param> protected void Initialize(IAdvised advised, IAopProxy proxy) { this.m_advised = advised; this.m_targetSource = advised.TargetSource; this.m_targetType = advised.TargetSource.TargetType; // initialize introduction advice this.m_introductions = new IAdvice[advised.Introductions.Count]; for (int i = 0; i < advised.Introductions.Count; i++) { this.m_introductions[i] = advised.Introductions[i].Advice; // set target proxy on introduction instance if it implements ITargetAware if (this.m_introductions[i] is ITargetAware) { ((ITargetAware)this.m_introductions[i]).TargetProxy = proxy; } } }
/// <summary> /// Add PersistenceExceptionTranslationAdvice to candidate object if it is a match. /// Create AOP proxy if necessary or add advice to existing advice chain. /// </summary> /// <param name="instance">The new object instance.</param> /// <param name="objectName">The name of the object.</param> /// <returns> /// The object instance to use, wrapped with either the original or a wrapped one. /// </returns> /// <exception cref="Spring.Objects.ObjectsException"> /// In case of errors. /// </exception> public object PostProcessAfterInitialization(object instance, string objectName) { IAdvised advised = instance as IAdvised; Type targetType; if (advised != null) { targetType = advised.TargetSource.TargetType; } else { targetType = instance.GetType(); } if (targetType == null) { // Can't do much here return(instance); } if (AopUtils.CanApply(this.persistenceExceptionTranslationAdvisor, targetType, ReflectionUtils.GetInterfaces(targetType))) { if (advised != null) { advised.AddAdvisor(this.persistenceExceptionTranslationAdvisor); return(instance); } else { ProxyFactory proxyFactory = new ProxyFactory(instance); // copy our properties inherited from ProxyConfig proxyFactory.CopyFrom(this); proxyFactory.AddAdvisor(this.persistenceExceptionTranslationAdvisor); return(proxyFactory.GetProxy()); } } else { return(instance); } }
public void PassEmptyInterceptorNamesArray_WithTargetThatImplementsAnInterfaceCanBeCastToSaidInterface() { IObjectFactory factory = A.Fake <IObjectFactory>(); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { }; fac.Target = new GoodCommand(); fac.ObjectFactory = factory; IAdvised advised = fac.GetObject() as IAdvised; Assert.IsNotNull(advised); ICommand cmd = fac.GetObject() as ICommand; Assert.IsNotNull(cmd); DoesntImplementAnyInterfaces obj = fac.GetObject() as DoesntImplementAnyInterfaces; Assert.IsNull(obj); }
/// <summary> /// Creates a new instance of the /// <see cref="DecoratorAopProxyTypeBuilder"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> public DecoratorAopProxyTypeBuilder(IAdvised advised) { if (!ReflectionUtils.IsTypeVisible(advised.TargetSource.TargetType, DynamicProxyManager.ASSEMBLY_NAME)) { throw new AopConfigException(String.Format( "Cannot create decorator-based IAopProxy for a non visible class [{0}]", advised.TargetSource.TargetType.FullName)); } if (advised.TargetSource.TargetType.IsSealed) { throw new AopConfigException(String.Format( "Cannot create decorator-based IAopProxy for a sealed class [{0}]", advised.TargetSource.TargetType.FullName)); } this.advised = advised; Name = PROXY_TYPE_NAME; TargetType = advised.TargetSource.TargetType.IsInterface ? typeof(object) : advised.TargetSource.TargetType; BaseType = TargetType; Interfaces = GetProxiableInterfaces(advised.Interfaces); ProxyTargetAttributes = advised.ProxyTargetAttributes; }
public void Registered() { Assert.IsNotNull(NamespaceParserRegistry.GetParser("http://www.springframework.net/aop")); IPointcut pointcut = ctx["getDescriptionCalls"] as IPointcut; Assert.IsNotNull(pointcut); Assert.IsFalse(AopUtils.IsAopProxy(pointcut)); ITestObject testObject = ctx["testObject"] as ITestObject; Assert.IsNotNull(testObject); Assert.IsTrue(AopUtils.IsAopProxy(testObject), "Object should be an AOP proxy"); IAdvised advised = testObject as IAdvised; Assert.IsNotNull(advised); IAdvisor[] advisors = advised.Advisors; Assert.IsTrue(advisors.Length > 0, "Advisors should not be empty"); }
/// <summary> /// Creates a new instance of the /// <see cref="Spring.Aop.Framework.DynamicProxy.AdvisedProxy"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> /// <param name="proxy">The proxy.</param> public AdvisedProxy(IAdvised advised, IAopProxy proxy) { Initialize(advised, proxy); }
/// <summary> /// Creates a new instance of the <see cref="AdvisedProxy"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> protected AdvisedProxy(IAdvised advised) { m_advised = advised; }
public void CanAddAndRemoveAspectInterfacesOnPrototype() { try { ITimeStamped ts = (ITimeStamped)factory.GetObject("test2"); Assert.Fail("Shouldn't implement ITimeStamped before manipulation"); } catch (InvalidCastException) { } IAdvised config = (IAdvised)factory.GetObject("&test2"); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(time); IIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); // add to front of introduction chain int oldCount = config.Introductions.Count; config.AddIntroduction(0, advisor); Assert.IsTrue(config.Introductions.Count == oldCount + 1); ITimeStamped ts2 = (ITimeStamped)factory.GetObject("test2"); Assert.IsTrue(ts2.TimeStamp == new DateTime(time)); // Can remove config.RemoveIntroduction(advisor); Assert.IsTrue(config.Introductions.Count == oldCount); // Existing reference will still work object o = ts2.TimeStamp; // But new proxies should not implement ITimeStamped try { ts2 = (ITimeStamped)factory.GetObject("test2"); Assert.Fail("Should no longer implement ITimeStamped"); } catch (InvalidCastException) { } // Now check non-effect of removing interceptor that isn't there ITestObject it = (ITestObject)factory.GetObject("test2"); config = (IAdvised)it; oldCount = config.Advisors.Count; config.RemoveAdvice(new DebugAdvice()); Assert.IsTrue(config.Advisors.Count == oldCount); DebugAdvice debugInterceptor = new DebugAdvice(); config.AddAdvice(0, debugInterceptor); object foo = it.Spouse; Assert.AreEqual(1, debugInterceptor.Count); config.RemoveAdvice(debugInterceptor); foo = it.Spouse; // not invoked again Assert.IsTrue(debugInterceptor.Count == 1); }
/// <summary> /// Creates a new instance of the /// <see cref="Spring.Aop.Framework.DynamicProxy.BaseCompositionAopProxy"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> public BaseCompositionAopProxy(IAdvised advised) : base(advised) { base.Initialize(advised, this); }
/// <summary> /// Creates a new instance of the /// <see cref="Oragon.Spring.Aop.Framework.DynamicProxy.BaseCompositionAopProxy"/> class. /// </summary> /// <param name="advised">The proxy configuration.</param> public BaseCompositionAopProxy(IAdvised advised) : base(advised) { base.Initialize(advised, this); }
public AdvisedProxy(IAdvised advised) : this() { this.advised = advised; }
/// <summary> /// Gets the list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </summary> /// <param name="advised">The proxy configuration object.</param> /// <param name="proxy">The object proxy.</param> /// <param name="method"> /// The method for which the interceptors are to be evaluated. /// </param> /// <param name="targetType"> /// The <see cref="System.Type"/> of the target object. /// </param> /// <returns> /// The list of <see cref="AopAlliance.Intercept.IInterceptor"/> and /// <see cref="Spring.Aop.Framework.InterceptorAndDynamicMethodMatcher"/> /// instances for the supplied <paramref name="proxy"/>. /// </returns> public IList <object> GetInterceptors(IAdvised advised, object proxy, MethodInfo method, Type targetType) { return(methodCache.GetOrAdd(method, m => AdvisorChainFactoryUtils.CalculateInterceptors(advised, proxy, m, targetType))); }