Objective-C runtime机制(3)——method swizzling

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013378438/article/details/80583000

方法替换,又称为method swizzling,是一个比较著名的runtime黑魔法。网上有很多的实现,我们这里直接讲最正规的实现方式以及其背后的原理。

Method Swizzling

在进行方法替换前,我们要考虑两种情况:

  1. 要替换的方法在target class中有实现
  2. 要替换的方法在target class中没有实现,而是在其父类中实现

对于第一种情况,很简单,我们直接调用method_exchangeImplementations即可达成方法。

而对于第二种情况,我们要仔细想想了。
因为在target class中没有对应的方法实现,方法实际上是在target class的父类中实现的,因此当我们要交换方法实现时,其实是交换了target class父类的实现。这样当其他地方调用这个父类的方法时,也会调用我们所替换的方法,这显然使我们不想要的。

比如,我想替换UIViewController类中的methodForSelector:方法,其实该方法是在其父类NSObject类中实现的。如果我们直接调用method_exchangeImplementations,则会替换掉NSObject的方法。这样当我们在别的地方,比如UITableView中再调用methodForSelector:方法时,其实会调用到父类NSObject,而NSObject的实现,已经被我们替换了。

为了避免这种情况,我们在进行方法替换前,需要检查target class是否有对应方法的实现,如果没有,则要讲方法动态的添加到class的method list中。

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //要特别注意你替换的方法到底是哪个性质的方法
        // When swizzling a Instance method, use the following:
                Class class = [self class];

        // When swizzling a class method, use the following:
       // Class class = object_getClass((id)self);

        SEL originalSelector = @selector(systemMethod_PrintLog);
        SEL swizzledSelector = @selector(ll_imageName);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

作者:春田花花幼儿园
链接:https://www.jianshu.com/p/a6b675f4d073
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这是网上的一段代码例子,比较工整。

来解释一下:
这里我们用class_addMethod方法来检查target class是否有方法实现。如果target class没有实现对应方法的话,则class_addMethod会返回true,同时,会将方法添加到target class中。如果target class已经有对应的方法实现的话,则class_addMethod调用失败,返回false,这时,我们直接调用
method_exchangeImplementations方法来对调originalMethodswizzledMethod即可。

这里有两个细节,一个是在class_addMethod方法中,我们传入的SEL是originalSelector,而实现是swizzledMethod IMP,这样就等同于调换了方法。当add method成功后,我们又调用

 if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
 } 

class_replaceMethod方法其实在内部会首先尝试调用class_addMethod,将方法添加到class中,如果添加失败,则说明class已经存在该方法,这时,会调用method_setImplementation来设置方法的IMP

if (didAddMethod) 中,我们将swizzledMethod的IMP设置为了originalMethod IMP,完成了方法交换。

第二个细节是这段注释:

+(void)load {
//要特别注意你替换的方法到底是哪个性质的方法
        // When swizzling a Instance method, use the following:
                Class class = [self class];

        // When swizzling a class method, use the following:
        // Class class = object_getClass((id)self);
...
}

结合+(void)load方法的调用时机,它是由runtime在将class加载入内存中所调用的类方法。因此,我们一般会在这里面进行方法交换,因为时机是很靠前的。

这里要注意,在类方法中,self是一个类对象而不是实例对象。

当我们要替换类方法时,其实是要替换类对象所对应元类中的方法,要获取类对象的元类,需要调用
object_getClass方法,它会返回ISA(),而类对象ISA(),恰好是元类

当我们要替换实例方法时,需要找到实例所对应的类,这时,就需要调用[self class],虽然self类对象,但是+ class会返回类对象自身,也就是实例对象所对应的类。

这段话说的比较绕,如果模糊的同学可以结合上一章最后类,元类的关系进行理解。

附带class方法的实现源码:

NSObject.mm

+ (Class)class {
    return self;
}

- (Class)class {
    return object_getClass(self);
}

Method swizzling原理

就如之前所说,runtime中所谓的黑魔法,只不过是基于runtime底层数据结构的应用而已。

现在,我们就一次剖析在method swizzling中所用到的runtime函数以及其背后实现和所依赖的数据结构。

class & object_getClass

要进行方法替换,首先要清楚我们要替换哪个类中的方法,即target class

// When swizzling a Instance method, use the following:
        Class class = [self class];

// When swizzling a class method, use the following:
        Class class = object_getClass((id)self);

我们有两种方式获取Class对象,NSObjectclass方法以及runtime函数object_getClass。这两种方法的具体实现,还是有差别的。

class

先看NSObject的方法class,其实有两个版本,一个是实例方法,一个是类方法,其源码如下:

+ (Class)class {
    return self;
}

- (Class)class {
    return object_getClass(self);
}

当调用者是类对象时,会调用类方法版本,返回类对象自身。而调用者是实例对象时,会调用实例方法版本,在该版本中,又会调用runtime方法object_getClass

那么在object_getClass中,又做了什么呢?

object_getClass

Class object_getClass(id obj)
{
    if (obj) return obj->getIsa();
    else return Nil;
}

实现很简单,就是调用了对象的getIsa()方法。这里我们可以简单的理解为就是返回了对象的isa指针。

