FLEXProperty.m 9.0 KB

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