From 51b8c22110f22f131ea5126c4bb2a28a19477e86 Mon Sep 17 00:00:00 2001 From: "sergey.gavrilko" Date: Mon, 18 Mar 2024 16:51:46 +0300 Subject: [PATCH] Add condition to Selector call --- .../PonyDebugger/FLEXNetworkObserver.m | 197 +++++++++++------- 1 file changed, 123 insertions(+), 74 deletions(-) diff --git a/Classes/Network/PonyDebugger/FLEXNetworkObserver.m b/Classes/Network/PonyDebugger/FLEXNetworkObserver.m index 4adfca72a..b6b478c57 100644 --- a/Classes/Network/PonyDebugger/FLEXNetworkObserver.m +++ b/Classes/Network/PonyDebugger/FLEXNetworkObserver.m @@ -589,9 +589,11 @@ + (void)injectIntoNSURLConnectionCancel { void (^swizzleBlock)(NSURLConnection *) = ^(NSURLConnection *slf) { [FLEXNetworkObserver.sharedObserver connectionWillCancel:slf]; - ((void(*)(id, SEL))objc_msgSend)( - slf, swizzledSelector - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL))objc_msgSend)( + slf, swizzledSelector + ); + } }; IMP implementation = imp_implementationWithBlock(swizzleBlock); @@ -680,10 +682,11 @@ + (void)swizzleResumeSelector:(SEL)selector forClass:(Class)class { [FLEXNetworkObserver.sharedObserver URLSessionTaskWillResume:slf]; } - - ((void(*)(id, SEL))objc_msgSend)( - slf, swizzledSelector - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL))objc_msgSend)( + slf, swizzledSelector + ); + } }); class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume)); @@ -745,13 +748,17 @@ typedef void (^SendAsyncRequestBlock)( completion(response, data, error); } }; - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, request, queue, wrapper - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, request, queue, wrapper + ); + } } else { - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, request, queue, completion - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, request, queue, completion + ); + } } }; @@ -786,9 +793,12 @@ + (void)injectIntoNSURLConnectionSynchronousClassMethod { [FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID]; NSError *temporaryError = nil; NSURLResponse *temporaryResponse = nil; - data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( - slf, swizzledSelector, request, &temporaryResponse, &temporaryError - ); + + if ([slf respondsToSelector:swizzledSelector]) { + data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( + slf, swizzledSelector, request, &temporaryResponse, &temporaryError + ); + } [FLEXNetworkRecorder.defaultRecorder recordResponseReceivedWithRequestID:requestID @@ -818,9 +828,11 @@ + (void)injectIntoNSURLConnectionSynchronousClassMethod { *response = temporaryResponse; } } else { - data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( - slf, swizzledSelector, request, response, error - ); + if ([slf respondsToSelector:swizzledSelector]) { + data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)( + slf, swizzledSelector, request, response, error + ); + } } return data; @@ -878,16 +890,21 @@ + (void)injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods:(Class)sessionClas ]; // Call the original method - task = ((id(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, argument, completionWrapper - ); + if ([slf respondsToSelector:swizzledSelector]) { + task = ((id(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, argument, completionWrapper + ); + } [self setRequestID:requestID forConnectionOrTask:task]; } else { // Network observer disabled or no callback provided, // just pass through to the original method - task = ((id(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, argument, completion - ); + if ([slf respondsToSelector:swizzledSelector]) { + task = ((id(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, argument, completion + ); + } + } return task; }; @@ -942,14 +959,18 @@ + (void)injectIntoNSURLSessionAsyncUploadTaskMethods:(Class)sessionClass { completion:completion ]; - task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, request, argument, completionWrapper - ); + if ([slf respondsToSelector:swizzledSelector]) { + task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, request, argument, completionWrapper + ); + } [self setRequestID:requestID forConnectionOrTask:task]; } else { - task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, request, argument, completion - ); + if ([slf respondsToSelector:swizzledSelector]) { + task = ((id(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, request, argument, completion + ); + } } return task; }; @@ -1043,9 +1064,11 @@ + (void)injectWillSendRequestIntoDelegateClass:(Class)cls { [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ undefinedBlock(slf, connection, request, response); } originalImplementationBlock:^{ - returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, connection, request, response - ); + if ([slf respondsToSelector:swizzledSelector]) { + returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, connection, request, response + ); + } }]; return returnValue; }; @@ -1087,9 +1110,11 @@ typedef void (^DidReceiveResponseBlock)( [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ undefinedBlock(slf, connection, response); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, connection, response - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, connection, response + ); + } }]; }; @@ -1130,9 +1155,11 @@ typedef void (^DidReceiveDataBlock)( [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ undefinedBlock(slf, connection, data); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, connection, data - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, connection, data + ); + } }]; }; @@ -1165,9 +1192,11 @@ + (void)injectDidFinishLoadingIntoDelegateClass:(Class)cls { [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ undefinedBlock(slf, connection); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id))objc_msgSend)( - slf, swizzledSelector, connection - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id))objc_msgSend)( + slf, swizzledSelector, connection + ); + } }]; }; @@ -1205,9 +1234,11 @@ typedef void (^DidFailWithErrorBlock)( [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{ undefinedBlock(slf, connection, error); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, connection, error - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, connection, error + ); + } }]; }; @@ -1265,9 +1296,11 @@ typedef void (^HTTPRedirectionBlock)(id slf, delegate:slf ]; } originalImplementationBlock:^{ - ((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)( - slf, swizzledSelector, session, task, response, newRequest, completionHandler - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)( + slf, swizzledSelector, session, task, response, newRequest, completionHandler + ); + } }]; }; @@ -1308,9 +1341,11 @@ typedef void (^DidReceiveDataBlock)(id slf, [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ undefinedBlock(slf, session, dataTask, data); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, session, dataTask, data - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, session, dataTask, data + ); + } }]; }; @@ -1352,9 +1387,11 @@ typedef void (^DidBecomeDownloadTaskBlock)(id slf, [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ undefinedBlock(slf, session, dataTask, downloadTask); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, session, dataTask, downloadTask - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, session, dataTask, downloadTask + ); + } }]; }; @@ -1410,9 +1447,11 @@ typedef void (^DidReceiveResponseBlock)(id slf, delegate:slf ]; } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)( - slf, swizzledSelector, session, dataTask, response, completion - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)( + slf, swizzledSelector, session, dataTask, response, completion + ); + } }]; }; @@ -1455,9 +1494,11 @@ typedef void (^DidCompleteWithErrorBlock)(id slf, [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ undefinedBlock(slf, session, task, error); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, session, task, error - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, session, task, error + ); + } }]; }; @@ -1530,9 +1571,11 @@ typedef void (^DidFinishDownloadingBlock)(id slf, [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{ undefinedBlock(slf, session, task, location); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, id))objc_msgSend)( - slf, swizzledSelector, session, task, location - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, id))objc_msgSend)( + slf, swizzledSelector, session, task, location + ); + } }]; }; @@ -1586,10 +1629,12 @@ typedef void (^DidWriteDataBlock)(id slf, totalBytesWritten, totalBytesExpectedToWrite ); } originalImplementationBlock:^{ - ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)( - slf, swizzledSelector, session, task, bytesWritten, - totalBytesWritten, totalBytesExpectedToWrite - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)( + slf, swizzledSelector, session, task, bytesWritten, + totalBytesWritten, totalBytesExpectedToWrite + ); + } }]; }; @@ -1631,9 +1676,11 @@ typedef void (^SendMessageBlock)( } }; - ((void(*)(id, SEL, id, id))objc_msgSend)( - slf, swizzledSelector, message, completionHook - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id, id))objc_msgSend)( + slf, swizzledSelector, message, completionHook + ); + } }; [FLEXUtility replaceImplementationOfKnownSelector:selector @@ -1663,9 +1710,11 @@ typedef void (^SendMessageBlock)( completion(message, error); }; - ((void(*)(id, SEL, id))objc_msgSend)( - slf, swizzledSelector, completionHook - ); + if ([slf respondsToSelector:swizzledSelector]) { + ((void(*)(id, SEL, id))objc_msgSend)( + slf, swizzledSelector, completionHook + ); + } };