KWInvocationCapturer.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWInvocationCapturer.h"
  7. #import "KWWorkarounds.h"
  8. #import "NSInvocation+KiwiAdditions.h"
  9. @implementation KWInvocationCapturer
  10. #pragma mark - Initializing
  11. - (id)initWithDelegate:(id)aDelegate {
  12. return [self initWithDelegate:aDelegate userInfo:nil];
  13. }
  14. - (id)initWithDelegate:(id)aDelegate userInfo:(NSDictionary *)aUserInfo {
  15. delegate = aDelegate;
  16. userInfo = aUserInfo;
  17. return self;
  18. }
  19. + (id)invocationCapturerWithDelegate:(id)aDelegate {
  20. return [self invocationCapturerWithDelegate:aDelegate userInfo:nil];
  21. }
  22. + (id)invocationCapturerWithDelegate:(id)aDelegate userInfo:(NSDictionary *)aUserInfo {
  23. return [[self alloc] initWithDelegate:aDelegate userInfo:aUserInfo];
  24. }
  25. #pragma mark - Properties
  26. @synthesize delegate;
  27. @synthesize userInfo;
  28. #pragma mark - Capturing Invocations
  29. - (void)KW_captureInvocation:(NSInvocation *)anInvocation {
  30. [self.delegate invocationCapturer:self didCaptureInvocation:anInvocation];
  31. }
  32. #pragma mark - Handling Invocations
  33. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
  34. return [self.delegate invocationCapturer:self methodSignatureForSelector:aSelector];
  35. }
  36. - (void)forwardInvocation:(NSInvocation *)anInvocation {
  37. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  38. @try {
  39. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  40. [self KW_captureInvocation:anInvocation];
  41. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  42. } @catch (NSException *exception) {
  43. KWSetExceptionFromAcrossInvocationBoundary(exception);
  44. }
  45. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  46. }
  47. #pragma mark - Whitelisted NSObject Methods
  48. // The return values from these methods should never be needed, so just call
  49. // the super implementation after capturing the invocation.
  50. - (BOOL)isEqual:(id)anObject {
  51. NSInvocation *invocation = [NSInvocation invocationWithTarget:self selector:_cmd messageArguments:&anObject];
  52. [self KW_captureInvocation:invocation];
  53. return [super isEqual:anObject];
  54. }
  55. - (NSUInteger)hash {
  56. NSInvocation *invocation = [NSInvocation invocationWithTarget:self selector:_cmd];
  57. [self KW_captureInvocation:invocation];
  58. return [super hash];
  59. }
  60. - (NSString *)description {
  61. NSInvocation *invocation = [NSInvocation invocationWithTarget:self selector:_cmd];
  62. [self KW_captureInvocation:invocation];
  63. return [super description];
  64. }
  65. @end