如果对象是实例对象isa返回实例对象所对应的类对象
如果对象是类对象isa返回类对象所对应的元类对象

我们在回过头来看这段注释(注意这里的前提是在+load()方法中,self类对象):

// When swizzling a Instance method, use the following:
        Class class = [self class];

// When swizzling a class method, use the following:
        Class class = object_getClass((id)self);

当我们要调换实例方法,则需要修改实例对象所对应的类对象的方法列表,因为这里的self已经是一个类对象,所有调用class方法其实会返回其自身,即实例对象对应的类对象

// When swizzling a Instance method, use the following:
        Class class = [self class];

当我们要调换类方法,则需要修改类对象所对应的元类对象的方法列表,因此要调用object_class方法,它会返回对象的isa,而类对象isa,则恰是类对象对应的元类对象:

// When swizzling a class method, use the following:
        Class class = object_getClass((id)self);

class_getInstanceMethod

确认了class后,我们就需要准备方法调用的原材料:originalMethod methodswizzled methodMethod数据类型在runtime中的定义为:

typedef struct method_t *Method;

struct method_t {
    SEL name;
    const char *types;
    IMP imp;

    struct SortBySELAddress :
        public std::binary_function<const method_t&,
                                    const method_t&, bool>
    {
        bool operator() (const method_t& lhs,
                         const method_t& rhs)
        { return lhs.name < rhs.name; }
    };
};

我们所说的类的方法列表中,就是存储的method_t类型。

Method数据类型的实例,如果自己创建的话,会比较麻烦,尤其是如何填充IMP,但我们可以从现有的class 方法列表中取出一个method来。很简单,只需要调用class_getInstanceMethod方法。

class_getInstanceMethod方法究竟做了什么呢?就像我们刚才说的一样,它就是在指定的类对象中的方法列表中去取SEL所对应的Method


/***********************************************************************
* class_getInstanceMethod.  Return the instance method for the
* specified class and selector.
**********************************************************************/
Method class_getInstanceMethod(Class cls, SEL sel)
{
    if (!cls  ||  !sel) return nil;        
    lookUpImpOrNil(cls, sel, nil, 
                   NO/*initialize*/, NO/*cache*/, YES/*resolver*/);
    return _class_getMethod(cls, sel);
}

class_getInstanceMethod 首先调用了lookUpImpOrNil,其实它的内部实现和普通的消息流程是一样的(内部会调用上一章中说所的消息查找函数lookUpImpOrForward),只不过对于消息转发得到的IMP,会替换为nil

在进行了一波消息流程之后,调用_class_getMethod方法

static Method _class_getMethod(Class cls, SEL sel)
{
    rwlock_reader_t lock(runtimeLock);
    return getMethod_nolock(cls, sel);
}

static method_t *
getMethod_nolock(Class cls, SEL sel)
{
    method_t *m = nil;
    runtimeLock.assertLocked();
    assert(cls->isRealized());
    // 核心:沿着继承链,向上查找第一个SEL所对应的method
    while (cls  &&  ((m = getMethodNoSuper_nolock(cls, sel))) == nil) {
        cls = cls->superclass;
    }

    return m;
}

// getMethodNoSuper_nolock 方法实质就是在查找class的消息列表
static method_t *
getMethodNoSuper_nolock(Class cls, SEL sel)
{
    runtimeLock.assertLocked();

    assert(cls->isRealized());
    // fixme nil cls? 
    // fixme nil sel?

    for (auto mlists = cls->data()->methods.beginLists(), 
              end = cls->data()->methods.endLists(); 
         mlists != end;
         ++mlists)
    {
        method_t *m = search_method_list(*mlists, sel);
        if (m) return m;
    }

    return nil;
}

class_addMethod

当我们获取到target classswizzled method后,首先尝试调用class_addMethod方法将swizzled method添加到target class中。

这样做的目的在于:如果target class中没有要替换的original method,则会直接将swizzled method 作为original method的实现添加到target class中。如果target class中确实存在original method,则class_addMethod会失败并返回false,我们就可以直接调用method_exchangeImplementations 方法来实现方法替换。这就是下面一段逻辑代码的意义:

BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }

我们先来看class_addMethod 是怎么实现的。其实到了这里,相信大家不用看代码也能猜的出来,class_addMethod 其实就是将我们提供的method,插入到target class的方法列表中。事实是这样的吗,看源码:

BOOL 
class_addMethod(Class cls, SEL name, IMP imp, const char *types)
{
    if (!cls) return NO;

    rwlock_writer_t lock(runtimeLock);
    return ! addMethod(cls, name, imp, types ?: "", NO);
}

