forked from joecannatti/Objective-C-Koans
/
KWIntercept.m
289 lines (223 loc) · 10.2 KB
/
KWIntercept.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "KWIntercept.h"
#import "KWMessagePattern.h"
#import "KWMessageSpying.h"
#import "KWStub.h"
static const char * const KWInterceptClassSuffix = "_KWIntercept";
static NSMutableDictionary *KWObjectStubs = nil;
static NSMutableDictionary *KWMessageSpies = nil;
#pragma mark -
#pragma mark Intercept Enabled Method Implementations
void KWInterceptedForwardInvocation(id anObject, SEL aSelector, NSInvocation* anInvocation);
void KWInterceptedDealloc(id anObject, SEL aSelector);
Class KWInterceptedClass(id anObject, SEL aSelector);
Class KWInterceptedSuperclass(id anObject, SEL aSelector);
#pragma mark -
#pragma mark Getting Forwarding Implementations
IMP KWRegularForwardingImplementation(void) {
return class_getMethodImplementation([NSObject class], @selector(KWNonExistantSelector));
}
IMP KWStretForwardingImplementation(void) {
return class_getMethodImplementation_stret([NSObject class], @selector(KWNonExistantSelector));
}
IMP KWForwardingImplementationForMethodEncoding(const char* encoding) {
#if TARGET_CPU_ARM
const NSUInteger stretLengthThreshold = 4;
#elif TARGET_CPU_X86
const NSUInteger stretLengthThreshold = 8;
#else
// TODO: This just makes an assumption right now. Expand to support all
// official architectures correctly.
const NSUInteger stretLengthThreshold = 8;
#endif // #if TARGET_CPU_ARM
NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:encoding];
if (*[signature methodReturnType] == '{' && [signature methodReturnLength] > stretLengthThreshold) {
NSLog(@"Warning: The Objective-C runtime appears to have bugs when forwarding messages with certain struct layouts as return types, so if a crash occurs this could be the culprit");
return KWStretForwardingImplementation();
} else {
return KWRegularForwardingImplementation();
}
}
#pragma mark -
#pragma mark Getting Intercept Class Information
BOOL KWObjectIsClass(id anObject) {
return class_isMetaClass(object_getClass(anObject));
}
BOOL KWClassIsInterceptClass(Class aClass) {
const char *name = class_getName(aClass);
char *result = strstr(name, KWInterceptClassSuffix);
return result != nil;
}
NSString *KWInterceptClassNameForClass(Class aClass) {
const char *className = class_getName(aClass);
return [NSString stringWithFormat:@"%s%s", className, KWInterceptClassSuffix];
}
Class KWInterceptClassForCanonicalClass(Class canonicalClass) {
NSString *interceptClassName = KWInterceptClassNameForClass(canonicalClass);
Class interceptClass = NSClassFromString(interceptClassName);
if (interceptClass != nil)
return interceptClass;
interceptClass = objc_allocateClassPair(canonicalClass, [interceptClassName UTF8String], 0);
objc_registerClassPair(interceptClass);
class_addMethod(interceptClass, @selector(forwardInvocation:), (IMP)KWInterceptedForwardInvocation, "v@:@");
class_addMethod(interceptClass, @selector(dealloc), (IMP)KWInterceptedDealloc, "v@:");
class_addMethod(interceptClass, @selector(class), (IMP)KWInterceptedClass, "#@:");
class_addMethod(interceptClass, @selector(superclass), (IMP)KWInterceptedSuperclass, "#@:");
Class interceptMetaClass = object_getClass(interceptClass);
class_addMethod(interceptMetaClass, @selector(forwardInvocation:), (IMP)KWInterceptedForwardInvocation, "v@:@");
return interceptClass;
}
Class KWRealClassForClass(Class aClass) {
if (KWClassIsInterceptClass(aClass))
return [aClass superclass];
return aClass;
}
#pragma mark -
#pragma mark Enabling Intercepting
static BOOL IsTollFreeBridged(Class class, id obj)
{
// this is a naive check, but good enough for the purposes of failing fast
return [NSStringFromClass(class) hasPrefix:@"NSCF"];
}
// Canonical class is the non-intercept, non-metaclass, class for an object.
//
// (e.g. [Animal class] would be canonical, not
// object_getClass([Animal class]), if the Animal class has not been touched
// by the intercept mechanism.
Class KWSetupObjectInterceptSupport(id anObject) {
Class objectClass = object_getClass(anObject);
if (IsTollFreeBridged(objectClass, anObject)) {
[NSException raise:@"KWTollFreeBridgingInterceptException" format:@"Attempted to stub object of class %@. Kiwi does not support setting expectation or stubbing methods on toll-free bridged objects.", NSStringFromClass(objectClass)];
}
if (KWClassIsInterceptClass(objectClass))
return objectClass;
BOOL objectIsClass = KWObjectIsClass(anObject);
Class canonicalClass = objectIsClass ? anObject : objectClass;
Class canonicalInterceptClass = KWInterceptClassForCanonicalClass(canonicalClass);
Class interceptClass = objectIsClass ? object_getClass(canonicalInterceptClass) : canonicalInterceptClass;
object_setClass(anObject, interceptClass);
return interceptClass;
}
void KWSetupMethodInterceptSupport(Class interceptClass, SEL aSelector) {
BOOL isMetaClass = class_isMetaClass(interceptClass);
Method method = isMetaClass ? class_getClassMethod(interceptClass, aSelector)
: class_getInstanceMethod(interceptClass, aSelector);
if (method == nil) {
[NSException raise:NSInvalidArgumentException format:@"cannot setup intercept support for -%@ because there is no such method exists",
NSStringFromSelector(aSelector)];
}
const char *encoding = method_getTypeEncoding(method);
IMP forwardingImplementation = KWForwardingImplementationForMethodEncoding(encoding);
class_addMethod(interceptClass, aSelector, forwardingImplementation, encoding);
}
#pragma mark -
#pragma mark Intercept Enabled Method Implementations
void KWInterceptedForwardInvocation(id anObject, SEL aSelector, NSInvocation* anInvocation) {
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
NSMutableDictionary *spyArrayDictionary = [KWMessageSpies objectForKey:key];
for (KWMessagePattern *messagePattern in spyArrayDictionary) {
if ([messagePattern matchesInvocation:anInvocation]) {
NSArray *spies = [spyArrayDictionary objectForKey:messagePattern];
for (NSValue *spyWrapper in spies) {
id<KWMessageSpying> spy = [spyWrapper nonretainedObjectValue];
[spy object:anObject didReceiveInvocation:anInvocation];
}
}
}
NSMutableArray *stubs = [KWObjectStubs objectForKey:key];
for (KWStub *stub in stubs) {
if ([stub processInvocation:anInvocation])
return;
}
Class interceptClass = object_getClass(anObject);
Class originalClass = class_getSuperclass(interceptClass);
anObject->isa = originalClass;
[anInvocation invoke];
anObject->isa = interceptClass;
}
void KWInterceptedDealloc(id anObject, SEL aSelector) {
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
[KWMessageSpies removeObjectForKey:key];
[KWObjectStubs removeObjectForKey:key];
Class interceptClass = object_getClass(anObject);
Class originalClass = class_getSuperclass(interceptClass);
anObject->isa = originalClass;
[anObject dealloc];
}
Class KWInterceptedClass(id anObject, SEL aSelector) {
Class interceptClass = object_getClass(anObject);
Class originalClass = class_getSuperclass(interceptClass);
return originalClass;
}
Class KWInterceptedSuperclass(id anObject, SEL aSelector) {
Class interceptClass = object_getClass(anObject);
Class originalClass = class_getSuperclass(interceptClass);
Class originalSuperclass = class_getSuperclass(originalClass);
return originalSuperclass;
}
#pragma mark -
#pragma mark Managing Objects Stubs
void KWAssociateObjectStub(id anObject, KWStub *aStub) {
if (KWObjectStubs == nil)
KWObjectStubs = [[NSMutableDictionary alloc] init];
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
NSMutableArray *stubs = [KWObjectStubs objectForKey:key];
if (stubs == nil) {
stubs = [[NSMutableArray alloc] init];
[KWObjectStubs setObject:stubs forKey:key];
[stubs release];
}
NSUInteger stubCount = [stubs count];
for (int i = 0; i < stubCount; ++i) {
KWStub *existingStub = [stubs objectAtIndex:i];
if ([aStub.messagePattern isEqualToMessagePattern:existingStub.messagePattern]) {
[stubs removeObjectAtIndex:i];
break;
}
}
[stubs addObject:aStub];
}
void KWClearObjectStubs(id anObject) {
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
[KWObjectStubs removeObjectForKey:key];
}
void KWClearAllObjectStubs(void) {
[KWObjectStubs removeAllObjects];
}
#pragma mark -
#pragma mark Managing Message Spies
void KWAssociateMessageSpy(id anObject, id aSpy, KWMessagePattern *aMessagePattern) {
if (KWMessageSpies == nil)
KWMessageSpies = [[NSMutableDictionary alloc] init];
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
NSMutableDictionary *spies = [KWMessageSpies objectForKey:key];
if (spies == nil) {
spies = [[NSMutableDictionary alloc] init];
[KWMessageSpies setObject:spies forKey:key];
[spies release];
}
NSMutableArray *messagePatternSpies = [spies objectForKey:aMessagePattern];
if (messagePatternSpies == nil) {
messagePatternSpies = [[NSMutableArray alloc] init];
[spies setObject:messagePatternSpies forKey:aMessagePattern];
[messagePatternSpies release];
}
NSValue *spyWrapper = [NSValue valueWithNonretainedObject:aSpy];
if ([messagePatternSpies containsObject:spyWrapper])
return;
[messagePatternSpies addObject:spyWrapper];
}
void KWClearObjectSpy(id anObject, id aSpy, KWMessagePattern *aMessagePattern) {
NSValue *key = [NSValue valueWithNonretainedObject:anObject];
NSMutableDictionary *spyArrayDictionary = [KWMessageSpies objectForKey:key];
NSMutableArray *spies = [spyArrayDictionary objectForKey:aMessagePattern];
NSValue *spyWrapper = [NSValue valueWithNonretainedObject:aSpy];
[spies removeObject:spyWrapper];
}
void KWClearAllMessageSpies(void) {
[KWMessageSpies removeAllObjects];
}