FLEXProperty.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // FLEXProperty.m
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 6/30/15.
  7. // Copyright (c) 2015 Tanner Bennett. All rights reserved.
  8. //
  9. #import "FLEXProperty.h"
  10. #import "FLEXPropertyAttributes.h"
  11. #import "FLEXMethodBase.h"
  12. #import "FLEXRuntimeUtility.h"
  13. #include <dlfcn.h>
  14. @interface FLEXProperty () {
  15. NSString *_flex_description;
  16. }
  17. @property (nonatomic ) BOOL uniqueCheckFlag;
  18. @property (nonatomic, readonly) Class cls;
  19. @end
  20. @implementation FLEXProperty
  21. @synthesize multiple = _multiple;
  22. @synthesize imageName = _imageName;
  23. #pragma mark Initializers
  24. - (id)init {
  25. [NSException
  26. raise:NSInternalInconsistencyException
  27. format:@"Class instance should not be created with -init"
  28. ];
  29. return nil;
  30. }
  31. + (instancetype)property:(objc_property_t)property {
  32. return [[self alloc] initWithProperty:property onClass:nil];
  33. }
  34. + (instancetype)property:(objc_property_t)property onClass:(Class)cls {
  35. return [[self alloc] initWithProperty:property onClass:cls];
  36. }
  37. + (instancetype)named:(NSString *)name onClass:(Class)cls {
  38. NSParameterAssert(class_getProperty(cls, name.UTF8String));
  39. return [self property:class_getProperty(cls, name.UTF8String) onClass:cls];
  40. }
  41. + (instancetype)propertyWithName:(NSString *)name attributes:(FLEXPropertyAttributes *)attributes {
  42. return [[self alloc] initWithName:name attributes:attributes];
  43. }
  44. - (id)initWithProperty:(objc_property_t)property onClass:(Class)cls {
  45. NSParameterAssert(property);
  46. self = [super init];
  47. if (self) {
  48. _objc_property = property;
  49. _attributes = [FLEXPropertyAttributes attributesForProperty:property];
  50. _name = @(property_getName(property) ?: "(nil)");
  51. _cls = cls;
  52. if (!_attributes) [NSException raise:NSInternalInconsistencyException format:@"Error retrieving property attributes"];
  53. if (!_name) [NSException raise:NSInternalInconsistencyException format:@"Error retrieving property name"];
  54. [self examine];
  55. }
  56. return self;
  57. }
  58. - (id)initWithName:(NSString *)name attributes:(FLEXPropertyAttributes *)attributes {
  59. NSParameterAssert(name); NSParameterAssert(attributes);
  60. self = [super init];
  61. if (self) {
  62. _attributes = attributes;
  63. _name = name;
  64. [self examine];
  65. }
  66. return self;
  67. }
  68. #pragma mark Private
  69. - (void)examine {
  70. _type = (FLEXTypeEncoding)[self.attributes.typeEncoding characterAtIndex:0];
  71. // Return the given selector if the class responds to it
  72. Class cls = _cls;
  73. SEL (^selectorIfValid)() = ^SEL(SEL sel) {
  74. if (!sel || !cls) return nil;
  75. return [cls instancesRespondToSelector:sel] ? sel : nil;
  76. };
  77. SEL customGetter = self.attributes.customGetter;
  78. SEL customSetter = self.attributes.customSetter;
  79. SEL defaultGetter = NSSelectorFromString(self.name);
  80. SEL defaultSetter = NSSelectorFromString([NSString
  81. stringWithFormat:@"set%c%@:",
  82. (char)toupper([self.name characterAtIndex:0]),
  83. [self.name substringFromIndex:1]
  84. ]);
  85. // Check if the likely getters/setters exist
  86. SEL validGetter = selectorIfValid(customGetter) ?: selectorIfValid(defaultGetter);
  87. SEL validSetter = selectorIfValid(customSetter) ?: selectorIfValid(defaultSetter);
  88. _likelyGetterExists = validGetter != nil;
  89. _likelySetterExists = validSetter != nil;
  90. // Assign likely getters and setters to the valid one,
  91. // or the default, regardless of whether the default exists
  92. _likelyGetter = validGetter ?: defaultGetter;
  93. _likelySetter = validSetter ?: defaultSetter;
  94. _likelyGetterString = NSStringFromSelector(_likelyGetter);
  95. _likelySetterString = NSStringFromSelector(_likelySetter);
  96. _isClassProperty = _cls ? class_isMetaClass(_cls) : NO;
  97. }
  98. #pragma mark Overrides
  99. - (NSString *)description {
  100. if (!_flex_description) {
  101. NSString *readableType = [FLEXRuntimeUtility readableTypeForEncoding:self.attributes.typeEncoding];
  102. _flex_description = [FLEXRuntimeUtility appendName:self.name toType:readableType];
  103. }
  104. return _flex_description;
  105. }
  106. - (NSString *)debugDescription {
  107. return [NSString stringWithFormat:@"<%@ name=%@, property=%p, attributes:\n\t%@\n>",
  108. NSStringFromClass(self.class), self.name, self.objc_property, self.attributes];
  109. }
  110. #pragma mark Public
  111. - (objc_property_attribute_t *)copyAttributesList:(unsigned int *)attributesCount {
  112. if (self.objc_property) {
  113. return property_copyAttributeList(self.objc_property, attributesCount);
  114. } else {
  115. return [self.attributes copyAttributesList:attributesCount];
  116. }
  117. }
  118. - (void)replacePropertyOnClass:(Class)cls {
  119. class_replaceProperty(cls, self.name.UTF8String, self.attributes.list, (unsigned int)self.attributes.count);
  120. }
  121. - (void)computeSymbolInfo:(BOOL)forceBundle {
  122. if ((!_multiple || !_uniqueCheckFlag) && _cls) {
  123. _multiple = _objc_property != class_getProperty(_cls, self.name.UTF8String);
  124. if (_multiple || forceBundle) {
  125. Dl_info exeInfo;
  126. dladdr(_objc_property, &exeInfo);
  127. NSString *path = @(exeInfo.dli_fname).stringByDeletingLastPathComponent;
  128. _imageName = [NSBundle bundleWithPath:path].executablePath.lastPathComponent;
  129. }
  130. }
  131. }
  132. - (BOOL)multiple {
  133. [self computeSymbolInfo:NO];
  134. return _multiple;
  135. }
  136. - (NSString *)imageName {
  137. [self computeSymbolInfo:YES];
  138. return _imageName;
  139. }
  140. - (NSString *)fullDescription {
  141. NSMutableArray<NSString *> *attributesStrings = [NSMutableArray new];
  142. FLEXPropertyAttributes *attributes = self.attributes;
  143. // Atomicity
  144. if (attributes.isNonatomic) {
  145. [attributesStrings addObject:@"nonatomic"];
  146. } else {
  147. [attributesStrings addObject:@"atomic"];
  148. }
  149. // Storage
  150. if (attributes.isRetained) {
  151. [attributesStrings addObject:@"strong"];
  152. } else if (attributes.isCopy) {
  153. [attributesStrings addObject:@"copy"];
  154. } else if (attributes.isWeak) {
  155. [attributesStrings addObject:@"weak"];
  156. } else {
  157. [attributesStrings addObject:@"assign"];
  158. }
  159. // Mutability
  160. if (attributes.isReadOnly) {
  161. [attributesStrings addObject:@"readonly"];
  162. } else {
  163. [attributesStrings addObject:@"readwrite"];
  164. }
  165. // Class or not
  166. if (self.isClassProperty) {
  167. [attributesStrings addObject:@"class"];
  168. }
  169. // Custom getter/setter
  170. SEL customGetter = attributes.customGetter;
  171. SEL customSetter = attributes.customSetter;
  172. if (customGetter) {
  173. [attributesStrings addObject:[NSString stringWithFormat:@"getter=%s", sel_getName(customGetter)]];
  174. }
  175. if (customSetter) {
  176. [attributesStrings addObject:[NSString stringWithFormat:@"setter=%s", sel_getName(customSetter)]];
  177. }
  178. NSString *attributesString = [attributesStrings componentsJoinedByString:@", "];
  179. return [NSString stringWithFormat:@"@property (%@) %@", attributesString, self.description];
  180. }
  181. - (id)getValue:(id)target {
  182. if (!target) return nil;
  183. // We don't care about checking dynamically whether the getter
  184. // _now_ exists on this object. If the getter doesn't exist
  185. // when this property is initialized, it will never call it.
  186. // Just re-create the property object if you need to call it.
  187. if (self.likelyGetterExists) {
  188. BOOL objectIsClass = object_isClass(target);
  189. BOOL instanceAndInstanceProperty = !objectIsClass && !self.isClassProperty;
  190. BOOL classAndClassProperty = objectIsClass && self.isClassProperty;
  191. if (instanceAndInstanceProperty || classAndClassProperty) {
  192. return [FLEXRuntimeUtility performSelector:self.likelyGetter onObject:target];
  193. }
  194. }
  195. return nil;
  196. }
  197. - (id)getPotentiallyUnboxedValue:(id)target {
  198. if (!target) return nil;
  199. return [FLEXRuntimeUtility
  200. potentiallyUnwrapBoxedPointer:[self getValue:target]
  201. type:self.attributes.typeEncoding.UTF8String
  202. ];
  203. }
  204. #pragma mark Suggested getters and setters
  205. - (FLEXMethodBase *)getterWithImplementation:(IMP)implementation {
  206. NSString *types = [NSString stringWithFormat:@"%@%s%s", self.attributes.typeEncoding, @encode(id), @encode(SEL)];
  207. NSString *name = [NSString stringWithFormat:@"%@", self.name];
  208. FLEXMethodBase *getter = [FLEXMethodBase buildMethodNamed:name withTypes:types implementation:implementation];
  209. return getter;
  210. }
  211. - (FLEXMethodBase *)setterWithImplementation:(IMP)implementation {
  212. NSString *types = [NSString stringWithFormat:@"%s%s%s%@", @encode(void), @encode(id), @encode(SEL), self.attributes.typeEncoding];
  213. NSString *name = [NSString stringWithFormat:@"set%@:", self.name.capitalizedString];
  214. FLEXMethodBase *setter = [FLEXMethodBase buildMethodNamed:name withTypes:types implementation:implementation];
  215. return setter;
  216. }
  217. @end