/**
Class information for a class.
一個類的類信息模型
*/
@interface YYClassInfo : NSObject
///< class object 本類類對象
@property (nonatomic, assign, readonly) Class cls;
///< super class object 父類類對象
@property (nullable, nonatomic, assign, readonly) Class superCls;
///< class's meta class object 元類類對象
@property (nullable, nonatomic, assign, readonly) Class metaCls;
///< whether this class is meta class 是否是一個元類
@property (nonatomic, readonly) BOOL isMeta;
///< class name 類名
@property (nonatomic, strong, readonly) NSString *name;
///< super class's class info 父類的類信息
@property (nullable, nonatomic, strong, readonly) YYClassInfo *superClassInfo;
// 變量列表(數(shù)組)
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassIvarInfo *> *ivarInfos; ///< ivars
// 方法列表(數(shù)組)
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassMethodInfo *> *methodInfos; ///< methods
// 屬性列表(數(shù)組)
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassPropertyInfo *> *propertyInfos; ///< properties
/**
If the class is changed (for example: you add a method to this class with
'class_addMethod()'), you should call this method to refresh the class info cache.
如果這個類是改變了的(例如: 你通過調用 class_addMethod() 給這類添加了方法 ),你就需要調用這個方法來刷新類的緩存信息。
After called this method, `needUpdate` will returns `YES`, and you should call
'classInfoWithClass' or 'classInfoWithClassName' to get the updated class info.
調用這個方法后, needUpdate 將返回 yes, 你將調用 classInfoWithClass 或者 classInfoWithClassName 來根系類信息。
*/
- (void)setNeedUpdate;
/**
If this method returns `YES`, you should stop using this instance and call
`classInfoWithClass` or `classInfoWithClassName` to get the updated class info.
如果這個方法返回 yes , 你需要停止使用這個類的實例并調用 classInfoWithClass 或 classInfoWithClassName 去更新類信息。
@return Whether this class info need update.
*/
- (BOOL)needUpdate;
/**
Get the class info of a specified Class.
獲取指定類的類信息。
@discussion This method will cache the class info and super-class info
at the first access to the Class. This method is thread-safe.
在第一次使用這個類的時候,這個方法將存儲本類的類信息和父類的類信息。 這個方法是線程安全的。
@param cls A class.
@return A class info, or nil if an error occurs.
一個類的類信息,如果是 nil 就會發(fā)生一個錯誤。
*/
+ (nullable instancetype)classInfoWithClass:(Class)cls;
這個方法和上面的方法是一樣的。
/**
Get the class info of a specified Class.
@discussion This method will cache the class info and super-class info
at the first access to the Class. This method is thread-safe.
@param className A class name.
@return A class info, or nil if an error occurs.
*/
+ (nullable instancetype)classInfoWithClassName:(NSString *)className;
@implementation YYClassInfo {
// 是否需要更新類信息
BOOL _needUpdate;
}
- (instancetype)initWithClass:(Class)cls {
// cls 為 nil 就直接返回 nil
if (!cls) return nil;
// 初始化父類
self = [super init];
// 記錄 “類”
_cls = cls;
// 記錄“父類”
_superCls = class_getSuperclass(cls);
// 判斷是否是 "元" 類
/*
判斷是一個 元類,主要是要確定是一個 “類“ 不是一個 ”實例“
*/
_isMeta = class_isMetaClass(cls);
// 不是 “元” 類
if (!_isMeta) {
// 獲取當前類的 “元” 類
_metaCls = objc_getMetaClass(class_getName(cls));
}
// 獲取類名
_name = NSStringFromClass(cls);
// 調用 update 方法
[self _update];
// 獲取父類的類信息
_superClassInfo = [self.class classInfoWithClass:_superCls];
return self;
}
- (void)_update {
_ivarInfos = nil;
_methodInfos = nil;
_propertyInfos = nil;
// 實例一個 "類" 制作
Class cls = self.cls;
// 定義變量記錄方法的個數(shù)
unsigned int methodCount = 0;
// 獲取方法列表信息
Method *methods = class_copyMethodList(cls, &methodCount);
if (methods) {
// 獲取方法信息
NSMutableDictionary *methodInfos = [NSMutableDictionary new];
_methodInfos = methodInfos;
for (unsigned int i = 0; i < methodCount; i++) {
YYClassMethodInfo *info = [[YYClassMethodInfo alloc] initWithMethod:methods[i]];
if (info.name) methodInfos[info.name] = info;
}
free(methods);
}
// 獲取屬性列表信息
unsigned int propertyCount = 0;
objc_property_t *properties = class_copyPropertyList(cls, &propertyCount);
if (properties) {
NSMutableDictionary *propertyInfos = [NSMutableDictionary new];
_propertyInfos = propertyInfos;
for (unsigned int i = 0; i < propertyCount; i++) {
YYClassPropertyInfo *info = [[YYClassPropertyInfo alloc] initWithProperty:properties[i]];
if (info.name) propertyInfos[info.name] = info;
}
free(properties);
}
// 獲取變量信息
unsigned int ivarCount = 0;
Ivar *ivars = class_copyIvarList(cls, &ivarCount);
if (ivars) {
NSMutableDictionary *ivarInfos = [NSMutableDictionary new];
_ivarInfos = ivarInfos;
for (unsigned int i = 0; i < ivarCount; i++) {
YYClassIvarInfo *info = [[YYClassIvarInfo alloc] initWithIvar:ivars[i]];
if (info.name) ivarInfos[info.name] = info;
}
free(ivars);
}
// 當獲取的方法列表信息,屬性列表信息,變量列表信息為空的時候對相應的存儲變量置空
if (!_ivarInfos) _ivarInfos = @{};
if (!_methodInfos) _methodInfos = @{};
if (!_propertyInfos) _propertyInfos = @{};
// 更新是否更新的標記
_needUpdate = NO;
}
- (void)setNeedUpdate {
_needUpdate = YES;
}
- (BOOL)needUpdate {
return _needUpdate;
}
// 一個類方法
+ (instancetype)classInfoWithClass:(Class)cls {
// 判斷類是否有值
if (!cls) return nil;
// 緩存池用的是全局靜態(tài)變量
// 類緩存池
static CFMutableDictionaryRef classCache;
// 元類緩存池
static CFMutableDictionaryRef metaCache;
// 鎖
static dispatch_once_t onceToken;
static dispatch_semaphore_t lock;
// 類緩存池,和 元類緩存池保證只初始化一次
dispatch_once(&onceToken, ^{
classCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
metaCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
lock = dispatch_semaphore_create(1);
});
dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
// 獲取緩存中的信息
YYClassInfo *info = CFDictionaryGetValue(class_isMetaClass(cls) ? metaCache : classCache, (__bridge const void *)(cls));
if (info && info->_needUpdate) {
[info _update];
}
dispatch_semaphore_signal(lock);
// 沒有從緩存池中獲取信息直接實例化一個
if (!info) {
info = [[YYClassInfo alloc] initWithClass:cls];
if (info) {
dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
// 將信息保存到緩存池中
CFDictionarySetValue(info.isMeta ? metaCache : classCache, (__bridge const void *)(cls), (__bridge const void *)(info));
dispatch_semaphore_signal(lock);
}
}
return info;
}
+ (instancetype)classInfoWithClassName:(NSString *)className {
// 通過字符串實例化一個類
Class cls = NSClassFromString(className);
return [self classInfoWithClass:cls];
}
@end