今天來說一講 _read_images ,相信你們已經厭煩了講 _read_images 這個方法了。但畢竟這個方法確實很重要,因此纔在這裏着墨最多。今天把最後的幾個代碼塊貼出來,你們一塊兒來看看吧。git
if (!noClassesRemapped()) {
for (EACH_HEADER) {
Class *classrefs = _getObjc2ClassRefs(hi, &count);
for (i = 0; i < count; i++) {
//這裏筆者打印出了相關信息
printf("ClassRefs:%s",classrefs[i]->mangledName());
fflush(stdout);
remapClassRef(&classrefs[i]);
}
// fixme why doesn't test future1 catch the absence of this? classrefs = _getObjc2SuperRefs(hi, &count); for (i = 0; i < count; i++) { remapClassRef(&classrefs[i]); } } } 複製代碼
static size_t UnfixedSelectors;
for (EACH_HEADER) {
if (hi->isPreoptimized()) continue;
// printf("name:%s\n",hi->fname());
// fflush(stdout);
bool isBundle = hi->isBundle();
SEL *sels = _getObjc2SelectorRefs(hi, &count);
UnfixedSelectors += count;
for (i = 0; i < count; i++) {
const char *name = sel_cname(sels[i]);
// printf("name:%s\n",name);
// fflush(stdout);
sels[i] = sel_registerNameNoLock(name, isBundle);
}
}
複製代碼
for (EACH_HEADER) {
//如下兩行是筆者加的調試信息
printf("class name:%s\n",hi->fname());
fflush(stdout);
extern objc_class OBJC_CLASS_$_Protocol;
Class cls = (Class)&OBJC_CLASS_$_Protocol;
assert(cls);
NXMapTable *protocol_map = protocols();
bool isPreoptimized = hi->isPreoptimized();
bool isBundle = hi->isBundle();
protocol_t **protolist = _getObjc2ProtocolList(hi, &count);
for (i = 0; i < count; i++) {
//如下兩行是筆者加的調試信息
printf("protocol :%s\n",protolist[i]->nameForLogging());
fflush(stdout);
readProtocol(protolist[i], cls, protocol_map,
isPreoptimized, isBundle);
}
}
複製代碼
這個代碼段筆者要稍微說一下,由於這個變量名可能你們有點不能理解:github
OBJC_CLASS_$_Protocol
複製代碼
全局搜索,咱們發現,實際上是定義在 彙編文件bash
objc-sel-table.s
複製代碼
中的。筆者將打印出來的信息放到筆者的 GitHub 中了。供你們查閱,地址在這裏:
protocollist.txt
你們可能留意到文件最後有個 KysonHere 協議,實際上是筆者本身加的,這裏筆者將本身寫的測試類,以及協議代碼貼出來:app
@protocol KysonHere <NSObject>
@optional
-(void) kysonIsHer;
@end
複製代碼
也算是筆者的一段測試代碼了。測試
for (EACH_HEADER) {
protocol_t **protolist = _getObjc2ProtocolRefs(hi, &count);
for (i = 0; i < count; i++) {
//如下兩行爲筆者的測試代碼
printf("ref protocol :%s\n",protolist[i]->nameForLogging());
fflush(stdout);
remapProtocolRef(&protolist[i]);
}
}
複製代碼
返回的是空。ui
// Discover categories.
for (EACH_HEADER) {
category_t **catlist = _getObjc2CategoryList(hi, &count);
bool hasClassProperties = hi->info()->hasCategoryClassProperties();
for (i = 0; i < count; i++) {
category_t *cat = catlist[i];
Class cls = remapClass(cat->cls);
if (!cls) {
catlist[i] = nil;
continue;
}
bool classExists = NO;
if (cat->instanceMethods || cat->protocols
|| cat->instanceProperties)
{
addUnattachedCategoryForClass(cat, cls, hi);
if (cls->isRealized()) {
remethodizeClass(cls);
classExists = YES;
}
}
if (cat->classMethods || cat->protocols
|| (hasClassProperties && cat->_classProperties))
{
addUnattachedCategoryForClass(cat, cls->ISA(), hi);
if (cls->ISA()->isRealized()) {
remethodizeClass(cls->ISA());
}
}
}
}
複製代碼
以上是對_read_images 方法的解析,但願你們有所收穫!this