/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

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