NSObject+Reflection.m 11 KB

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