Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge 6c9169f into ab3a811
  • Loading branch information
springwinding committed Jan 13, 2016
2 parents ab3a811 + 6c9169f commit 24a83a7
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Aspects.m
Expand Up @@ -388,12 +388,18 @@ static Class aspect_hookClass(NSObject *self, NSError **error) {
}

static NSString *const AspectsForwardInvocationSelectorName = @"__aspects_forwardInvocation:";
static NSString *const AspectsForwardInvocationOrignSelectorName = @"__aspects_orignForwardInvocation:";
static void aspect_swizzleForwardInvocation(Class klass) {
NSCParameterAssert(klass);
// If there is no method, replace will act like class_addMethod.
IMP originalImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@");
Method forwardMethod = class_getInstanceMethod(klass, @selector(forwardInvocation:));
IMP originalImplementation = method_getImplementation(forwardMethod);
if (originalImplementation) {
class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@");
class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationOrignSelectorName), originalImplementation, "v@:@");
}
IMP replaceImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@");
if (replaceImplementation) {
class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), replaceImplementation, "v@:@");
}
AspectLog(@"Aspects: %@ is now aspect aware.", NSStringFromClass(klass));
}
Expand Down Expand Up @@ -512,7 +518,16 @@ static void __ASPECTS_ARE_BEING_CALLED__(__unsafe_unretained NSObject *self, SEL
if ([self respondsToSelector:originalForwardInvocationSEL]) {
((void( *)(id, SEL, NSInvocation *))objc_msgSend)(self, originalForwardInvocationSEL, invocation);
}else {
[self doesNotRecognizeSelector:invocation.selector];
SEL originalForwardInvocationSEL = NSSelectorFromString(AspectsForwardInvocationOrignSelectorName);
Class klass = object_getClass(invocation.target);
Method originalForwardMethod = class_getInstanceMethod(klass, originalForwardInvocationSEL);
// There is no class_removeMethod, so the best we can do is to retore the original implementation, or use a dummy.
IMP originalImplementation = method_getImplementation(originalForwardMethod);
if (originalImplementation) {
((void( *)(id, SEL, NSInvocation *))originalImplementation)(self, selector, invocation);
}else {
[self doesNotRecognizeSelector:invocation.selector];
}
}
}

Expand Down

0 comments on commit 24a83a7

Please sign in to comment.