forked from S2Ler/MAObjCRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTProperty.m
318 lines (266 loc) · 7.79 KB
/
RTProperty.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#import "RTProperty.h"
@interface _RTObjCProperty : RTProperty
{
objc_property_t _property;
NSMutableDictionary *_attrs;
NSString *_name;
}
@end
@implementation _RTObjCProperty
- (id)initWithObjCProperty: (objc_property_t)property
{
if((self = [self init]))
{
_property = property;
NSArray *attrPairs = [[NSString stringWithUTF8String: property_getAttributes(property)] componentsSeparatedByString: @","];
_attrs = [[NSMutableDictionary alloc] initWithCapacity:[attrPairs count]];
for(NSString *attrPair in attrPairs)
[_attrs setObject:[attrPair substringFromIndex:1] forKey:[attrPair substringToIndex:1]];
}
return self;
}
- (id)initWithName: (NSString *)name attributes:(NSDictionary *)attributes
{
if((self = [self init]))
{
_name = [name copy];
_attrs = [attributes copy];
}
return self;
}
- (void)dealloc
{
[_attrs release];
[_name release];
[super dealloc];
}
- (NSString *)name
{
if (_property)
return [NSString stringWithUTF8String: property_getName(_property)];
else
return _name;
}
- (NSDictionary *)attributes
{
return [[_attrs copy] autorelease];
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_7 || __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
- (BOOL)addToClass:(Class)classToAddTo
{
NSDictionary *attrs = [self attributes];
objc_property_attribute_t *cattrs = (objc_property_attribute_t*)calloc([attrs count], sizeof(objc_property_attribute_t));
unsigned attrIdx = 0;
for (NSString *attrCode in attrs) {
cattrs[attrIdx].name = [attrCode UTF8String];
cattrs[attrIdx].value = [[attrs objectForKey:attrCode] UTF8String];
attrIdx++;
}
BOOL result = class_addProperty(classToAddTo,
[[self name] UTF8String],
cattrs,
[attrs count]);
free(cattrs);
return result;
}
#endif
- (NSString *)attributeEncodings
{
NSMutableArray *filteredAttributes = [NSMutableArray arrayWithCapacity:[_attrs count] - 2];
for (NSString *attrKey in _attrs)
{
if (![attrKey isEqualToString:RTPropertyTypeEncodingAttribute] && ![attrKey isEqualToString:RTPropertyBackingIVarNameAttribute])
[filteredAttributes addObject:[_attrs objectForKey:attrKey]];
}
return [filteredAttributes componentsJoinedByString: @","];
}
- (BOOL)hasAttribute: (NSString *)code
{
return [_attrs objectForKey:code] != nil;
}
- (BOOL)isReadOnly
{
return [self hasAttribute: RTPropertyReadOnlyAttribute];
}
- (RTPropertySetterSemantics)setterSemantics
{
if([self hasAttribute: RTPropertyCopyAttribute]) return RTPropertySetterSemanticsCopy;
if([self hasAttribute: RTPropertyRetainAttribute]) return RTPropertySetterSemanticsRetain;
return RTPropertySetterSemanticsAssign;
}
- (BOOL)isNonAtomic
{
return [self hasAttribute: RTPropertyNonAtomicAttribute];
}
- (BOOL)isDynamic
{
return [self hasAttribute: RTPropertyDynamicAttribute];
}
- (BOOL)isWeakReference
{
return [self hasAttribute: RTPropertyWeakReferenceAttribute];
}
- (BOOL)isEligibleForGarbageCollection
{
return [self hasAttribute: RTPropertyEligibleForGarbageCollectionAttribute];
}
- (NSString *)contentOfAttribute: (NSString *)code
{
return [_attrs objectForKey:code];
}
- (SEL)customGetter
{
return NSSelectorFromString([self contentOfAttribute: RTPropertyCustomGetterAttribute]);
}
- (SEL)customSetter
{
return NSSelectorFromString([self contentOfAttribute: RTPropertyCustomSetterAttribute]);
}
- (NSString *)typeEncoding
{
return [self contentOfAttribute: RTPropertyTypeEncodingAttribute];
}
- (NSString *)typeName
{
NSString *propertyTypeEncoded = [self typeEncoding];
propertyTypeEncoded = [propertyTypeEncoded stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"@\""]];
return propertyTypeEncoded;
}
- (NSString *)oldTypeEncoding
{
return [self contentOfAttribute: RTPropertyOldTypeEncodingAttribute];
}
- (NSString *)ivarName
{
return [self contentOfAttribute: RTPropertyBackingIVarNameAttribute];
}
@end
@implementation RTProperty
+ (id)propertyWithObjCProperty: (objc_property_t)property
{
return [[[self alloc] initWithObjCProperty: property] autorelease];
}
+ (id)propertyWithName: (NSString *)name attributes:(NSDictionary *)attributes
{
return [[[self alloc] initWithName: name attributes: attributes] autorelease];
}
- (id)initWithObjCProperty: (objc_property_t)property
{
[self release];
return [[_RTObjCProperty alloc] initWithObjCProperty: property];
}
- (id)initWithName: (NSString *)name attributes:(NSDictionary *)attributes
{
[self release];
return [[_RTObjCProperty alloc] initWithName: name attributes: attributes];
}
- (NSString *)description
{
return [NSString stringWithFormat: @"<%@ %p: %@ %@ %@ %@>", [self class], self, [self name], [self attributeEncodings], [self typeEncoding], [self ivarName]];
}
- (BOOL)isEqual: (id)other
{
return [other isKindOfClass: [RTProperty class]] &&
[[self name] isEqual: [other name]] &&
([self attributeEncodings] ? [[self attributeEncodings] isEqual: [other attributeEncodings]] : ![other attributeEncodings]) &&
[[self typeEncoding] isEqual: [other typeEncoding]] &&
([self ivarName] ? [[self ivarName] isEqual: [other ivarName]] : ![other ivarName]);
}
- (NSUInteger)hash
{
return [[self name] hash] ^ [[self typeEncoding] hash];
}
- (NSString *)name
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
- (NSDictionary *)attributes
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
- (BOOL)addToClass:(Class)classToAddTo
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (NSString *)attributeEncodings
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
- (BOOL)isReadOnly
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (RTPropertySetterSemantics)setterSemantics
{
[self doesNotRecognizeSelector: _cmd];
return RTPropertySetterSemanticsAssign;
}
- (BOOL)isNonAtomic
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (BOOL)isDynamic
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (BOOL)isWeakReference
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (BOOL)isEligibleForGarbageCollection
{
[self doesNotRecognizeSelector: _cmd];
return NO;
}
- (SEL)customGetter
{
[self doesNotRecognizeSelector: _cmd];
return (SEL)0;
}
- (SEL)customSetter
{
[self doesNotRecognizeSelector: _cmd];
return (SEL)0;
}
- (NSString *)typeEncoding
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
- (NSString *)typeName
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
- (NSString *)oldTypeEncoding
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
- (NSString *)ivarName
{
[self doesNotRecognizeSelector: _cmd];
return nil;
}
@end
NSString * const RTPropertyTypeEncodingAttribute = @"T";
NSString * const RTPropertyBackingIVarNameAttribute = @"V";
NSString * const RTPropertyCopyAttribute = @"C";
NSString * const RTPropertyCustomGetterAttribute = @"G";
NSString * const RTPropertyCustomSetterAttribute = @"S";
NSString * const RTPropertyDynamicAttribute = @"D";
NSString * const RTPropertyEligibleForGarbageCollectionAttribute = @"P";
NSString * const RTPropertyNonAtomicAttribute = @"N";
NSString * const RTPropertyOldTypeEncodingAttribute = @"t";
NSString * const RTPropertyReadOnlyAttribute = @"R";
NSString * const RTPropertyRetainAttribute = @"&";
NSString * const RTPropertyWeakReferenceAttribute = @"W";