1414@implementation NSObject (UnrecognizedSelectorHook)
1515
1616+ (void )jj_swizzleUnrecognizedSelector {
17+
18+ // Class Method
19+ swizzleClassMethod ([self class ], @selector (methodSignatureForSelector: ), @selector (classMethodSignatureForSelectorSwizzled: ));
20+ swizzleClassMethod ([self class ], @selector (forwardInvocation: ), @selector (forwardClassInvocationSwizzled: ));
21+
22+ // Instance Method
1723 swizzleInstanceMethod ([self class ], @selector (methodSignatureForSelector: ), @selector (methodSignatureForSelectorSwizzled: ));
1824 swizzleInstanceMethod ([self class ], @selector (forwardInvocation: ), @selector (forwardInvocationSwizzled: ));
1925}
2026
27+ + (NSMethodSignature *)classMethodSignatureForSelectorSwizzled : (SEL )aSelector {
28+ NSMethodSignature * methodSignature = [self classMethodSignatureForSelectorSwizzled: aSelector];
29+ if (methodSignature) {
30+ return methodSignature;
31+ }
32+
33+ return [self .class checkObjectSignatureAndCurrentClass: self .class];
34+ }
35+
2136- (NSMethodSignature *)methodSignatureForSelectorSwizzled : (SEL )aSelector {
2237 NSMethodSignature * methodSignature = [self methodSignatureForSelectorSwizzled: aSelector];
2338 if (methodSignature) {
2439 return methodSignature;
2540 }
2641
42+ return [self .class checkObjectSignatureAndCurrentClass: self .class];
43+ }
44+
45+ /* *
46+ * Check the class method signature to the [NSObject class]
47+ * If not equals,return nil
48+ * If equals,return the v@:@ method
49+
50+ @param currentClass Class
51+ @return NSMethodSignature
52+ */
53+ + (NSMethodSignature *)checkObjectSignatureAndCurrentClass : (Class )currentClass {
2754 IMP originIMP = class_getMethodImplementation ([NSObject class ], @selector (methodSignatureForSelector: ));
28- IMP currentClassIMP = class_getMethodImplementation (self. class , @selector (methodSignatureForSelector: ));
55+ IMP currentClassIMP = class_getMethodImplementation (currentClass , @selector (methodSignatureForSelector: ));
2956
3057 // If current class override methodSignatureForSelector return nil
3158 if (originIMP != currentClassIMP){
@@ -37,8 +64,23 @@ - (NSMethodSignature*)methodSignatureForSelectorSwizzled:(SEL)aSelector {
3764 return [NSMethodSignature signatureWithObjCTypes: " v@:@" ];
3865}
3966
67+ /* *
68+ Forward instance object
69+
70+ @param invocation NSInvocation
71+ */
4072- (void )forwardInvocationSwizzled : (NSInvocation *)invocation {
41- NSString * message = [NSString stringWithFormat: @" Unrecognized selector class:%@ and selector:%@ " ,NSStringFromClass (self .class),NSStringFromSelector (invocation.selector)];
73+ NSString * message = [NSString stringWithFormat: @" Unrecognized instance class:%@ and selector:%@ " ,NSStringFromClass (self .class),NSStringFromSelector (invocation.selector)];
74+ handleCrashException (JJExceptionGuardUnrecognizedSelector,message);
75+ }
76+
77+ /* *
78+ Forward class object
79+
80+ @param invocation NSInvocation
81+ */
82+ + (void )forwardClassInvocationSwizzled : (NSInvocation *)invocation {
83+ NSString * message = [NSString stringWithFormat: @" Unrecognized static class:%@ and selector:%@ " ,NSStringFromClass (self .class),NSStringFromSelector (invocation.selector)];
4284 handleCrashException (JJExceptionGuardUnrecognizedSelector,message);
4385}
4486
0 commit comments