-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANSSwizzler.m
124 lines (108 loc) · 4.48 KB
/
ANSSwizzler.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
//
// ANSSwizzler.m
// AnalysysAgent
//
// Created by analysys on 2018/2/6.
// Copyright © 2018年 analysys. All rights reserved.
//
#import <objc/runtime.h>
#import "ANSSwizzler.h"
#import "ANSConsoleLog.h"
#import <UIKit/UIKit.h>
@implementation ANSSwizzler
+ (BOOL)isLocallyDefinedMethod:(Method)aMethod onClass:(Class)aClass {
uint count;
BOOL isLocal = NO;
Method *methods = class_copyMethodList(aClass, &count);
for (NSUInteger i = 0; i < count; i++) {
if (aMethod == methods[i]) {
isLocal = YES;
break;
}
}
free(methods);
return isLocal;
}
extern IMP getSwizzleIMPBySELName (NSString * name);
extern ANSSwizzle * __strong *getSwizzleByName (NSString * name);
+ (void)swizzleSelector:(SEL)aSelector
onClass:(Class)aClass
withBlock:(swizzleBlock)aBlock
named:(NSString *)aName;{
if (!aSelector || !aClass || ! aBlock || !aName) {
return;
}
Method aMethod = class_getInstanceMethod(aClass,aSelector);
if (!aMethod) {
[NSException raise:@"SwizzleBoolException" format:@"Cannot find method for %@ on %@", NSStringFromSelector(aSelector), NSStringFromClass(aClass)];
}
IMP aSwizzleMethod = getSwizzleIMPBySELName(NSStringFromSelector(aSelector));
ANSSwizzle * __strong * swizzle = getSwizzleByName(NSStringFromSelector(aSelector));
if (!aSwizzleMethod || !swizzle) {
return;
}
BOOL isLocal = [self isLocallyDefinedMethod:aMethod onClass:aClass];
if (isLocal) {
if (!*swizzle) {
IMP originalMethod = method_getImplementation(aMethod);
method_setImplementation(aMethod, aSwizzleMethod);
*swizzle = [[ANSSwizzle alloc] initWithBlock:aBlock named:aName forClass:aClass selector:aSelector originalMethod:originalMethod];
} else {
[(*swizzle).blocks setObject:aBlock forKey:aName];
}
} else {
IMP originalMethod = *swizzle ? (*swizzle).originalMethod : method_getImplementation(aMethod);
// Add the swizzle as a new local method on the class.
if (!class_addMethod(aClass, aSelector, aSwizzleMethod, method_getTypeEncoding(aMethod))) {
AnsDebug(@"SwizzleException:Could not add swizzled for %@::%@, even though it didn't already exist locally", NSStringFromClass(aClass), NSStringFromSelector(aSelector));
return;
}
// Now re-get the Method, it should be the one we just added.
Method newMethod = class_getInstanceMethod(aClass, aSelector);
if (aMethod == newMethod) {
AnsDebug(@"SwizzleException:Newly added method for %@::%@ was the same as the old method", NSStringFromClass(aClass), NSStringFromSelector(aSelector));
return;
}
*swizzle = [[ANSSwizzle alloc] initWithBlock:aBlock named:aName forClass:aClass selector:aSelector originalMethod:originalMethod];
}
}
+ (void)unswizzleSelector:(SEL)aSelector onClass:(Class)aClass named:(NSString *)aName{
ANSSwizzle * __strong *swizzle = getSwizzleByName(NSStringFromSelector(aSelector));
if (swizzle && *swizzle) {
if (aName && [(*swizzle).blocks objectForKey:aName]) {
[(*swizzle).blocks removeObjectForKey:aName];
}
}
}
@end
@implementation ANSSwizzle
- (instancetype)init {
if ((self = [super init])) {
self.blocks = [NSMapTable mapTableWithKeyOptions:(NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality)
valueOptions:(NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality)];
}
return self;
}
- (instancetype)initWithBlock:(swizzleBlock)aBlock
named:(NSString *)aName
forClass:(Class)aClass
selector:(SEL)aSelector
originalMethod:(IMP)aMethod {
if ((self = [self init])) {
self.class = aClass;
self.selector = aSelector;
self.originalMethod = aMethod;
[self.blocks setObject:aBlock forKey:aName];
}
return self;
}
- (NSString *)description {
NSString *descriptors = @"";
NSString *key;
NSEnumerator *keys = [self.blocks keyEnumerator];
while ((key = [keys nextObject])) {
descriptors = [descriptors stringByAppendingFormat:@"\t%@ : %@\n", key, [self.blocks objectForKey:key]];
}
return [NSString stringWithFormat:@"Swizzle on %@::%@ [\n%@]", NSStringFromClass(self.class), NSStringFromSelector(self.selector), descriptors];
}
@end