PageRenderTime 243ms CodeModel.GetById 121ms app.highlight 5ms RepoModel.GetById 115ms app.codeStats 1ms

/src/LinFu.AOP/Extensions/ExceptionHandlerInterceptionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 104 lines | 52 code | 9 blank | 43 comment | 4 complexity | 629974e4e6ae0b4423e60ec8a77d99c4 MD5 | raw file
  1using System;
  2using LinFu.AOP.Cecil.Interfaces;
  3using LinFu.AOP.Interfaces;
  4using Mono.Cecil;
  5
  6namespace LinFu.AOP.Cecil.Extensions
  7{
  8    /// <summary>
  9    ///     Represents an extension class that adds support for intercepting exceptions thrown at runtime.
 10    /// </summary>
 11    public static class ExceptionHandlerInterceptionExtensions
 12    {
 13        /// <summary>
 14        ///     Enables exception interception on the given type.
 15        /// </summary>
 16        /// <param name="visitable">The target type.</param>
 17        public static void InterceptAllExceptions(this TypeDefinition visitable)
 18        {
 19            var filter = GetMethodFilter();
 20            InterceptExceptions(visitable, filter);
 21        }
 22
 23        /// <summary>
 24        ///     Enables exception interception on the given type.
 25        /// </summary>
 26        /// <param name="visitable">The target type.</param>
 27        public static void InterceptAllExceptions(this AssemblyDefinition visitable)
 28        {
 29            var filter = GetMethodFilter();
 30            InterceptExceptions(visitable, filter);
 31        }
 32
 33        /// <summary>
 34        ///     Enables exception interception on the given type.
 35        /// </summary>
 36        /// <param name="visitable">The target type.</param>
 37        /// <param name="methodFilter">
 38        ///     The <see cref="IMethodFilter" /> instance that will determine which methods should support
 39        ///     exception interception.
 40        /// </param>
 41        public static void InterceptExceptions(this TypeDefinition visitable, IMethodFilter methodFilter)
 42        {
 43            visitable.InterceptExceptions(methodFilter.ShouldWeave);
 44        }
 45
 46        /// <summary>
 47        ///     Enables exception interception on the given type.
 48        /// </summary>
 49        /// <param name="visitable">The target type.</param>
 50        /// <param name="methodFilter">
 51        ///     The <see cref="IMethodFilter" /> instance that will determine which methods should support
 52        ///     exception interception.
 53        /// </param>
 54        public static void InterceptExceptions(this AssemblyDefinition visitable, IMethodFilter methodFilter)
 55        {
 56            visitable.InterceptExceptions(methodFilter.ShouldWeave);
 57        }
 58
 59        /// <summary>
 60        ///     Enables exception interception on the given type.
 61        /// </summary>
 62        /// <param name="visitable">The target type.</param>
 63        /// <param name="methodFilter">
 64        ///     The method filter functor that will determine which methods should support exception
 65        ///     interception.
 66        /// </param>
 67        public static void InterceptExceptions(this AssemblyDefinition visitable,
 68            Func<MethodReference, bool> methodFilter)
 69        {
 70            if (visitable == null)
 71                throw new ArgumentNullException("visitable");
 72
 73            IMethodWeaver catchAllThrownExceptions = new CatchAllThrownExceptions();
 74            visitable.WeaveWith(catchAllThrownExceptions, methodFilter);
 75        }
 76
 77        /// <summary>
 78        ///     Enables exception interception on the given type.
 79        /// </summary>
 80        /// <param name="visitable">The target type.</param>
 81        /// <param name="methodFilter">
 82        ///     The method filter functor that will determine which methods should support exception
 83        ///     interception.
 84        /// </param>
 85        public static void InterceptExceptions(this TypeDefinition visitable,
 86            Func<MethodReference, bool> methodFilter)
 87        {
 88            if (visitable == null)
 89                throw new ArgumentNullException("visitable");
 90
 91            IMethodWeaver catchAllThrownExceptions = new CatchAllThrownExceptions();
 92            visitable.WeaveWith(catchAllThrownExceptions, methodFilter);
 93        }
 94
 95        private static Func<MethodReference, bool> GetMethodFilter()
 96        {
 97            return method =>
 98            {
 99                var actualMethod = method.Resolve();
100                return actualMethod.HasBody;
101            };
102        }
103    }
104}