不忘初心,砥砺前行.
前面在分享Aspects的时候用到了class_replaceMethod
,今天就来看看它内部是怎么实现的
class_replaceMethod
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);
}
从源码中可以看出,class_replaceMethod
方法内部是调用了addMethod
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))) {
// already exists
if (!replace) {
result = m->imp;
} else {
result = _method_setImplementation(cls, m, imp);
}
} else {
// fixme optimize
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;
}
首先通过调用getMethodNoSuper_nolock
去类的方法列表中查找,找到就判断传进来的replace
,如果为NO,就直接返回在方法列表中查找到的IMP,如果为YES,就交换IMP,并返回原来方法的IMP
如果在方法列表中没有找到对应的方法实现,创建一个method_list_t
,把方法信息赋值到method_list_t
中.最后再和原来的方法列表进行合并,这种方式添加的方法返回IMP为nil
static IMP
_method_setImplementation(Class cls, method_t *m, IMP imp)
{
runtimeLock.assertWriting();
if (!m) return nil;
if (!imp) return nil;
IMP old = m->imp;
m->imp = imp;
flushCaches(cls);
updateCustomRR_AWZ(cls, m);
return old;
}
交换方法实现,返回原来的方法实现
接下来看一下实践吧
@interface Person : NSObject
- (void)run;
@end
@implementation Person
@end
- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [[Person alloc]init];
self.person = person;
Method methodNew = class_getInstanceMethod([self class], @selector(customImp));
IMP methodIMPNew = method_getImplementation(methodNew);
const char *typeEncoding = method_getTypeEncoding(methodNew);
IMP replaceImp = class_replaceMethod([person class], @selector(run), methodIMPNew, typeEncoding);
NSLog(@"methodIMPNew =%p,replaceImp = %p",methodIMPNew,replaceImp);
}
打印结果:methodIMPNew =0x106704440,replaceImp = 0x0
Person
类中的run方法并没有实现,所以在方法列表中是找不到的,在addMethod
中就会创建一个集合,把methodIMPNew
传进去,方法名还是原来的方法名,返回nil,所以上面打印的replaceImp = 0x0
再看一下另一种情况
@implementation Person
- (void)run
{
NSLog(@"%s",__func__);
}
- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [[Person alloc]init];
self.person = person;
Method methodOld = class_getInstanceMethod([self.person class], @selector(run));
IMP methodIMPOld = method_getImplementation(methodOld);
Method methodNew = class_getInstanceMethod([self class], @selector(customImp));
IMP methodIMPNew = method_getImplementation(methodNew);
const char *typeEncoding = method_getTypeEncoding(methodNew);
// //把run方法的IMP替换成eat
IMP replaceImp = class_replaceMethod([self.person class], @selector(run), methodIMPNew, typeEncoding);
NSLog(@"methodIMPOld = %p,methodIMPNew =%p,replaceImp = %p",methodIMPOld,methodIMPNew,replaceImp);
}
@end
控制台打印:methodIMPOld = 0x105e8f640,methodIMPNew =0x105e8f3f0,replaceImp = 0x105e8f640
class_replaceMethod
返回的就是原来方法的IMP,但是此时run
方法的IMP却指向了customImp
方法的实现
总结一下: class_replaceMethod
返回的是原方法对应的IMP,如果原来方法没有实现就返回nil,调用了class_replaceMethod
会把原来方法的IMP指向另一个方法的实现
有不对的地方欢迎提出,让我们共同学习,共同成长...