forked from tuenti/TMInstanceMethodSwizzler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMInstanceMethodSwizzler.m
447 lines (354 loc) · 15.4 KB
/
TMInstanceMethodSwizzler.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// TMInstanceMethodSwizzler.m
//
// Copyright (c) 2014 Tuenti Technologies S.L. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "TMInstanceMethodSwizzler.h"
#import <objc/runtime.h>
#pragma mark - Swizzling assignment data object
@interface TMSwizzlingInformation : NSObject
@property (nonatomic, strong) id object;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, copy) TMSwizzlingBlock swizzlingBlock;
@property (nonatomic, assign) TMOriginalCalling originalCallingTime;
@property (nonatomic, assign) BOOL currentlyRunning;
@property (nonatomic, assign) BOOL shouldBeUnswizzledAfterCallingOriginal;
@end
@implementation TMSwizzlingInformation
- (NSString *)description
{
return [NSString stringWithFormat:@"object: %@; selector: %@; swizzlingBlock: %@; originalCallingTime: %d; currentlyRunning: %d; shouldBeUnswizzledAfterCallingOriginal: %d>",
_object, NSStringFromSelector(_selector), _swizzlingBlock, _originalCallingTime, _currentlyRunning, _shouldBeUnswizzledAfterCallingOriginal];
}
@end
#pragma mark - TMInstanceMethodSwizzler class extension
static TMInstanceMethodSwizzler *sharedInstanceMethodSwizzler = nil;
static NSString *const kTMActionHolderClassName = @"TMActionHolder";
static NSUInteger const kFirstParamIndex = 2;
@interface TMInstanceMethodSwizzler ()
@property (nonatomic, strong) NSMutableArray *swizzlingInformationStorage;
@end
#pragma mark - TMInstanceMethodSwizzler implementation
@implementation TMInstanceMethodSwizzler
#pragma mark - Really strict singleton implementation
+ (TMInstanceMethodSwizzler *)sharedInstanceMethodSwizzler
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstanceMethodSwizzler = [[TMInstanceMethodSwizzler alloc] init];
});
return sharedInstanceMethodSwizzler;
}
+ (id)allocWithZone:(NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!sharedInstanceMethodSwizzler)
{
sharedInstanceMethodSwizzler = [super allocWithZone:zone];
}
});
return sharedInstanceMethodSwizzler;
}
- (id)init
{
__block TMInstanceMethodSwizzler *selfObject;
@synchronized(self)
{
selfObject = [super init];
};
self = selfObject;
return self;
}
- (void)dealloc
{
[self undoAllSwizzling];
}
#pragma mark - Public interface
- (void)swizzleSelector:(SEL)selector fromObject:(id)object withBlock:(TMSwizzlingBlock)swizzlingBlock
{
[self swizzleSelector:selector fromObject:object originalCallingTime:TMOriginalCallingNever swizzlingBlock:swizzlingBlock];
}
- (void)swizzleSelector:(SEL)selector
fromObject:(id)object
originalCallingTime:(TMOriginalCalling)originalCallingTime
swizzlingBlock:(TMSwizzlingBlock)swizzlingBlock
{
NSParameterAssert(selector);
NSParameterAssert(object);
NSParameterAssert(swizzlingBlock);
[self registerActionHolderClassForObject:object selector:selector];
TMSwizzlingInformation *swizzlingInformation = [self swizzlingInformationWithObject:object
selector:selector
swizzlingBlock:swizzlingBlock
originalCallingTime:originalCallingTime];
[self storeSwizzlingInformationReplacingExistingOne:swizzlingInformation];
[self performSwizzlingWithInformation:swizzlingInformation];
}
- (void)undoSwizzlingForSelector:(SEL)selector fromObject:(id)object
{
TMSwizzlingInformation *swizzlingInformation = [self swizzlingInformationForSelector:selector object:object];
if (swizzlingInformation.currentlyRunning)
{
swizzlingInformation.shouldBeUnswizzledAfterCallingOriginal = YES;
return;
}
[self perfomUnswizzlingWithInformation:swizzlingInformation];
}
- (void)undoAllSwizzling
{
while ([self.swizzlingInformationStorage count] > 0)
{
[self perfomUnswizzlingWithInformation:[self.swizzlingInformationStorage firstObject]];
}
}
#pragma mark - Action holder class
- (void)registerActionHolderClassForObject:(id)object selector:(SEL)selector
{
NSString *className = [self actionHolderClassNameForObject:object selector:selector];
Class class = NSClassFromString(className);
if (!class)
{
class = [self registeredActionHolderClassWithName: className];
[self addSelector:selector
fromClass:[object class]
toClass:class];
}
}
- (NSString *)actionHolderClassNameForObject:(id)object selector:(SEL)selector
{
NSString *objectClassName = NSStringFromClass([object class]);
NSString *methodName = NSStringFromSelector(selector);
return [NSString stringWithFormat:@"%@_%@_%@", kTMActionHolderClassName, objectClassName, methodName];
}
- (Class)actionHolderClassForObject:(id)object selector:(SEL)selector
{
NSString *actionHolderClassName = [self actionHolderClassNameForObject:object selector:selector];
return NSClassFromString(actionHolderClassName);
}
- (Class)registeredActionHolderClassWithName:(NSString *)className
{
const char *classNameAsCString = [className cStringUsingEncoding:NSASCIIStringEncoding];
Class class = objc_allocateClassPair([NSObject class], classNameAsCString, 0);
objc_registerClassPair(class);
return class;
}
- (void)addSelector:(SEL)selector fromClass:(Class)sourceClass toClass:(Class)destinationClass
{
Method instanceMethod = class_getInstanceMethod(sourceClass, selector);
const char *methodTypeEncoding = method_getTypeEncoding(instanceMethod);
IMP originalMethodImplementation = class_getMethodImplementation(sourceClass, selector);
class_addMethod(destinationClass, selector, originalMethodImplementation, methodTypeEncoding);
}
- (void)unregisterActionHolderClassForObject:(id)object selector:(SEL)selector
{
Class actionHolderClass = [self actionHolderClassForObject:object selector:selector];
objc_disposeClassPair(actionHolderClass);
}
#pragma mark - Action selector implementation
// This will replace the implementation of the method swizzled
static void *actionSelectorImplementation(id self, SEL _cmd, ...)
{
NSMethodSignature *methodSignature = [self methodSignatureForSelector:_cmd];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.selector = _cmd;
invocation.target = self;
va_list arguments;
va_start(arguments, _cmd);
char* args = (char*)arguments;
NSUInteger argumentCount = [methodSignature numberOfArguments];
for (NSUInteger index = kFirstParamIndex; index < argumentCount; index++)
{
const char *argumentType = [methodSignature getArgumentTypeAtIndex:index];
NSUInteger argumentSize, argumentAlignment;
NSGetSizeAndAlignment(argumentType, &argumentSize, &argumentAlignment);
NSUInteger mod = (NSUInteger)args % argumentAlignment;
if (mod != 0) args += (argumentAlignment - mod);
[invocation setArgument:args atIndex:index];
args += argumentSize;
}
va_end(arguments);
[sharedInstanceMethodSwizzler handleInvocation:invocation];
void *returnValue = nil;
if (methodSignature.methodReturnLength) [invocation getReturnValue:&returnValue];
return returnValue;
}
- (void)handleInvocation:(NSInvocation *)invocation
{
TMSwizzlingInformation *swizzlingInformation = [self swizzlingInformationForSelector:invocation.selector
object:invocation.target];
swizzlingInformation.currentlyRunning = (swizzlingInformation != nil);
switch (swizzlingInformation.originalCallingTime) {
case TMOriginalCallingNever:
swizzlingInformation.swizzlingBlock(invocation);
break;
case TMOriginalCallingBeforeBlock:
[self performActionHolderClassSelectorForInvocation:invocation
withSwizzlingInformation:swizzlingInformation];
swizzlingInformation.swizzlingBlock(invocation);
break;
case TMOriginalCallingAfterBlock:
swizzlingInformation.swizzlingBlock(invocation);
[self performActionHolderClassSelectorForInvocation:invocation
withSwizzlingInformation:swizzlingInformation];
break;
default:
// We will arrive here if swizzlingInformation is nil, which means this is an
// object whose method hasn't been swizzled, but belonging to a class which has.
// We use any of the swizzled objects to get the original implementation
[self performActionHolderClassSelectorForInvocation:invocation
withSwizzlingInformation:[self anyStoredSwizzlingMatchingInvocation:invocation]];
break;
}
swizzlingInformation.currentlyRunning = NO;
}
- (TMSwizzlingInformation *)swizzlingInformationForSelector:(SEL)selector object:(id)object
{
NSPredicate *filteringPredicate = [NSPredicate predicateWithBlock:^BOOL(TMSwizzlingInformation *swizzlingInformation, NSDictionary *bindings) {
return (swizzlingInformation.object == object && sel_isEqual(swizzlingInformation.selector, selector));
}];
NSArray *swizzlingInformations = [self.swizzlingInformationStorage filteredArrayUsingPredicate:filteringPredicate];
NSAssert([swizzlingInformations count] <= 1, @"We should just have a swizzling for the same selector in an object");
return [swizzlingInformations firstObject];
}
- (void)performActionHolderClassSelectorForInvocation:(NSInvocation *)invocation
withSwizzlingInformation:(TMSwizzlingInformation *)swizzlingInformation
{
/*
Here we undo the swizzling so we can call the original implementation. Then, we do the swizzling again.
There is an easier and better way to do the same, and allows us to drop the synchronized block, which is
selecting the implementation the NSInvocation will call:
Class actionHolderClass = [self actionHolderClassForObject:invocation.target selector:invocation.selector];
IMP originalMethodImplementation = class_getMethodImplementation(actionHolderClass, invocation.selector);
[invocation invokeUsingIMP:originalMethodImplementation];
Unfortunately, invokeUsingIMP is a private API of NSInvocation :-( we can still call it, but Apple will probably
kill us or something worse. For the adventurous, this is a way that will probably bypass Apple checkings:
#import <objc/message.h>
...
objc_msgSend(invocation, sel_registerName("invokeUsingIMP:"), originalMethodImplementation);
*/
@synchronized(self)
{
[self undoSwizzlingWithInformation:swizzlingInformation];
[invocation invoke];
[self performSwizzlingWithInformation:swizzlingInformation];
if (swizzlingInformation.shouldBeUnswizzledAfterCallingOriginal)
{
[self undoSwizzlingWithInformation:swizzlingInformation];
}
}
}
- (TMSwizzlingInformation *)anyStoredSwizzlingMatchingInvocation:(NSInvocation *)invocation
{
TMSwizzlingInformation *swizzlingInformation = [[TMSwizzlingInformation alloc] init];
swizzlingInformation.object = invocation.target;
swizzlingInformation.selector = invocation.selector;
return [[self storedSwizzlingsSimilarTo:swizzlingInformation] firstObject];
}
#pragma mark - Swizzling assignment generation and storage management
- (NSMutableArray *)swizzlingInformationStorage
{
if (!_swizzlingInformationStorage)
{
_swizzlingInformationStorage = [NSMutableArray array];
}
return _swizzlingInformationStorage;
}
- (TMSwizzlingInformation *)swizzlingInformationWithObject:(id)object
selector:(SEL)selector
swizzlingBlock:(TMSwizzlingBlock)swizzlingBlock
originalCallingTime:(TMOriginalCalling)originalCallingTime;
{
TMSwizzlingInformation *swizzlingInformation = [[TMSwizzlingInformation alloc] init];
swizzlingInformation.object = object;
swizzlingInformation.selector = selector;
swizzlingInformation.swizzlingBlock = swizzlingBlock;
swizzlingInformation.originalCallingTime = originalCallingTime;
return swizzlingInformation;
}
- (void)storeSwizzlingInformationReplacingExistingOne:(TMSwizzlingInformation *)swizzlingInformation
{
TMSwizzlingInformation *storedSwizzlingInformation = [self swizzlingInformationForSelector:swizzlingInformation.selector
object:swizzlingInformation.object];
if (storedSwizzlingInformation)
{
[self updateSwizzlingInformation:storedSwizzlingInformation withSwizzlingInformation:swizzlingInformation];
}
else
{
[self storeSwizzlingInformation:swizzlingInformation];
}
}
- (void)updateSwizzlingInformation:(TMSwizzlingInformation *)originalSwizzlingInformation
withSwizzlingInformation:(TMSwizzlingInformation *)replacingSwizzlingInformation
{
originalSwizzlingInformation.swizzlingBlock = replacingSwizzlingInformation.swizzlingBlock;
}
- (void)storeSwizzlingInformation:(TMSwizzlingInformation *)swizzlingInformation
{
[self.swizzlingInformationStorage addObject:swizzlingInformation];
}
#pragma mark - Selector swizzling
- (void)performSwizzlingWithInformation:(TMSwizzlingInformation *)swizzlingInformation
{
[self setUpImplementationForSelector:swizzlingInformation.selector
fromClass:[swizzlingInformation.object class]
withImplementation:(IMP)actionSelectorImplementation];
}
- (void)perfomUnswizzlingWithInformation:(TMSwizzlingInformation *)swizzlingInformation
{
if (![self.swizzlingInformationStorage containsObject:swizzlingInformation]) return;
if ([[self storedSwizzlingsSimilarTo:swizzlingInformation] count] == 0)
{
[self undoSwizzlingWithInformation:swizzlingInformation];
[self unregisterActionHolderClassForSwizzling:swizzlingInformation];
}
[self.swizzlingInformationStorage removeObject:swizzlingInformation];
}
- (NSArray *)storedSwizzlingsSimilarTo:(TMSwizzlingInformation *)swizzlingInformationToCompare
{
NSPredicate *filteringPredicate = [NSPredicate predicateWithBlock:^BOOL(TMSwizzlingInformation *swizzlingInformation, NSDictionary *bindings) {
return ([swizzlingInformation.object class] == [swizzlingInformationToCompare.object class]
&& sel_isEqual(swizzlingInformation.selector, swizzlingInformationToCompare.selector)
&& swizzlingInformation.object != swizzlingInformationToCompare.object);
}];
NSArray *swizzlingInformations = [self.swizzlingInformationStorage filteredArrayUsingPredicate:filteringPredicate];
return swizzlingInformations;
}
- (void)unregisterActionHolderClassForSwizzling:(TMSwizzlingInformation *)swizzlingInformation
{
[self unregisterActionHolderClassForObject:swizzlingInformation.object selector:swizzlingInformation.selector];
}
- (void)undoSwizzlingWithInformation:(TMSwizzlingInformation *)swizzlingInformation
{
Class actionHolderClass = [self actionHolderClassForObject:swizzlingInformation.object selector:swizzlingInformation.selector];
[self setUpImplementationForSelector:swizzlingInformation.selector
fromClass:[swizzlingInformation.object class]
withSelector:swizzlingInformation.selector
fromClass:actionHolderClass];
}
- (void)setUpImplementationForSelector:(SEL)originalSelector fromClass:(Class)originalClass withSelector:(SEL)replacingSelector fromClass:(Class)replacingClass
{
IMP replacingImplementation = class_getMethodImplementation(replacingClass, replacingSelector);
[self setUpImplementationForSelector:originalSelector
fromClass:originalClass
withImplementation:replacingImplementation];
}
- (void)setUpImplementationForSelector:(SEL)originalSelector fromClass:(Class)originalClass withImplementation:(IMP)replacingImplementation
{
Method method = class_getInstanceMethod(originalClass, originalSelector);
method_setImplementation(method, replacingImplementation);
}
@end