NSObject+FLEX_Reflection.m 13 KB

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