Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 24a83a7

Browse files
author
springwinding
committedJan 13, 2016
Merge 6c9169f into ab3a811
2 parents ab3a811 + 6c9169f commit 24a83a7

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed
 

‎Aspects.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,18 @@ static Class aspect_hookClass(NSObject *self, NSError **error) {
388388
}
389389

390390
static NSString *const AspectsForwardInvocationSelectorName = @"__aspects_forwardInvocation:";
391+
static NSString *const AspectsForwardInvocationOrignSelectorName = @"__aspects_orignForwardInvocation:";
391392
static void aspect_swizzleForwardInvocation(Class klass) {
392393
NSCParameterAssert(klass);
393394
// 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);
395397
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@:@");
397403
}
398404
AspectLog(@"Aspects: %@ is now aspect aware.", NSStringFromClass(klass));
399405
}
@@ -512,7 +518,16 @@ static void __ASPECTS_ARE_BEING_CALLED__(__unsafe_unretained NSObject *self, SEL
512518
if ([self respondsToSelector:originalForwardInvocationSEL]) {
513519
((void( *)(id, SEL, NSInvocation *))objc_msgSend)(self, originalForwardInvocationSEL, invocation);
514520
}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+
}
516531
}
517532
}
518533

0 commit comments

Comments
 (0)
Please sign in to comment.