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

Why reimplement aspect_swizzleClassInPlace method in RFAspects.m? #4

Open
hawk0620 opened this issue Jul 1, 2019 · 2 comments
Open

Comments

@hawk0620
Copy link

hawk0620 commented Jul 1, 2019

Hello, Can I ask you why choose AssociatedObject instead of singleton NSMutableSet in RFAspects.m? Is it for the memory optimization? Thank you

static Class aspect_swizzleClassInPlace(Class klass) {
    NSCParameterAssert(klass);
//    NSString *className = NSStringFromClass(klass);
//
//    _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) {
//        if (![swizzledClasses containsObject:className]) {
//            aspect_swizzleForwardInvocation(klass);
//            [swizzledClasses addObject:className];
//        }
//    });
    NSNumber *hasBeenHooked = objc_getAssociatedObject(klass, kClassHasBeenHookedKey);
    if (!hasBeenHooked.boolValue) {
        aspect_swizzleForwardInvocation(klass);
        objc_setAssociatedObject(klass, kClassHasBeenHookedKey, @YES, OBJC_ASSOCIATION_RETAIN);
    }
    return klass;
}
@Zepo
Copy link
Owner

Zepo commented Jul 1, 2019

Aspects(https://github.com/steipete/Aspects) only supports hooking instance methods, bug not class methods. The code changing you mentioned is for supporting hooking class methods.

@hawk0620
Copy link
Author

hawk0620 commented Jul 1, 2019

Thanks a lot. Emmmmm, I test hooking class method as you told me, but I can't get the methodSignature, then identifierWithSelector:object:options:block:error: directly return nil

static BOOL aspect_isCompatibleBlockSignature(NSMethodSignature *blockSignature, id object, SEL selector, NSError **error) {
    NSCParameterAssert(blockSignature);
    NSCParameterAssert(object);
    NSCParameterAssert(selector);

    BOOL signaturesMatch = YES;
    NSMethodSignature *methodSignature = [[object class] instanceMethodSignatureForSelector:selector];
...
// the methodSignature is nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants