NSObject+Reflection.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // NSObject+Reflection.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 "NSObject+Reflection.h"
  10. #import "FLEXMirror.h"
  11. #import "FLEXProperty.h"
  12. #import "FLEXMethod.h"
  13. #import "FLEXIvar.h"
  14. #import "FLEXPropertyAttributes.h"
  15. NSString * FLEXTypeEncodingString(const char *returnType, NSUInteger count, ...) {
  16. if (returnType == NULL) return nil;
  17. NSMutableString *encoding = [NSMutableString string];
  18. [encoding appendFormat:@"%s%s%s", returnType, @encode(id), @encode(SEL)];
  19. va_list args;
  20. va_start(args, count);
  21. char *type = va_arg(args, char *);
  22. for (NSUInteger i = 0; i < count; i++, type = va_arg(args, char *)) {
  23. [encoding appendFormat:@"%s", type];
  24. }
  25. va_end(args);
  26. return encoding.copy;
  27. }
  28. #pragma mark Reflection
  29. @implementation NSObject (Reflection)
  30. + (FLEXMirror *)flex_reflection {
  31. return [FLEXMirror reflect:self];
  32. }
  33. - (FLEXMirror *)flex_reflection {
  34. return [FLEXMirror reflect:self];
  35. }
  36. /// Code borrowed from MAObjCRuntime by Mike Ash
  37. + (NSArray *)flex_allSubclasses {
  38. Class *buffer = NULL;
  39. int count, size;
  40. do {
  41. count = objc_getClassList(NULL, 0);
  42. buffer = (Class *)realloc(buffer, count * sizeof(*buffer));
  43. size = objc_getClassList(buffer, count);
  44. } while (size != count);
  45. NSMutableArray *array = [NSMutableArray array];
  46. for (int i = 0; i < count; i++) {
  47. Class candidate = buffer[i];
  48. Class superclass = candidate;
  49. while (superclass) {
  50. if (superclass == self) {
  51. [array addObject:candidate];
  52. break;
  53. }
  54. superclass = class_getSuperclass(superclass);
  55. }
  56. }
  57. free(buffer);
  58. return array;
  59. }
  60. - (Class)flex_setClass:(Class)cls {
  61. return object_setClass(self, cls);
  62. }
  63. + (Class)flex_metaclass {
  64. return objc_getMetaClass(NSStringFromClass(self.class).UTF8String);
  65. }
  66. + (size_t)flex_instanceSize {
  67. return class_getInstanceSize(self.class);
  68. }
  69. + (Class)flex_setSuperclass:(Class)superclass {
  70. #pragma clang diagnostic push
  71. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  72. return class_setSuperclass(self, superclass);
  73. #pragma clang diagnostic pop
  74. }
  75. + (NSArray<Class> *)flex_classHierarchy {
  76. NSMutableArray *classes = [NSMutableArray array];
  77. Class cls = self;
  78. do {
  79. [classes addObject:cls];
  80. } while ((cls = [cls superclass]));
  81. return classes.copy;
  82. }
  83. @end
  84. #pragma mark Methods
  85. @implementation NSObject (Methods)
  86. + (NSArray<FLEXMethod *> *)flex_allMethods {
  87. NSMutableArray *instanceMethods = (id)self.flex_allInstanceMethods;
  88. [instanceMethods addObjectsFromArray:self.flex_allClassMethods];
  89. return instanceMethods;
  90. }
  91. + (NSArray<FLEXMethod *> *)flex_allInstanceMethods {
  92. unsigned int mcount;
  93. Method *objcmethods = class_copyMethodList([self class], &mcount);
  94. NSMutableArray *methods = [NSMutableArray array];
  95. for (int i = 0; i < mcount; i++) {
  96. FLEXMethod *m = [FLEXMethod method:objcmethods[i] isInstanceMethod:YES];
  97. if (m) {
  98. [methods addObject:m];
  99. }
  100. }
  101. free(objcmethods);
  102. return methods;
  103. }
  104. + (NSArray<FLEXMethod *> *)flex_allClassMethods {
  105. unsigned int mcount;
  106. Method *objcmethods = class_copyMethodList(self.flex_metaclass, &mcount);
  107. NSMutableArray *methods = [NSMutableArray array];
  108. for (int i = 0; i < mcount; i++) {
  109. FLEXMethod *m = [FLEXMethod method:objcmethods[i] isInstanceMethod:NO];
  110. if (m) {
  111. [methods addObject:m];
  112. }
  113. }
  114. free(objcmethods);
  115. return methods;
  116. }
  117. + (FLEXMethod *)flex_methodNamed:(NSString *)name {
  118. Method m = class_getInstanceMethod([self class], NSSelectorFromString(name));
  119. if (m == NULL) {
  120. return nil;
  121. }
  122. return [FLEXMethod method:m isInstanceMethod:YES];
  123. }
  124. + (FLEXMethod *)flex_classMethodNamed:(NSString *)name {
  125. Method m = class_getClassMethod([self class], NSSelectorFromString(name));
  126. if (m == NULL) {
  127. return nil;
  128. }
  129. return [FLEXMethod method:m isInstanceMethod:NO];
  130. }
  131. + (BOOL)addMethod:(SEL)selector
  132. typeEncoding:(NSString *)typeEncoding
  133. implementation:(IMP)implementaiton
  134. toInstances:(BOOL)instance {
  135. return class_addMethod(instance ? self.class : self.flex_metaclass, selector, implementaiton, typeEncoding.UTF8String);
  136. }
  137. + (IMP)replaceImplementationOfMethod:(FLEXMethodBase *)method with:(IMP)implementation useInstance:(BOOL)instance {
  138. return class_replaceMethod(instance ? self.class : self.flex_metaclass, method.selector, implementation, method.typeEncoding.UTF8String);
  139. }
  140. + (void)swizzle:(FLEXMethodBase *)original with:(FLEXMethodBase *)other onInstance:(BOOL)instance {
  141. [self swizzleBySelector:original.selector with:other.selector onInstance:instance];
  142. }
  143. + (BOOL)swizzleByName:(NSString *)original with:(NSString *)other onInstance:(BOOL)instance {
  144. SEL originalMethod = NSSelectorFromString(original);
  145. SEL newMethod = NSSelectorFromString(other);
  146. if (originalMethod == 0 || newMethod == 0) {
  147. return NO;
  148. }
  149. [self swizzleBySelector:originalMethod with:newMethod onInstance:instance];
  150. return YES;
  151. }
  152. + (void)swizzleBySelector:(SEL)original with:(SEL)other onInstance:(BOOL)instance {
  153. Class cls = instance ? self.class : self.flex_metaclass;
  154. Method originalMethod = class_getInstanceMethod(cls, original);
  155. Method newMethod = class_getInstanceMethod(cls, other);
  156. if (class_addMethod(cls, original, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
  157. class_replaceMethod(cls, other, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  158. } else {
  159. method_exchangeImplementations(originalMethod, newMethod);
  160. }
  161. }
  162. @end
  163. #pragma mark Ivars
  164. @implementation NSObject (Ivars)
  165. + (NSArray<FLEXIvar *> *)flex_allIvars {
  166. unsigned int ivcount;
  167. Ivar *objcivars = class_copyIvarList([self class], &ivcount);
  168. NSMutableArray *ivars = [NSMutableArray array];
  169. for (int i = 0; i < ivcount; i++) {
  170. [ivars addObject:[FLEXIvar ivar:objcivars[i]]];
  171. }
  172. free(objcivars);
  173. return ivars;
  174. }
  175. + (FLEXIvar *)flex_ivarNamed:(NSString *)name {
  176. Ivar i = class_getInstanceVariable([self class], name.UTF8String);
  177. if (i == NULL) {
  178. return nil;
  179. }
  180. return [FLEXIvar ivar:i];
  181. }
  182. #pragma mark Get address
  183. - (void *)flex_getIvarAddress:(FLEXIvar *)ivar {
  184. return (uint8_t *)(__bridge void *)self + ivar.offset;
  185. }
  186. - (void *)flex_getObjcIvarAddress:(Ivar)ivar {
  187. return (uint8_t *)(__bridge void *)self + ivar_getOffset(ivar);
  188. }
  189. - (void *)flex_getIvarAddressByName:(NSString *)name {
  190. Ivar ivar = class_getInstanceVariable(self.class, name.UTF8String);
  191. if (!ivar) return 0;
  192. return (uint8_t *)(__bridge void *)self + ivar_getOffset(ivar);
  193. }
  194. #pragma mark Set ivar object
  195. - (void)flex_setIvar:(FLEXIvar *)ivar object:(id)value {
  196. object_setIvar(self, ivar.objc_ivar, value);
  197. }
  198. - (BOOL)flex_setIvarByName:(NSString *)name object:(id)value {
  199. Ivar ivar = class_getInstanceVariable(self.class, name.UTF8String);
  200. if (!ivar) return NO;
  201. object_setIvar(self, ivar, value);
  202. return YES;
  203. }
  204. - (void)flex_setObjcIvar:(Ivar)ivar object:(id)value {
  205. object_setIvar(self, ivar, value);
  206. }
  207. #pragma mark Set ivar value
  208. - (void)flex_setIvar:(FLEXIvar *)ivar value:(void *)value size:(size_t)size {
  209. void *address = [self flex_getIvarAddress:ivar];
  210. memcpy(address, value, size);
  211. }
  212. - (BOOL)flex_setIvarByName:(NSString *)name value:(void *)value size:(size_t)size {
  213. Ivar ivar = class_getInstanceVariable(self.class, name.UTF8String);
  214. if (!ivar) return NO;
  215. [self flex_setObjcIvar:ivar value:value size:size];
  216. return YES;
  217. }
  218. - (void)flex_setObjcIvar:(Ivar)ivar value:(void *)value size:(size_t)size {
  219. void *address = [self flex_getObjcIvarAddress:ivar];
  220. memcpy(address, value, size);
  221. }
  222. @end
  223. #pragma mark Properties
  224. @implementation NSObject (Properties)
  225. + (NSArray<FLEXProperty *> *)flex_allProperties {
  226. NSMutableArray *instanceProperties = (id)self.flex_allInstanceProperties;
  227. [instanceProperties addObjectsFromArray:self.flex_allClassProperties];
  228. return instanceProperties;
  229. }
  230. + (NSArray<FLEXProperty *> *)flex_allInstanceProperties {
  231. unsigned int pcount;
  232. objc_property_t *objcproperties = class_copyPropertyList(self, &pcount);
  233. NSMutableArray *properties = [NSMutableArray array];
  234. for (int i = 0; i < pcount; i++) {
  235. [properties addObject:[FLEXProperty property:objcproperties[i] onClass:self]];
  236. }
  237. free(objcproperties);
  238. return properties;
  239. }
  240. + (NSArray<FLEXProperty *> *)flex_allClassProperties {
  241. Class metaclass = self.flex_metaclass;
  242. unsigned int pcount;
  243. objc_property_t *objcproperties = class_copyPropertyList(metaclass, &pcount);
  244. NSMutableArray *properties = [NSMutableArray array];
  245. for (int i = 0; i < pcount; i++) {
  246. [properties addObject:[FLEXProperty property:objcproperties[i] onClass:metaclass]];
  247. }
  248. free(objcproperties);
  249. return properties;
  250. }
  251. + (FLEXProperty *)flex_propertyNamed:(NSString *)name {
  252. objc_property_t p = class_getProperty([self class], name.UTF8String);
  253. if (p == NULL) {
  254. return nil;
  255. }
  256. return [FLEXProperty property:p onClass:self];
  257. }
  258. + (FLEXProperty *)flex_classPropertyNamed:(NSString *)name {
  259. objc_property_t p = class_getProperty(object_getClass(self), name.UTF8String);
  260. if (p == NULL) {
  261. return nil;
  262. }
  263. return [FLEXProperty property:p onClass:object_getClass(self)];
  264. }
  265. + (void)flex_replaceProperty:(FLEXProperty *)property {
  266. [self flex_replaceProperty:property.name attributes:property.attributes];
  267. }
  268. + (void)flex_replaceProperty:(NSString *)name attributes:(FLEXPropertyAttributes *)attributes {
  269. unsigned int count;
  270. objc_property_attribute_t *objc_attributes = [attributes copyAttributesList:&count];
  271. class_replaceProperty([self class], name.UTF8String, objc_attributes, count);
  272. free(objc_attributes);
  273. }
  274. @end