Authored by 张文文

枚举位运算逻辑修改

@@ -10,18 +10,13 @@ @@ -10,18 +10,13 @@
10 #import "YH_CrashCatchError.h" 10 #import "YH_CrashCatchError.h"
11 11
12 typedef NS_ENUM(NSInteger, YHCrashProtectionType) { 12 typedef NS_ENUM(NSInteger, YHCrashProtectionType) {
13 - /*开启全部保护*/  
14 - YHCrashProtectionTypeAll = 0,  
15 - /*UnrecognizedSelector保护*/  
16 - YHCrashProtectionTypeUnrecognizedSelector = 1<<0,  
17 - /*KVO保护*/  
18 - YHCrashProtectionTypeKVO = 1<<1,  
19 - /*Notification*/  
20 - YHCrashProtectionTypeNotification = 1<<2,  
21 - /*Timer保护*/  
22 - YHCrashProtectionTypeTimer = 1<<3,  
23 - /*Containers保护:包括NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSString、NSMutableString*/  
24 - YHCrashProtectionTypeContainers = 1<<4 13 + YHCrashProtectionTypeNone = 0,
  14 + YHCrashProtectionTypeUnrecognizedSelector = 1<<0, //UnrecognizedSelector保护
  15 + YHCrashProtectionTypeKVO = 1<<1, //KVO保护
  16 + YHCrashProtectionTypeNotification = 1<<2, //Notification保护
  17 + YHCrashProtectionTypeTimer = 1<<3, //Timer保护
  18 + YHCrashProtectionTypeContainers = 1<<4, //Containers保护
  19 + YHCrashProtectionTypeAll = 1<<5, //全部保护
25 }; 20 };
26 21
27 @interface YH_CrashProtector : NSObject 22 @interface YH_CrashProtector : NSObject
@@ -345,50 +345,39 @@ static NSString *const NSNotificationProtectorValue = @"YHNotificationProtectorV @@ -345,50 +345,39 @@ static NSString *const NSNotificationProtectorValue = @"YHNotificationProtectorV
345 } 345 }
346 346
347 + (void)filterProtectionsOn:(YHCrashProtectionType)protectionType operation:(BOOL)openOperation { 347 + (void)filterProtectionsOn:(YHCrashProtectionType)protectionType operation:(BOOL)openOperation {
348 - IMP imp;  
349 - if (protectionType > (1<<4)) {  
350 - imp = class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods));  
351 - [self filterProtectionsOn:YHCrashProtectionTypeContainers protectionName:@"Containers" operation:openOperation imp:imp];  
352 - protectionType -= (1<<4) ; 348 +
  349 + if (protectionType == YHCrashProtectionTypeNone) {
  350 + return;
353 } 351 }
354 - if (protectionType > (1<<3) && protectionType != YHCrashErrorTypeContainers) {  
355 - imp = class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod));  
356 - [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:imp];  
357 - protectionType -= (1<<3) ; 352 +
  353 + if (protectionType & YHCrashProtectionTypeUnrecognizedSelector) {
  354 + [self filterProtectionsOn:YHCrashProtectionTypeUnrecognizedSelector protectionName:@"UnrecognizedSelector" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod))];
