FLEXProperty.m 8.4 KB

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