public void Intercept(IInvocation invocation)
        {
            object     newProxy;
            MethodInfo proxyMethod;

            var aopInterceptors       = GetMethodInterceptors(invocation.MethodInvocationTarget);
            var remainingInterceptors = GetRemainingInterceptors(invocation.Proxy);
            var mergeInterceptors     = aopInterceptors.Union(remainingInterceptors);

            newProxy    = InterfaceProxyFactory.CreateInterfaceProxy(invocation.InvocationTarget, mergeInterceptors.ToArray());
            proxyMethod = invocation.Method; // same interface method to the new proxy

            try
            {
                invocation.ReturnValue = proxyMethod.Invoke(newProxy, invocation.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }

                throw ex;
            }
        }
Пример #2
0
        public override void PostBuildUp(ref BuilderContext context)
        {
            if (context.Existing == null)
            {
                base.PostBuildUp(ref context);
                return;
            }

            var container    = context.Container;
            var interceptors = new List <IInterceptor>();
            var types        = GetUnproxiedTypes(context.Existing);

            // class interceptor
            var typeInterceptorsMap = AopInterceptorContainer.FindInterceptors(AttributeTargets.Class | AttributeTargets.Interface);

            foreach (var type in types)
            {
                foreach (var attribute in type.GetCustomAttributes(false))
                {
                    if (typeInterceptorsMap.ContainsKey(attribute.GetType()))
                    {
                        interceptors.AddRange(typeInterceptorsMap[attribute.GetType()].Select(t => (IInterceptor)container.Resolve(t)));
                    }
                }
            }

            // method interceptor
            if (IsMethodBootstrapInterceptorNeeded(types))
            {
                interceptors.Add(container.Resolve <AopMethodBootstrapInterceptor>());
            }

            if (interceptors.Count() > 0)
            {
                if (ProxyUtil.IsProxy(context.Existing))
                {
                    AddInterceptorsToProxy(context.Existing, interceptors);
                }
                else
                {
                    context.Existing = InterfaceProxyFactory.CreateInterfaceProxy(context.Existing, interceptors.ToArray());
                }
            }

            if (ProxyUtil.IsProxy(context.Existing) && !HasInterceptors(context.Existing))
            {
                throw new InvalidOperationException($"No interceptor found for this proxy bean {context.Existing}.");
            }

            base.PostBuildUp(ref context);
        }