@@ -388,12 +388,18 @@ static Class aspect_hookClass(NSObject *self, NSError **error) {
388
388
}
389
389
390
390
static NSString *const AspectsForwardInvocationSelectorName = @" __aspects_forwardInvocation:" ;
391
+ static NSString *const AspectsForwardInvocationOrignSelectorName = @" __aspects_orignForwardInvocation:" ;
391
392
static void aspect_swizzleForwardInvocation (Class klass) {
392
393
NSCParameterAssert (klass);
393
394
// If there is no method, replace will act like class_addMethod.
394
- IMP originalImplementation = class_replaceMethod (klass, @selector (forwardInvocation: ), (IMP )__ASPECTS_ARE_BEING_CALLED__, " v@:@" );
395
+ Method forwardMethod = class_getInstanceMethod (klass, @selector (forwardInvocation: ));
396
+ IMP originalImplementation = method_getImplementation (forwardMethod);
395
397
if (originalImplementation) {
396
- class_addMethod (klass, NSSelectorFromString (AspectsForwardInvocationSelectorName), originalImplementation, " v@:@" );
398
+ class_addMethod (klass, NSSelectorFromString (AspectsForwardInvocationOrignSelectorName), originalImplementation, " v@:@" );
399
+ }
400
+ IMP replaceImplementation = class_replaceMethod (klass, @selector (forwardInvocation: ), (IMP )__ASPECTS_ARE_BEING_CALLED__, " v@:@" );
401
+ if (replaceImplementation) {
402
+ class_addMethod (klass, NSSelectorFromString (AspectsForwardInvocationSelectorName), replaceImplementation, " v@:@" );
397
403
}
398
404
AspectLog (@" Aspects: %@ is now aspect aware." , NSStringFromClass (klass));
399
405
}
@@ -512,7 +518,16 @@ static void __ASPECTS_ARE_BEING_CALLED__(__unsafe_unretained NSObject *self, SEL
512
518
if ([self respondsToSelector: originalForwardInvocationSEL]) {
513
519
((void ( *)(id , SEL , NSInvocation *))objc_msgSend)(self, originalForwardInvocationSEL, invocation);
514
520
}else {
515
- [self doesNotRecognizeSelector: invocation.selector];
521
+ SEL originalForwardInvocationSEL = NSSelectorFromString (AspectsForwardInvocationOrignSelectorName);
522
+ Class klass = object_getClass (invocation.target );
523
+ Method originalForwardMethod = class_getInstanceMethod (klass, originalForwardInvocationSEL);
524
+ // There is no class_removeMethod, so the best we can do is to retore the original implementation, or use a dummy.
525
+ IMP originalImplementation = method_getImplementation (originalForwardMethod);
526
+ if (originalImplementation) {
527
+ ((void ( *)(id , SEL , NSInvocation *))originalImplementation)(self, selector, invocation);
528
+ }else {
529
+ [self doesNotRecognizeSelector: invocation.selector];
530
+ }
516
531
}
517
532
}
518
533
0 commit comments