FLEXObjectListViewController.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // FLEXObjectListViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectListViewController.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXObjectExplorerViewController.h"
  11. #import "FLEXMutableListSection.h"
  12. #import "FLEXRuntimeUtility.h"
  13. #import "FLEXUtility.h"
  14. #import "FLEXHeapEnumerator.h"
  15. #import "FLEXObjectRef.h"
  16. #import "NSString+FLEX.h"
  17. #import "NSObject+FLEX_Reflection.h"
  18. #import "FLEXTableViewCell.h"
  19. #import <malloc/malloc.h>
  20. @interface FLEXObjectListViewController ()
  21. @property (nonatomic, copy) NSArray<FLEXMutableListSection *> *sections;
  22. @property (nonatomic, copy) NSArray<FLEXMutableListSection *> *allSections;
  23. @property (nonatomic, readonly) NSArray<FLEXObjectRef *> *references;
  24. @property (nonatomic, readonly) NSArray<NSPredicate *> *predicates;
  25. @property (nonatomic, readonly) NSArray<NSString *> *sectionTitles;
  26. @end
  27. @implementation FLEXObjectListViewController
  28. @dynamic sections, allSections;
  29. #pragma mark - Reference Grouping
  30. + (NSPredicate *)defaultPredicateForSection:(NSInteger)section {
  31. // These are the types of references that we typically don't care about.
  32. // We want this list of "object-ivar pairs" split into two sections.
  33. BOOL(^isObserver)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
  34. NSString *row = ref.reference;
  35. return [row isEqualToString:@"__NSObserver object"] ||
  36. [row isEqualToString:@"_CFXNotificationObjcObserverRegistration _object"];
  37. };
  38. /// These are common AutoLayout related references we also rarely care about.
  39. BOOL(^isConstraintRelated)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
  40. static NSSet *ignored = nil;
  41. static dispatch_once_t onceToken;
  42. dispatch_once(&onceToken, ^{
  43. ignored = [NSSet setWithArray:@[
  44. @"NSLayoutConstraint _container",
  45. @"NSContentSizeLayoutConstraint _container",
  46. @"NSAutoresizingMaskLayoutConstraint _container",
  47. @"MASViewConstraint _installedView",
  48. @"MASLayoutConstraint _container",
  49. @"MASViewAttribute _view"
  50. ]];
  51. });
  52. NSString *row = ref.reference;
  53. return ([row hasPrefix:@"NSLayout"] && [row hasSuffix:@" _referenceItem"]) ||
  54. ([row hasPrefix:@"NSIS"] && [row hasSuffix:@" _delegate"]) ||
  55. ([row hasPrefix:@"_NSAutoresizingMask"] && [row hasSuffix:@" _referenceItem"]) ||
  56. [ignored containsObject:row];
  57. };
  58. BOOL(^isEssential)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
  59. return !(isObserver(ref, bindings) || isConstraintRelated(ref, bindings));
  60. };
  61. switch (section) {
  62. case 0: return [NSPredicate predicateWithBlock:isEssential];
  63. case 1: return [NSPredicate predicateWithBlock:isConstraintRelated];
  64. case 2: return [NSPredicate predicateWithBlock:isObserver];
  65. default: return nil;
  66. }
  67. }
  68. + (NSArray<NSPredicate *> *)defaultPredicates {
  69. return @[[self defaultPredicateForSection:0],
  70. [self defaultPredicateForSection:1],
  71. [self defaultPredicateForSection:2]];
  72. }
  73. + (NSArray<NSString *> *)defaultSectionTitles {
  74. return @[@"", @"AutoLayout", @"Trivial"];
  75. }
  76. #pragma mark - Initialization
  77. - (id)initWithReferences:(NSArray<FLEXObjectRef *> *)references {
  78. return [self initWithReferences:references predicates:nil sectionTitles:nil];
  79. }
  80. - (id)initWithReferences:(NSArray<FLEXObjectRef *> *)references
  81. predicates:(NSArray<NSPredicate *> *)predicates
  82. sectionTitles:(NSArray<NSString *> *)sectionTitles {
  83. NSParameterAssert(predicates.count == sectionTitles.count);
  84. self = [super initWithStyle:UITableViewStylePlain];
  85. if (self) {
  86. _references = references;
  87. _predicates = predicates;
  88. _sectionTitles = sectionTitles;
  89. }
  90. return self;
  91. }
  92. + (UIViewController *)instancesOfClassWithName:(NSString *)className {
  93. const char *classNameCString = className.UTF8String;
  94. NSMutableArray *instances = [NSMutableArray new];
  95. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
  96. if (strcmp(classNameCString, class_getName(actualClass)) == 0) {
  97. // Note: objects of certain classes crash when retain is called.
  98. // It is up to the user to avoid tapping into instance lists for these classes.
  99. // Ex. OS_dispatch_queue_specific_queue
  100. // In the future, we could provide some kind of warning for classes that are known to be problematic.
  101. if (malloc_size((__bridge const void *)(object)) > 0) {
  102. [instances addObject:object];
  103. }
  104. }
  105. }];
  106. NSArray<FLEXObjectRef *> *references = [FLEXObjectRef referencingAll:instances];
  107. if (references.count == 1) {
  108. return [FLEXObjectExplorerFactory
  109. explorerViewControllerForObject:references.firstObject.object
  110. ];
  111. }
  112. FLEXObjectListViewController *controller = [[self alloc] initWithReferences:references];
  113. controller.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)instances.count];
  114. return controller;
  115. }
  116. + (instancetype)subclassesOfClassWithName:(NSString *)className {
  117. NSArray<Class> *classes = FLEXGetAllSubclasses(NSClassFromString(className), NO);
  118. NSArray<FLEXObjectRef *> *references = [FLEXObjectRef referencingClasses:classes];
  119. FLEXObjectListViewController *controller = [[self alloc] initWithReferences:references];
  120. controller.title = [NSString stringWithFormat:@"Subclasses of %@ (%lu)",
  121. className, (unsigned long)classes.count
  122. ];
  123. return controller;
  124. }
  125. + (instancetype)objectsWithReferencesToObject:(id)object {
  126. static Class SwiftObjectClass = nil;
  127. static dispatch_once_t onceToken;
  128. dispatch_once(&onceToken, ^{
  129. SwiftObjectClass = NSClassFromString(@"SwiftObject");
  130. if (!SwiftObjectClass) {
  131. SwiftObjectClass = NSClassFromString(@"Swift._SwiftObject");
  132. }
  133. });
  134. NSMutableArray<FLEXObjectRef *> *instances = [NSMutableArray new];
  135. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id tryObject, __unsafe_unretained Class actualClass) {
  136. // Get all the ivars on the object. Start with the class and and travel up the inheritance chain.
  137. // Once we find a match, record it and move on to the next object. There's no reason to find multiple matches within the same object.
  138. Class tryClass = actualClass;
  139. while (tryClass) {
  140. unsigned int ivarCount = 0;
  141. Ivar *ivars = class_copyIvarList(tryClass, &ivarCount);
  142. for (unsigned int ivarIndex = 0; ivarIndex < ivarCount; ivarIndex++) {
  143. Ivar ivar = ivars[ivarIndex];
  144. NSString *typeEncoding = @(ivar_getTypeEncoding(ivar) ?: "");
  145. if (typeEncoding.flex_typeIsObjectOrClass) {
  146. ptrdiff_t offset = ivar_getOffset(ivar);
  147. uintptr_t *fieldPointer = (__bridge void *)tryObject + offset;
  148. if (*fieldPointer == (uintptr_t)(__bridge void *)object) {
  149. NSString *ivarName = @(ivar_getName(ivar) ?: "???");
  150. [instances addObject:[FLEXObjectRef referencing:tryObject ivar:ivarName]];
  151. return;
  152. }
  153. }
  154. }
  155. tryClass = class_getSuperclass(tryClass);
  156. }
  157. }];
  158. NSArray<NSPredicate *> *predicates = [self defaultPredicates];
  159. NSArray<NSString *> *sectionTitles = [self defaultSectionTitles];
  160. FLEXObjectListViewController *viewController = [[self alloc]
  161. initWithReferences:instances
  162. predicates:predicates
  163. sectionTitles:sectionTitles
  164. ];
  165. viewController.title = [NSString stringWithFormat:@"Referencing %@ %p",
  166. NSStringFromClass(object_getClass(object)), object
  167. ];
  168. return viewController;
  169. }
  170. #pragma mark - Overrides
  171. - (void)viewDidLoad {
  172. [super viewDidLoad];
  173. self.showsSearchBar = YES;
  174. }
  175. - (NSArray<FLEXMutableListSection *> *)makeSections {
  176. if (self.predicates.count) {
  177. return [self buildSections:self.sectionTitles predicates:self.predicates];
  178. } else {
  179. return @[[self makeSection:self.references title:nil]];
  180. }
  181. }
  182. #pragma mark - Private
  183. - (NSArray *)buildSections:(NSArray<NSString *> *)titles predicates:(NSArray<NSPredicate *> *)predicates {
  184. NSParameterAssert(titles.count == predicates.count);
  185. NSParameterAssert(titles); NSParameterAssert(predicates);
  186. return [NSArray flex_forEachUpTo:titles.count map:^id(NSUInteger i) {
  187. NSArray *rows = [self.references filteredArrayUsingPredicate:predicates[i]];
  188. return [self makeSection:rows title:titles[i]];
  189. }];
  190. }
  191. - (FLEXMutableListSection *)makeSection:(NSArray *)rows title:(NSString *)title {
  192. FLEXMutableListSection *section = [FLEXMutableListSection list:rows
  193. cellConfiguration:^(FLEXTableViewCell *cell, FLEXObjectRef *ref, NSInteger row) {
  194. cell.textLabel.text = ref.reference;
  195. cell.detailTextLabel.text = ref.summary;
  196. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  197. } filterMatcher:^BOOL(NSString *filterText, FLEXObjectRef *ref) {
  198. if (ref.summary && [ref.summary localizedCaseInsensitiveContainsString:filterText]) {
  199. return YES;
  200. }
  201. return [ref.reference localizedCaseInsensitiveContainsString:filterText];
  202. }
  203. ];
  204. section.selectionHandler = ^(__kindof UIViewController *host, FLEXObjectRef *ref) {
  205. [self.navigationController pushViewController:[
  206. FLEXObjectExplorerFactory explorerViewControllerForObject:ref.object
  207. ] animated:YES];
  208. };
  209. section.customTitle = title;
  210. return section;
  211. }
  212. @end