private bool IsMethodBootstrapInterceptorNeeded(IEnumerable <Type> types) { var methodInterceptorsMap = AopInterceptorContainer.FindInterceptors(AttributeTargets.Method); return(types.SelectMany(type => MethodSelector.GetAllMethods(type)) .SelectMany(m => m.GetCustomAttributes()) .Select(attr => attr.GetType()) .Any(attrType => methodInterceptorsMap.ContainsKey(attrType))); }
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); }