Zepo / Reflow

A unidirectional data flow framework for Objective-C inspired by Flux, Redux and Vue.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why reimplement aspect_swizzleClassInPlace method in RFAspects.m?

hawk0620 opened this issue · comments

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;
}
commented

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.

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