forked from bang590/JSPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJPCleaner.m
49 lines (40 loc) · 1.75 KB
/
JPCleaner.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
//
// JPReverter.m
// JSPatchDemo
//
// Created by bang on 2/4/16.
// Copyright © 2016 bang. All rights reserved.
//
#import "JPCleaner.h"
#import <objc/runtime.h>
@implementation JPCleaner
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
+ (void)cleanAll
{
[self cleanClass:nil];
}
+ (void)cleanClass:(NSString *)className
{
NSDictionary *methodsDict = [JPExtension overideMethods];
for (Class cls in methodsDict.allKeys) {
if (className && ![className isEqualToString:NSStringFromClass(cls)]) {
continue;
}
for (NSString *jpSelectorName in [methodsDict[cls] allKeys]) {
NSString *selectorName = [jpSelectorName substringFromIndex:3];
NSString *originalSelectorName = [NSString stringWithFormat:@"ORIG%@", selectorName];
SEL selector = NSSelectorFromString(selectorName);
SEL originalSelector = NSSelectorFromString(originalSelectorName);
IMP originalImp = class_respondsToSelector(cls, originalSelector) ? class_getMethodImplementation(cls, originalSelector) : NULL;
Method method = class_getInstanceMethod(cls, originalSelector);
char *typeDescription = (char *)method_getTypeEncoding(method);
class_replaceMethod(cls, selector, originalImp, typeDescription);
}
char *typeDescription = (char *)method_getTypeEncoding(class_getInstanceMethod(cls, @selector(forwardInvocation:)));
IMP forwardInvocationIMP = class_getMethodImplementation(cls, @selector(ORIGforwardInvocation:));
class_replaceMethod(cls, @selector(forwardInvocation:), forwardInvocationIMP, typeDescription);
}
}
#pragma clang diagnostic pop
@end