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