static IMP 
addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace)
{
    IMP result = nil;

    runtimeLock.assertWriting();

    assert(types);
    assert(cls->isRealized());

    method_t *m;
    if ((m = getMethodNoSuper_nolock(cls, name))) {
        // 方法已经存在
        if (!replace) { // 如果选择不替换,则返回原始的方法,添加方法失败
            result = m->imp;
        } else {  // 如果选择替换,则返回原始方法,同时,替换为新的方法
            result = _method_setImplementation(cls, m, imp);
        }
    } else {
        // 方法不存在, 则在class的方法列表中添加方法, 并返回nil
        method_list_t *newlist;
        newlist = (method_list_t *)calloc(sizeof(*newlist), 1);
        newlist->entsizeAndFlags = 
            (uint32_t)sizeof(method_t) | fixed_up_method_list;
        newlist->count = 1;
        newlist->first.name = name;
        newlist->first.types = strdupIfMutable(types);
        newlist->first.imp = imp;

        prepareMethodLists(cls, &newlist, 1, NO, NO);
        cls->data()->methods.attachLists(&newlist, 1);
        flushCaches(cls);

        result = nil;
    }

    return result;
}

源码证明,我们的猜想是正确的:)

class_replaceMethod

如果class_addMethod返回成功,则说明我们已经为target class添加上了SEL为original SEL,并且其实现是swizzled method。至此,我们方法交换完成了一半,现在我们将swizzled method替换为original method

 if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
 } 

这里,我们调用了class_replaceMethod 方法。它的内部逻辑是这样的:1. 如果target class中没有SEL的对应实现,则会为target class添加上对应实现。 2. 如果target class中已经有了SEL对应的方法,则会将SEL对应的原始IMP,替换为新的IMP

IMP 
class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
{
    if (!cls) return nil;

    rwlock_writer_t lock(runtimeLock);
    return addMethod(cls, name, imp, types ?: "", YES);
}

static IMP 
addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace)
{
    IMP result = nil;

    runtimeLock.assertWriting();

    assert(types);
    assert(cls->isRealized());

    method_t *m;
    if ((m = getMethodNoSuper_nolock(cls, name))) {
        // 方法已经存在
        if (!replace) { // 如果选择不替换,则返回原始的方法,添加方法失败
            result = m->imp;
        } else {  // 如果选择替换,则返回原始方法,同时,替换为新的方法
            result = _method_setImplementation(cls, m, imp);
        }
    } else {
        // 方法不存在, 则在class的方法列表中添加方法, 并返回nil
        method_list_t *newlist;
        newlist = (method_list_t *)calloc(sizeof(*newlist), 1);
        newlist->entsizeAndFlags = 
            (uint32_t)sizeof(method_t) | fixed_up_method_list;
        newlist->count = 1;
        newlist->first.name = name;
        newlist->first.types = strdupIfMutable(types);
        newlist->first.imp = imp;

        prepareMethodLists(cls, &newlist, 1, NO, NO);
        cls->data()->methods.attachLists(&newlist, 1);
        flushCaches(cls);

        result = nil;
    }

    return result;
}

通过源码对比可以发现,class_addMethodclass_replaceMethod其实都是调用的addMethod方法,区别只是bool replace参数,一个是NO,不会替换原始实现,另一个是YES,会替换原始实现。

method_exchangeImplementations

如果class_addMethod 失败,则说明target class中的original method是在target class中有定义的,这时候,我们直接调用method_exchangeImplementations交换实现即可。method_exchangeImplementations 实现很简单,就是交换两个MethodIMP:


void method_exchangeImplementations(Method m1, Method m2)
{
    if (!m1  ||  !m2) return;

    rwlock_writer_t lock(runtimeLock);

    IMP m1_imp = m1->imp;
    m1->imp = m2->imp;
    m2->imp = m1_imp;


    // RR/AWZ updates are slow because class is unknown
    // Cache updates are slow because class is unknown
    // fixme build list of classes whose Methods are known externally?

    flushCaches(nil);

    updateCustomRR_AWZ(nil, m1);
    updateCustomRR_AWZ(nil, m2);
}

值得注意的地方

在写这篇博文的时候,笔者曾做过这个实验,在UIViewControllerCategory中,测试

- (void)exchangeImp {
    Class aClass = object_getClass(self);
    SEL originalSelector = @selector(viewWillAppear:);
    SEL swizzledSelector = @selector(sw_viewWillAppearXXX:);

    Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector);
    IMP result = class_replaceMethod(aClass, originalSelector,method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    NSLog(@"result is %p", result);
}

因为在class_replaceMethod方法中,如果target class已经存在SEL对应的方法实现,则会返回其old IMP,并替换为new IMP。本来以为result会返回viewWillAppear:的实现,但结果却是返回了nil。这是怎么回事呢?

究其根本,原来是因为我是在UIViewController的子类ViewController中调用的exchangeImp方法,那么object_getClass(self),其实会返回子类ViewController而不是UIViewController

class_replaceMethod中,runtime仅会查找当前类aClass,即ViewController的方法列表,而不会向上查询其父类UIViewController的方法列表。这样自然就找不到viewWillAppear:的实现啦。

而对于class_getInstanceMethodruntime除了查找当前类,还会沿着继承链向上查找对应的Method。

所以,这里就造成了,class_getInstanceMethod可以得到viewWillAppear:对应的Method,而在class_replaceMethod中,却找不到viewWillAppear:对应的IMP

如果不了解背后的实现,确实很难理解这种看似矛盾的结果。

猜你喜欢

转载自blog.csdn.net/u013378438/article/details/80583000