-
Notifications
You must be signed in to change notification settings - Fork 0
/
JGMethodSwizzler.m
executable file
·776 lines (544 loc) · 23.2 KB
/
JGMethodSwizzler.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//
// JGMethodSwizzler.m
// JGMethodSwizzler
//
// Created by Jonas Gessner 22.08.2013
// Copyright (c) 2013 Jonas Gessner. All rights reserved.
//
#import "JGMethodSwizzler.h"
#import <objc/runtime.h>
#import <libkern/OSAtomic.h>
#pragma mark Defines
#ifdef __clang__
#if __has_feature(objc_arc)
#define JG_ARC_ENABLED
#endif
#endif
#ifdef JG_ARC_ENABLED
#define JGBridgeCast(type, obj) ((__bridge type)obj)
#define releaseIfNecessary(object)
#else
#define JGBridgeCast(type, obj) ((type)obj)
#define releaseIfNecessary(object) [object release];
#endif
#define kClassKey @"k"
#define kCountKey @"c"
#define kIMPKey @"i"
// See http://clang.llvm.org/docs/Block-ABI-Apple.html#high-level
struct Block_literal_1 {
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
int flags;
int reserved;
void (*invoke)(void *, ...);
struct Block_descriptor_1 {
unsigned long int reserved; // NULL
unsigned long int size; // sizeof(struct Block_literal_1)
// optional helper functions
void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
void (*dispose_helper)(void *src); // IFF (1<<25)
// required ABI.2010.3.16
const char *signature; // IFF (1<<30)
} *descriptor;
// imported variables
};
enum {
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE
BLOCK_HAS_SIGNATURE = (1 << 30),
};
typedef int BlockFlags;
#pragma mark - Block Analysis
NS_INLINE const char *blockGetType(id block) {
struct Block_literal_1 *blockRef = JGBridgeCast(struct Block_literal_1 *, block);
BlockFlags flags = blockRef->flags;
if (flags & BLOCK_HAS_SIGNATURE) {
void *signatureLocation = blockRef->descriptor;
signatureLocation += sizeof(unsigned long int);
signatureLocation += sizeof(unsigned long int);
if (flags & BLOCK_HAS_COPY_DISPOSE) {
signatureLocation += sizeof(void(*)(void *dst, void *src));
signatureLocation += sizeof(void (*)(void *src));
}
const char *signature = (*(const char **)signatureLocation);
return signature;
}
return NULL;
}
NS_INLINE BOOL blockIsCompatibleWithMethodType(id block, __unsafe_unretained Class class, SEL selector, BOOL instanceMethod) {
/* const char *blockType = blockGetType(block);
NSMethodSignature *blockSignature = [NSMethodSignature signatureWithObjCTypes:blockType];
NSMethodSignature *methodSignature = (instanceMethod ? [class instanceMethodSignatureForSelector:selector] : [class methodSignatureForSelector:selector]);
//#ifdef DEBUG
NSLog(@"blockSignature:%@",[NSString stringWithUTF8String:blockType]);
NSLog(@"MethodSignature:%@",methodSignature);
//#endif
if (!blockSignature || !methodSignature) {
return NO;
}
if (blockSignature.numberOfArguments != methodSignature.numberOfArguments) {
return NO;
}
const char *blockReturnType = blockSignature.methodReturnType;
if (strncmp(blockReturnType, "@", 1) == 0) {
blockReturnType = "@";
}
if (strcmp(blockReturnType, methodSignature.methodReturnType) != 0) {
return NO;
}
for (unsigned int i = 0; i < methodSignature.numberOfArguments; i++) {
if (i == 0) {
// self in method, block in block
if (strcmp([methodSignature getArgumentTypeAtIndex:i], "@") != 0) {
return NO;
}
if (strcmp([blockSignature getArgumentTypeAtIndex:i], "@?") != 0) {
return NO;
}
}
else if(i == 1) {
// SEL in method, self in block
if (strcmp([methodSignature getArgumentTypeAtIndex:i], ":") != 0) {
return NO;
}
if (instanceMethod ? strncmp([blockSignature getArgumentTypeAtIndex:i], "@", 1) != 0 : (strncmp([blockSignature getArgumentTypeAtIndex:i], "@", 1) != 0 && strcmp([blockSignature getArgumentTypeAtIndex:i], "r^#") != 0)) {
return NO;
}
}
else {
const char *blockSignatureArg = [blockSignature getArgumentTypeAtIndex:i];
if (strncmp(blockSignatureArg, "@", 1) == 0) {
blockSignatureArg = "@";
}
if (strcmp(blockSignatureArg, [methodSignature getArgumentTypeAtIndex:i]) != 0) {
return NO;
}
}
}*/
return YES;
}
NS_INLINE BOOL blockIsValidReplacementProvider(id block) {
const char *blockType = blockGetType(block);
JGMethodReplacementProvider dummy = JGMethodReplacementProviderBlock {
return nil;
};
const char *expectedType = blockGetType(dummy);
return (strcmp(expectedType, blockType) == 0);
}
NS_INLINE void classSwizzleMethod(Class cls, Method method, IMP newImp) {
if (!class_addMethod(cls, method_getName(method), newImp, method_getTypeEncoding(method))) {
// class already has implementation, swizzle it instead
method_setImplementation(method, newImp);
}
}
#pragma mark - Original Implementations
static OSSpinLock lock = OS_SPINLOCK_INIT;
static NSMutableDictionary *originalClassMethods;
static NSMutableDictionary *originalInstanceMethods;
static NSMutableDictionary *originalInstanceInstanceMethods;
NS_INLINE JG_IMP originalClassMethodImplementation(__unsafe_unretained Class class, SEL selector, BOOL fetchOnly) {
NSCAssert(!OSSpinLockTry(&lock), @"Spin lock is not locked");
if (!originalClassMethods) {
originalClassMethods = [[NSMutableDictionary alloc] init];
}
NSString *classKey = NSStringFromClass(class);
NSString *selectorKey = NSStringFromSelector(selector);
NSMutableDictionary *classSwizzles = originalClassMethods[classKey];
NSValue *pointerValue = classSwizzles[selectorKey];
if (!classSwizzles) {
classSwizzles = [NSMutableDictionary dictionary];
originalClassMethods[classKey] = classSwizzles;
}
JG_IMP orig = NULL;
if (pointerValue) {
orig = [pointerValue pointerValue];
if (fetchOnly) {
if (classSwizzles.count == 1) {
[originalClassMethods removeObjectForKey:classKey];
}
else {
[classSwizzles removeObjectForKey:selectorKey];
}
}
}
else if (!fetchOnly) {
orig = (JG_IMP)[class methodForSelector:selector];
classSwizzles[selectorKey] = [NSValue valueWithPointer:orig];
}
if (classSwizzles.count == 0) {
[originalClassMethods removeObjectForKey:classKey];
}
if (originalClassMethods.count == 0) {
releaseIfNecessary(originalClassMethods);
originalClassMethods = nil;
}
return orig;
}
NS_INLINE JG_IMP originalInstanceMethodImplementation(__unsafe_unretained Class class, SEL selector, BOOL fetchOnly) {
NSCAssert(!OSSpinLockTry(&lock), @"Spin lock is not locked");
if (!originalInstanceMethods) {
originalInstanceMethods = [[NSMutableDictionary alloc] init];
}
NSString *classKey = NSStringFromClass(class);
NSString *selectorKey = NSStringFromSelector(selector);
NSMutableDictionary *classSwizzles = originalInstanceMethods[classKey];
NSValue *pointerValue = classSwizzles[selectorKey];
if (!classSwizzles) {
classSwizzles = [NSMutableDictionary dictionary];
originalInstanceMethods[classKey] = classSwizzles;
}
JG_IMP orig = NULL;
if (pointerValue) {
orig = [pointerValue pointerValue];
if (fetchOnly) {
[classSwizzles removeObjectForKey:selectorKey];
if (classSwizzles.count == 0) {
[originalInstanceMethods removeObjectForKey:classKey];
}
}
}
else if (!fetchOnly) {
orig = (JG_IMP)[class instanceMethodForSelector:selector];
classSwizzles[selectorKey] = [NSValue valueWithPointer:orig];
}
if (classSwizzles.count == 0) {
[originalInstanceMethods removeObjectForKey:classKey];
}
if (originalInstanceMethods.count == 0) {
releaseIfNecessary(originalInstanceMethods);
originalInstanceMethods = nil;
}
return orig;
}
NS_INLINE JG_IMP originalInstanceInstanceMethodImplementation(__unsafe_unretained Class class, SEL selector, BOOL fetchOnly) {
NSCAssert(!OSSpinLockTry(&lock), @"Spin lock is not locked");
if (!originalInstanceInstanceMethods) {
originalInstanceInstanceMethods = [[NSMutableDictionary alloc] init];
}
NSString *classKey = NSStringFromClass(class);
NSString *selectorKey = NSStringFromSelector(selector);
NSMutableDictionary *instanceSwizzles = originalInstanceInstanceMethods[classKey];
if (!instanceSwizzles) {
instanceSwizzles = [NSMutableDictionary dictionary];
originalInstanceInstanceMethods[classKey] = instanceSwizzles;
}
JG_IMP orig = NULL;
if (fetchOnly) {
NSMutableDictionary *dict = instanceSwizzles[selectorKey];
if (!dict) {
return NULL;
}
NSValue *pointerValue = dict[kIMPKey];
orig = [pointerValue pointerValue];
unsigned int count = [dict[kCountKey] unsignedIntValue];
if (count == 1) {
[instanceSwizzles removeObjectForKey:selectorKey];
if (instanceSwizzles.count == 0) {
[originalInstanceInstanceMethods removeObjectForKey:classKey];
}
}
else {
dict[kCountKey] = @(count-1);
}
}
else {
NSMutableDictionary *dict = instanceSwizzles[selectorKey];
if (!dict) {
dict = [NSMutableDictionary dictionaryWithCapacity:2];
dict[kCountKey] = @(1);
orig = (JG_IMP)[class instanceMethodForSelector:selector];
dict[kIMPKey] = [NSValue valueWithPointer:orig];
instanceSwizzles[selectorKey] = dict;
}
else {
orig = [dict[kIMPKey] pointerValue];
unsigned int count = [dict[kCountKey] unsignedIntValue];
dict[kCountKey] = @(count+1);
}
}
if (originalInstanceInstanceMethods.count == 0) {
releaseIfNecessary(originalInstanceInstanceMethods);
originalInstanceInstanceMethods = nil;
}
return orig;
}
#pragma mark - Deswizzling Global Swizzles
NS_INLINE BOOL deswizzleClassMethod(__unsafe_unretained Class class, SEL selector) {
OSSpinLockLock(&lock);
JG_IMP originalIMP = originalClassMethodImplementation(class, selector, YES);
if (originalIMP) {
method_setImplementation(class_getClassMethod(class, selector), (IMP)originalIMP);
OSSpinLockUnlock(&lock);
return YES;
}
else {
OSSpinLockUnlock(&lock);
return NO;
}
}
NS_INLINE BOOL deswizzleInstanceMethod(__unsafe_unretained Class class, SEL selector) {
OSSpinLockLock(&lock);
JG_IMP originalIMP = originalInstanceMethodImplementation(class, selector, YES);
if (originalIMP) {
method_setImplementation(class_getInstanceMethod(class, selector), (IMP)originalIMP);
OSSpinLockUnlock(&lock);
return YES;
}
else {
OSSpinLockUnlock(&lock);
return NO;
}
}
NS_INLINE BOOL deswizzleAllClassMethods(__unsafe_unretained Class class) {
OSSpinLockLock(&lock);
BOOL success = NO;
NSDictionary *d = [originalClassMethods[NSStringFromClass(class)] copy];
for (NSString *sel in d) {
OSSpinLockUnlock(&lock);
if (deswizzleClassMethod(class, NSSelectorFromString(sel))) {
success = YES;
}
OSSpinLockLock(&lock);
}
OSSpinLockUnlock(&lock);
releaseIfNecessary(d);
return success;
}
NS_INLINE BOOL deswizzleAllInstanceMethods(__unsafe_unretained Class class) {
OSSpinLockLock(&lock);
BOOL success = NO;
NSDictionary *d = [originalInstanceMethods[NSStringFromClass(class)] copy];
for (NSString *sel in d) {
OSSpinLockUnlock(&lock);
if (deswizzleInstanceMethod(class, NSSelectorFromString(sel))) {
success = YES;
}
OSSpinLockLock(&lock);
}
OSSpinLockUnlock(&lock);
releaseIfNecessary(d);
return success;
}
#pragma mark - Global Swizzling
NS_INLINE void swizzleClassMethod(__unsafe_unretained Class class, SEL selector, JGMethodReplacementProvider replacement) {
NSCAssert(blockIsValidReplacementProvider(replacement), @"Invalid method replacemt provider");
NSCAssert([class respondsToSelector:selector], @"Invalid method: +[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector));
OSSpinLockLock(&lock);
Method originalMethod = class_getClassMethod(class, selector);
JG_IMP orig = originalClassMethodImplementation(class, selector, NO);
id replaceBlock = replacement(orig, class, selector);
NSCAssert(blockIsCompatibleWithMethodType(replaceBlock, class, selector, NO), @"Invalid method replacement");
Class meta = object_getClass(class);
classSwizzleMethod(meta, originalMethod, imp_implementationWithBlock(replaceBlock));
OSSpinLockUnlock(&lock);
}
NS_INLINE void swizzleInstanceMethod(__unsafe_unretained Class class, SEL selector, JGMethodReplacementProvider replacement) {
NSCAssert(blockIsValidReplacementProvider(replacement), @"Invalid method replacemt provider");
NSCAssert([class instancesRespondToSelector:selector], @"Invalid method: -[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector));
NSCAssert(originalInstanceInstanceMethods[NSStringFromClass(class)][NSStringFromSelector(selector)] == nil, @"Swizzling an instance method that has already been swizzled on a specific instance is not supported");
OSSpinLockLock(&lock);
Method originalMethod = class_getInstanceMethod(class, selector);
JG_IMP orig = originalInstanceMethodImplementation(class, selector, NO);
id replaceBlock = replacement(orig, class, selector);
NSCAssert(blockIsCompatibleWithMethodType(replaceBlock, class, selector, YES), @"Invalid method replacement");
IMP replace = imp_implementationWithBlock(replaceBlock);
classSwizzleMethod(class, originalMethod, replace);
OSSpinLockUnlock(&lock);
}
#pragma mark - Instance Specific Swizzling & Deswizzling
static NSMutableDictionary *dynamicSubclassesByObject;
NS_INLINE unsigned int swizzleCount(__unsafe_unretained id object) {
NSValue *key = [NSValue valueWithPointer:JGBridgeCast(const void *, object)];
unsigned int count = [dynamicSubclassesByObject[key][kCountKey] unsignedIntValue];
return count;
}
NS_INLINE void decreaseSwizzleCount(__unsafe_unretained id object) {
NSValue *key = [NSValue valueWithPointer:JGBridgeCast(const void *, object)];
NSMutableDictionary *classDict = dynamicSubclassesByObject[key];
unsigned int count = [classDict[kCountKey] unsignedIntValue];
classDict[kCountKey] = @(count-1);
}
NS_INLINE BOOL deswizzleInstance(__unsafe_unretained id object) {
OSSpinLockLock(&lock);
BOOL success = NO;
if (swizzleCount(object) > 0) {
Class dynamicSubclass = object_getClass(object);
object_setClass(object, [object class]);
objc_disposeClassPair(dynamicSubclass);
[originalInstanceInstanceMethods removeObjectForKey:NSStringFromClass([object class])];
[dynamicSubclassesByObject removeObjectForKey:[NSValue valueWithPointer:JGBridgeCast(const void *, object)]];
if (!dynamicSubclassesByObject.count) {
releaseIfNecessary(dynamicSubclassesByObject);
dynamicSubclassesByObject = nil;
}
if (!originalInstanceInstanceMethods.count) {
releaseIfNecessary(originalInstanceInstanceMethods);
originalInstanceInstanceMethods = nil;
}
success = YES;
}
OSSpinLockUnlock(&lock);
return success;
}
NS_INLINE BOOL deswizzleMethod(__unsafe_unretained id object, SEL selector) {
OSSpinLockLock(&lock);
BOOL success = NO;
unsigned int count = swizzleCount(object);
if (count == 1) {
OSSpinLockUnlock(&lock);
return deswizzleInstance(object);
}
else if (count > 1) {
JG_IMP originalIMP = originalInstanceInstanceMethodImplementation([object class], selector, YES);
if (originalIMP) {
method_setImplementation(class_getInstanceMethod(object_getClass(object), selector), (IMP)originalIMP);
success = YES;
}
decreaseSwizzleCount(object);
}
OSSpinLockUnlock(&lock);
return success;
}
NS_INLINE void swizzleInstance(__unsafe_unretained id object, SEL selector, JGMethodReplacementProvider replacementProvider) {
Class class = [object class];
NSCAssert(blockIsValidReplacementProvider(replacementProvider), @"Invalid method replacemt provider");
NSCAssert([object respondsToSelector:selector], @"Invalid method: -[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector));
OSSpinLockLock(&lock);
if (!dynamicSubclassesByObject) {
dynamicSubclassesByObject = [[NSMutableDictionary alloc] init];
};
NSValue *key = [NSValue valueWithPointer:JGBridgeCast(const void *, object)];
NSMutableDictionary *classDict = dynamicSubclassesByObject[key];
Class newClass = [classDict[kClassKey] pointerValue];
if (!classDict || !newClass) {
NSString *dynamicSubclass = [NSStringFromClass(class) stringByAppendingFormat:@"_JGMS_%@", [[NSUUID UUID] UUIDString]];
const char *newClsName = [dynamicSubclass UTF8String];
NSCAssert(!objc_lookUpClass(newClsName), @"Class %@ already exists!\n", dynamicSubclass);
newClass = objc_allocateClassPair(class, newClsName, 0);
NSCAssert(newClass, @"Could not create class %@\n", dynamicSubclass);
objc_registerClassPair(newClass);
classDict = [NSMutableDictionary dictionary];
classDict[kClassKey] = [NSValue valueWithPointer:JGBridgeCast(const void *, newClass)];
classDict[kCountKey] = @(1);
dynamicSubclassesByObject[[NSValue valueWithPointer:JGBridgeCast(const void *, object)]] = classDict;
Method classMethod = class_getInstanceMethod(newClass, @selector(class));
id swizzledClass = ^Class (__unsafe_unretained id self) {
return class;
};
classSwizzleMethod(newClass, classMethod, imp_implementationWithBlock(swizzledClass));
SEL deallocSel = sel_getUid("dealloc");
Method dealloc = class_getInstanceMethod(newClass, deallocSel);
__block JG_IMP deallocImp = (JG_IMP)method_getImplementation(dealloc);
id deallocHandler = ^(__unsafe_unretained id self) {
NSCAssert(deswizzleInstance(self), @"Deswizzling of class %@ failed", NSStringFromClass([self class]));
if (deallocImp) {
deallocImp(self, deallocSel);
}
};
classSwizzleMethod(newClass, dealloc, imp_implementationWithBlock(deallocHandler));
}
else {
unsigned int count = [classDict[kCountKey] unsignedIntValue];
classDict[kCountKey] = @(count+1);
}
Method origMethod = class_getInstanceMethod(class, selector);
JG_IMP origIMP = originalInstanceInstanceMethodImplementation([object class], selector, NO);
id replaceBlock = replacementProvider(origIMP, class, selector);
NSCAssert(blockIsCompatibleWithMethodType(replaceBlock, class, selector, YES), @"Invalid method replacement");
classSwizzleMethod(newClass, origMethod, imp_implementationWithBlock(replaceBlock));
object_setClass(object, newClass);
OSSpinLockUnlock(&lock);
}
#pragma mark - Category Implementations
@implementation NSObject (JGMethodSwizzler)
+ (void)swizzleClassMethod:(SEL)selector withReplacement:(JGMethodReplacementProvider)replacementProvider {
swizzleClassMethod(self, selector, replacementProvider);
}
+ (void)swizzleInstanceMethod:(SEL)selector withReplacement:(JGMethodReplacementProvider)replacementProvider {
swizzleInstanceMethod(self, selector, replacementProvider);
}
@end
@implementation NSObject (JGMethodDeSwizzler)
+ (BOOL)deswizzleClassMethod:(SEL)selector {
return deswizzleClassMethod(self, selector);
}
+ (BOOL)deswizzleInstanceMethod:(SEL)selector {
return deswizzleInstanceMethod(self, selector);
}
+ (BOOL)deswizzleAllClassMethods {
return deswizzleAllClassMethods(self);
}
+ (BOOL)deswizzleAllInstanceMethods {
return deswizzleAllInstanceMethods(self);
}
+ (BOOL)deswizzleAllMethods {
BOOL c = [self deswizzleAllClassMethods];
BOOL i = [self deswizzleAllInstanceMethods];
return (c || i);
}
@end
@implementation NSObject (JGInstanceSwizzler)
- (void)swizzleMethod:(SEL)selector withReplacement:(JGMethodReplacementProvider)replacementProvider {
swizzleInstance(self, selector, replacementProvider);
}
- (BOOL)deswizzleMethod:(SEL)selector {
return deswizzleMethod(self, selector);
}
- (BOOL)deswizzle {
return deswizzleInstance(self);
}
@end
#pragma mark - Public functions
BOOL deswizzleGlobal(void) {
BOOL success = NO;
OSSpinLockLock(&lock);
NSDictionary *d = originalClassMethods.copy;
for (NSString *classKey in d) {
OSSpinLockUnlock(&lock);
BOOL ok = [NSClassFromString(classKey) deswizzleAllMethods];
OSSpinLockLock(&lock);
if (!success == ok) {
success = YES;
}
}
NSDictionary *d1 = originalInstanceMethods.copy;
for (NSString *classKey in d1) {
OSSpinLockUnlock(&lock);
BOOL ok = [NSClassFromString(classKey) deswizzleAllMethods];
OSSpinLockLock(&lock);
if (!success == ok) {
success = YES;
}
}
OSSpinLockUnlock(&lock);
releaseIfNecessary(d);
releaseIfNecessary(d1);
return success;
}
BOOL deswizzleInstances(void) {
OSSpinLockLock(&lock);
BOOL success = NO;
NSDictionary *d = dynamicSubclassesByObject.copy;
for (NSValue *pointer in d) {
id object = [pointer pointerValue];
OSSpinLockUnlock(&lock);
BOOL ok = [object deswizzle];
OSSpinLockLock(&lock);
if (!success && ok) {
success = YES;
}
}
OSSpinLockUnlock(&lock);
releaseIfNecessary(d);
return success;
}
BOOL deswizzleAll(void) {
BOOL a = deswizzleGlobal();
BOOL b = deswizzleInstances();
return (a || b);
}
//For debugging purposes:
//NSString *getStatus() {
// return [NSString stringWithFormat:@"Original Class:\n%@\n\n\nOriginal Instance:\n%@\n\n\nOriginal Instance Specific:\n%@\n\n\nDynamic Subclasses:\n%@\n\n\n", originalClassMethods, originalInstanceMethods, originalInstanceInstanceMethods, dynamicSubclassesByObject];
//}