forked from mikeash/MAObjCRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MARTNSObject.m
180 lines (144 loc) · 3.94 KB
/
MARTNSObject.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#import "MARTNSObject.h"
#import <objc/runtime.h>
#import "RTProtocol.h"
#import "RTIvar.h"
#import "RTProperty.h"
#import "RTMethod.h"
#import "RTUnregisteredClass.h"
@implementation NSObject (MARuntime)
+ (NSArray *)rt_subclasses
{
Class *buffer = NULL;
int count, size;
do
{
count = objc_getClassList(NULL, 0);
buffer = realloc(buffer, count * sizeof(*buffer));
size = objc_getClassList(buffer, count);
} while(size != count);
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i < count; i++)
{
Class candidate = buffer[i];
Class superclass = candidate;
while(superclass)
{
if(superclass == self)
{
[array addObject: candidate];
break;
}
superclass = class_getSuperclass(superclass);
}
}
free(buffer);
return array;
}
+ (RTUnregisteredClass *)rt_createUnregisteredSubclassNamed: (NSString *)name
{
return [RTUnregisteredClass unregisteredClassWithName: name withSuperclass: self];
}
+ (Class)rt_createSubclassNamed: (NSString *)name
{
return [[self rt_createUnregisteredSubclassNamed: name] registerClass];
}
+ (void)rt_destroyClass
{
objc_disposeClassPair(self);
}
+ (BOOL)rt_isMetaClass
{
return class_isMetaClass(self);
}
#ifdef __clang__
#pragma clang diagnostic push
#endif
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+ (Class)rt_setSuperclass: (Class)newSuperclass
{
return class_setSuperclass(self, newSuperclass);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
+ (size_t)rt_instanceSize
{
return class_getInstanceSize(self);
}
+ (NSArray *)rt_protocols
{
unsigned int count;
Protocol **protocols = class_copyProtocolList(self, &count);
NSMutableArray *array = [NSMutableArray array];
for(unsigned i = 0; i < count; i++)
[array addObject: [RTProtocol protocolWithObjCProtocol: protocols[i]]];
free(protocols);
return array;
}
+ (NSArray *)rt_methods
{
unsigned int count;
Method *methods = class_copyMethodList(self, &count);
NSMutableArray *array = [NSMutableArray array];
for(unsigned i = 0; i < count; i++)
[array addObject: [RTMethod methodWithObjCMethod: methods[i]]];
free(methods);
return array;
}
+ (RTMethod *)rt_methodForSelector: (SEL)sel
{
Method m = class_getInstanceMethod(self, sel);
if(!m) return nil;
return [RTMethod methodWithObjCMethod: m];
}
+ (void)rt_addMethod: (RTMethod *)method
{
class_addMethod(self, [method selector], [method implementation], [[method signature] UTF8String]);
}
+ (NSArray *)rt_ivars
{
unsigned int count;
Ivar *list = class_copyIvarList(self, &count);
NSMutableArray *array = [NSMutableArray array];
for(unsigned i = 0; i < count; i++)
[array addObject: [RTIvar ivarWithObjCIvar: list[i]]];
free(list);
return array;
}
+ (RTIvar *)rt_ivarForName: (NSString *)name
{
Ivar ivar = class_getInstanceVariable(self, [name UTF8String]);
if(!ivar) return nil;
return [RTIvar ivarWithObjCIvar: ivar];
}
+ (NSArray *)rt_properties
{
unsigned int count;
objc_property_t *list = class_copyPropertyList(self, &count);
NSMutableArray *array = [NSMutableArray array];
for(unsigned i = 0; i < count; i++)
[array addObject: [RTProperty propertyWithObjCProperty: list[i]]];
free(list);
return array;
}
+ (RTProperty *)rt_propertyForName: (NSString *)name
{
objc_property_t property = class_getProperty(self, [name UTF8String]);
if(!property) return nil;
return [RTProperty propertyWithObjCProperty: property];
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
+ (BOOL)rt_addProperty: (RTProperty *)property
{
return [property addToClass:self];
}
#endif
- (Class)rt_class
{
return object_getClass(self);
}
- (Class)rt_setClass: (Class)newClass
{
return object_setClass(self, newClass);
}
@end