358 } 355 }
359 - switch (protectionType) {  
360 - case YHCrashProtectionTypeUnrecognizedSelector:  
361 - imp = class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod));  
362 - [self filterProtectionsOn:protectionType protectionName:@"UnrecognizedSelector" operation:openOperation imp:imp];  
363 - break;  
364 -  
365 - case YHCrashProtectionTypeKVO:  
366 - imp = class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:));  
367 - [self filterProtectionsOn:protectionType protectionName:@"KVO" operation:openOperation imp:imp];  
368 - break;  
369 -  
370 - case YHCrashProtectionTypeNotification:  
371 - imp = class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:));  
372 - [self filterProtectionsOn:protectionType protectionName:@"Notification" operation:openOperation imp:imp];  
373 - break;  
374 -  
375 - case YHCrashProtectionTypeTimer:  
376 - imp = class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod));  
377 - [self filterProtectionsOn:protectionType protectionName:@"Timer" operation:openOperation imp:imp];  
378 - break;  
379 -  
380 - case YHCrashProtectionTypeContainers:  
381 - imp = class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods));  
382 - [self filterProtectionsOn:protectionType protectionName:@"Containers" operation:openOperation imp:imp];  
383 - break;  
384 -  
385 - case YHCrashProtectionTypeAll:  
386 - [self filterProtectionsOn:YHCrashProtectionTypeUnrecognizedSelector protectionName:@"UnrecognizedSelector" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod))];  
387 - [self filterProtectionsOn:YHCrashProtectionTypeKVO protectionName:@"KVO" operation:openOperation imp:class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:))];  
388 - [self filterProtectionsOn:YHCrashProtectionTypeNotification protectionName:@"Notification" operation:openOperation imp:class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:))];  
389 - [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod))];  
390 - [self filterProtectionsOn:YHCrashProtectionTypeContainers protectionName:@"Containers" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods))]; 356 +
  357 + if (protectionType & YHCrashProtectionTypeKVO) {
  358 + [self filterProtectionsOn:YHCrashProtectionTypeKVO protectionName:@"KVO" operation:openOperation imp:class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:))];
  359 + }
  360 +
  361 + if (protectionType & YHCrashProtectionTypeNotification) {
  362 + [self filterProtectionsOn:YHCrashProtectionTypeNotification protectionName:@"Notification" operation:openOperation imp:class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:))];
  363 + }
  364 +
  365 + if (protectionType & YHCrashProtectionTypeTimer) {
  366 + [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod))];
  367 + }
  368 +
  369 + if (protectionType & YHCrashProtectionTypeContainers) {
  370 + [self filterProtectionsOn:YHCrashProtectionTypeContainers protectionName:@"Containers" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods))];
391 } 371 }
  372 +
  373 + if (protectionType & YHCrashProtectionTypeAll) {
  374 + [self filterProtectionsOn:YHCrashProtectionTypeUnrecognizedSelector protectionName:@"UnrecognizedSelector" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod))];
  375 + [self filterProtectionsOn:YHCrashProtectionTypeKVO protectionName:@"KVO" operation:openOperation imp:class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:))];
  376 + [self filterProtectionsOn:YHCrashProtectionTypeNotification protectionName:@"Notification" operation:openOperation imp:class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:))];
  377 + [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod))];
  378 + [self filterProtectionsOn:YHCrashProtectionTypeContainers protectionName:@"Containers" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods))];
  379 + }
  380 +
