Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对forwardInvocation:原始方法做个复制 #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Aspects.m
Original file line number Diff line number Diff line change
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