NSObject+Reflection.m 11 KB

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