392 } 381 }
393 382
394 + (void)filterProtectionsOn:(YHCrashProtectionType)protectionType protectionName:(NSString *)protectionName operation:(BOOL)openOperation imp:(IMP)imp { 383 + (void)filterProtectionsOn:(YHCrashProtectionType)protectionType protectionName:(NSString *)protectionName operation:(BOOL)openOperation imp:(IMP)imp {
@@ -437,98 +426,29 @@ static NSString *const NSNotificationProtectorValue = @"YHNotificationProtectorV @@ -437,98 +426,29 @@ static NSString *const NSNotificationProtectorValue = @"YHNotificationProtectorV
437 426
438 + (void)openProtectionsOn:(YHCrashProtectionType)protectionType catchErrorHandler:(void(^_Nullable)(YH_CrashCatchError * _Nullable error))errorHandler { 427 + (void)openProtectionsOn:(YHCrashProtectionType)protectionType catchErrorHandler:(void(^_Nullable)(YH_CrashCatchError * _Nullable error))errorHandler {
439 _errorHandler = errorHandler; 428 _errorHandler = errorHandler;
440 - if (protectionType > (1<<4)) {  
441 - [self exchangeMethodWithType:YHCrashProtectionTypeContainers];  
442 - protectionType -= (1<<4); 429 +
  430 + if (protectionType == YHCrashProtectionTypeNone) {
  431 + return;
443 } 432 }
444 - if (protectionType > (1<<3) && protectionType != YHCrashProtectionTypeContainers) {  
445 - [self exchangeMethodWithType:YHCrashProtectionTypeTimer];  
446 - protectionType -= (1<<3) ; 433 +
  434 + if (protectionType & YHCrashProtectionTypeUnrecognizedSelector) {
  435 + [self swizzleUnrecoginzeTypeMethod];
447 } 436 }
448 - switch ((long)protectionType) {  
449 - case YHCrashProtectionTypeUnrecognizedSelector:  
450 - case YHCrashProtectionTypeKVO:  
451 - case YHCrashProtectionTypeNotification:  
452 - case YHCrashProtectionTypeTimer:  
453 - case YHCrashProtectionTypeContainers:  
454 - case YHCrashProtectionTypeAll:  
455 - {  
456 - [self exchangeMethodWithType:protectionType];  
457 - }  
458 - break;  
459 -  
460 - case 3:  
461 - {  
462 - [self exchangeMethodWithType:YHCrashProtectionTypeUnrecognizedSelector];  
463 - [self exchangeMethodWithType:YHCrashProtectionTypeKVO];  
464 - }  
465 - break;  
466 -  
467 - case 5:  
468 - {  
469 - [self exchangeMethodWithType:YHCrashProtectionTypeUnrecognizedSelector];  
470 - [self exchangeMethodWithType:YHCrashProtectionTypeNotification];  
471 - }  
472 - break;  
473 -  
474 - case 6:  
475 - {  
476 - [self exchangeMethodWithType:YHCrashProtectionTypeKVO];  
477 - [self exchangeMethodWithType:YHCrashProtectionTypeNotification];  
478 - }  
479 - break;  
480 -  
481 - case 7:  
482 - {  
483 - [self exchangeMethodWithType:YHCrashProtectionTypeUnrecognizedSelector];  
484 - [self exchangeMethodWithType:YHCrashProtectionTypeKVO];  
485 - [self exchangeMethodWithType:YHCrashProtectionTypeNotification];  
486 - }  
487 - break; 437 +
  438 + if (protectionType & YHCrashProtectionTypeKVO) {
  439 + [self swizzleKVOTypeMethod];
488 } 440 }
489 -}  
490 -  
491 -+ (void)exchangeMethodWithType:(YHCrashProtectionType)protectionType {  
492 - switch (protectionType) {  
493 - case YHCrashProtectionTypeUnrecognizedSelector:  
494 - {  
495 - [self swizzleUnrecoginzeTypeMethod];  
496 - }  
497 - break;  
498 -  
499 - case YHCrashProtectionTypeKVO:  
500 - {  
501 - [self swizzleKVOTypeMethod];  
502 - }  
503 - break;  
504 -  
505 - case YHCrashProtectionTypeNotification:  
506 - {  
507 - [self swizzleNotificationTypeMethod];  
508 - }  
509 - break;  
510 -  
511 - case YHCrashProtectionTypeTimer:  
512 - {  
513 - [self swizzleTimerTypeMethod];  
514 - }  
515 - break;  
516 -  
517 - case YHCrashProtectionTypeContainers:  
518 - {  
519 - [self swizzleContainersTypeMethod];  
520 - }  
521 - break;  
522 -  
523 - case YHCrashProtectionTypeAll:  
524 - {  
525 - [self swizzleUnrecoginzeTypeMethod];  
526 - [self swizzleKVOTypeMethod];  
527 - [self swizzleNotificationTypeMethod];  
528 - [self swizzleTimerTypeMethod];  
529 - [self swizzleContainersTypeMethod];  
530 - }  
531 - break; 441 +
  442 + if (protectionType & YHCrashProtectionTypeNotification) {
  443 + [self swizzleNotificationTypeMethod];
  444 + }
  445 +
  446 + if (protectionType & YHCrashProtectionTypeTimer) {
  447 + [self swizzleTimerTypeMethod];
  448 + }
  449 +
  450 + if (protectionType & YHCrashProtectionTypeContainers) {
  451 + [self swizzleContainersTypeMethod];
532 } 452 }
533 } 453 }
534 454