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) ;  
353 - }  
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) ;  
358 - }  
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: 348 +
  349 + if (protectionType == YHCrashProtectionTypeNone) {
  350 + return;
  351 + }
  352 +
  353 + if (protectionType & YHCrashProtectionTypeUnrecognizedSelector) {
386 [self filterProtectionsOn:YHCrashProtectionTypeUnrecognizedSelector protectionName:@"UnrecognizedSelector" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod))]; 354 [self filterProtectionsOn:YHCrashProtectionTypeUnrecognizedSelector protectionName:@"UnrecognizedSelector" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingForwardingTargetForSelectorMethod))];
  355 + }
  356 +
  357 + if (protectionType & YHCrashProtectionTypeKVO) {
387 [self filterProtectionsOn:YHCrashProtectionTypeKVO protectionName:@"KVO" operation:openOperation imp:class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:))]; 358 [self filterProtectionsOn:YHCrashProtectionTypeKVO protectionName:@"KVO" operation:openOperation imp:class_getMethodImplementation([NSObject class], @selector(addObserver:forKeyPath:options:context:))];
  359 + }
  360 +
  361 + if (protectionType & YHCrashProtectionTypeNotification) {
388 [self filterProtectionsOn:YHCrashProtectionTypeNotification protectionName:@"Notification" operation:openOperation imp:class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:))]; 362 [self filterProtectionsOn:YHCrashProtectionTypeNotification protectionName:@"Notification" operation:openOperation imp:class_getMethodImplementation([NSNotificationCenter class], @selector(addObserver:selector:name:object:))];
  363 + }
  364 +
  365 + if (protectionType & YHCrashProtectionTypeTimer) {
389 [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod))]; 366 [self filterProtectionsOn:YHCrashProtectionTypeTimer protectionName:@"Timer" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingTimerMethod))];
  367 + }
  368 +
  369 + if (protectionType & YHCrashProtectionTypeContainers) {
390 [self filterProtectionsOn:YHCrashProtectionTypeContainers protectionName:@"Containers" operation:openOperation imp:class_getMethodImplementation([YH_CrashProtector class], @selector(yh_mappingContainersMethods))]; 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,99 +426,30 @@ static NSString *const NSNotificationProtectorValue = @"YHNotificationProtectorV @@ -437,99 +426,30 @@ 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);  
443 - }  
444 - if (protectionType > (1<<3) && protectionType != YHCrashProtectionTypeContainers) {  
445 - [self exchangeMethodWithType:YHCrashProtectionTypeTimer];  
446 - protectionType -= (1<<3) ;  
447 - }  
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 429
467 - case 5:  
468 - {  
469 - [self exchangeMethodWithType:YHCrashProtectionTypeUnrecognizedSelector];  
470 - [self exchangeMethodWithType:YHCrashProtectionTypeNotification]; 430 + if (protectionType == YHCrashProtectionTypeNone) {
  431 + return;
471 } 432 }
472 - break;  
473 433
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;  
488 - }  
489 -}  
490 -  
491 -+ (void)exchangeMethodWithType:(YHCrashProtectionType)protectionType {  
492 - switch (protectionType) {  
493 - case YHCrashProtectionTypeUnrecognizedSelector:  
494 - { 434 + if (protectionType & YHCrashProtectionTypeUnrecognizedSelector) {
495 [self swizzleUnrecoginzeTypeMethod]; 435 [self swizzleUnrecoginzeTypeMethod];
496 } 436 }
497 - break;  
498 437
499 - case YHCrashProtectionTypeKVO:  
500 - { 438 + if (protectionType & YHCrashProtectionTypeKVO) {
501 [self swizzleKVOTypeMethod]; 439 [self swizzleKVOTypeMethod];
502 } 440 }
503 - break;  
504 441
505 - case YHCrashProtectionTypeNotification:  
506 - { 442 + if (protectionType & YHCrashProtectionTypeNotification) {
507 [self swizzleNotificationTypeMethod]; 443 [self swizzleNotificationTypeMethod];
508 } 444 }
509 - break;  
510 445
511 - case YHCrashProtectionTypeTimer:  
512 - { 446 + if (protectionType & YHCrashProtectionTypeTimer) {
513 [self swizzleTimerTypeMethod]; 447 [self swizzleTimerTypeMethod];
514 } 448 }
515 - break;  
516 449
517 - case YHCrashProtectionTypeContainers:  
518 - { 450 + if (protectionType & YHCrashProtectionTypeContainers) {
519 [self swizzleContainersTypeMethod]; 451 [self swizzleContainersTypeMethod];
520 } 452 }
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;  
532 - }  
533 } 453 }
534 454
535 + (void)swizzleUnrecoginzeTypeMethod { 455 + (void)swizzleUnrecoginzeTypeMethod {