GULSwizzler.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleUtilities/MethodSwizzler/Private/GULSwizzler.h"
  15. #import <objc/runtime.h>
  16. #ifdef DEBUG
  17. #import <GoogleUtilities/GULLogger.h>
  18. #import "GoogleUtilities/Common/GULLoggerCodes.h"
  19. static GULLoggerService kGULLoggerSwizzler = @"[GoogleUtilities/MethodSwizzler]";
  20. #endif
  21. dispatch_queue_t GetGULSwizzlingQueue(void) {
  22. static dispatch_queue_t queue;
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. queue = dispatch_queue_create("com.google.GULSwizzler", DISPATCH_QUEUE_SERIAL);
  26. });
  27. return queue;
  28. }
  29. @implementation GULSwizzler
  30. + (void)swizzleClass:(Class)aClass
  31. selector:(SEL)selector
  32. isClassSelector:(BOOL)isClassSelector
  33. withBlock:(nullable id)block {
  34. dispatch_sync(GetGULSwizzlingQueue(), ^{
  35. NSAssert(selector, @"The selector cannot be NULL");
  36. NSAssert(aClass, @"The class cannot be Nil");
  37. Class resolvedClass = aClass;
  38. Method method = nil;
  39. if (isClassSelector) {
  40. method = class_getClassMethod(aClass, selector);
  41. resolvedClass = object_getClass(aClass);
  42. } else {
  43. method = class_getInstanceMethod(aClass, selector);
  44. }
  45. NSAssert(method, @"You're attempting to swizzle a method that doesn't exist. (%@, %@)",
  46. NSStringFromClass(resolvedClass), NSStringFromSelector(selector));
  47. IMP newImp = imp_implementationWithBlock(block);
  48. #ifdef DEBUG
  49. IMP currentImp = class_getMethodImplementation(resolvedClass, selector);
  50. Class class = NSClassFromString(@"GULSwizzlingCache");
  51. if (class) {
  52. SEL cacheSelector = NSSelectorFromString(@"cacheCurrentIMP:forNewIMP:forClass:withSelector:");
  53. NSMethodSignature *methodSignature = [class methodSignatureForSelector:cacheSelector];
  54. if (methodSignature != nil) {
  55. NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
  56. [inv setSelector:cacheSelector];
  57. [inv setTarget:class];
  58. [inv setArgument:&(currentImp) atIndex:2];
  59. [inv setArgument:&(newImp) atIndex:3];
  60. [inv setArgument:&(resolvedClass) atIndex:4];
  61. [inv setArgument:(void *_Nonnull) & (selector) atIndex:5];
  62. [inv invoke];
  63. }
  64. }
  65. #endif
  66. const char *typeEncoding = method_getTypeEncoding(method);
  67. __unused IMP originalImpOfClass =
  68. class_replaceMethod(resolvedClass, selector, newImp, typeEncoding);
  69. #ifdef DEBUG
  70. // If !originalImpOfClass, then the IMP came from a superclass.
  71. if (originalImpOfClass) {
  72. SEL selector = NSSelectorFromString(@"originalIMPOfCurrentIMP:");
  73. NSMethodSignature *methodSignature = [class methodSignatureForSelector:selector];
  74. if (methodSignature != nil) {
  75. NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
  76. [inv setSelector:selector];
  77. [inv setTarget:class];
  78. [inv setArgument:&(currentImp) atIndex:2];
  79. [inv invoke];
  80. IMP testOriginal;
  81. [inv getReturnValue:&testOriginal];
  82. if (originalImpOfClass != testOriginal) {
  83. GULLogWarning(kGULLoggerSwizzler, NO,
  84. [NSString stringWithFormat:@"I-SWZ%06ld",
  85. (long)kGULSwizzlerMessageCodeMethodSwizzling000],
  86. @"Swizzling class: %@ SEL:%@ after it has been previously been swizzled.",
  87. NSStringFromClass(resolvedClass), NSStringFromSelector(selector));
  88. }
  89. }
  90. }
  91. #endif
  92. });
  93. }
  94. + (nullable IMP)currentImplementationForClass:(Class)aClass
  95. selector:(SEL)selector
  96. isClassSelector:(BOOL)isClassSelector {
  97. NSAssert(selector, @"The selector cannot be NULL");
  98. NSAssert(aClass, @"The class cannot be Nil");
  99. if (selector == NULL || aClass == nil) {
  100. return nil;
  101. }
  102. __block IMP currentIMP = nil;
  103. dispatch_sync(GetGULSwizzlingQueue(), ^{
  104. Method method = nil;
  105. if (isClassSelector) {
  106. method = class_getClassMethod(aClass, selector);
  107. } else {
  108. method = class_getInstanceMethod(aClass, selector);
  109. }
  110. NSAssert(method, @"The Method for this class/selector combo doesn't exist (%@, %@).",
  111. NSStringFromClass(aClass), NSStringFromSelector(selector));
  112. if (method == nil) {
  113. return;
  114. }
  115. currentIMP = method_getImplementation(method);
  116. NSAssert(currentIMP, @"The IMP for this class/selector combo doesn't exist (%@, %@).",
  117. NSStringFromClass(aClass), NSStringFromSelector(selector));
  118. });
  119. return currentIMP;
  120. }
  121. + (BOOL)selector:(SEL)selector existsInClass:(Class)aClass isClassSelector:(BOOL)isClassSelector {
  122. Method method = isClassSelector ? class_getClassMethod(aClass, selector)
  123. : class_getInstanceMethod(aClass, selector);
  124. return method != nil;
  125. }
  126. + (NSArray<id> *)ivarObjectsForObject:(id)object {
  127. NSMutableArray *array = [NSMutableArray array];
  128. unsigned int count;
  129. Ivar *vars = class_copyIvarList([object class], &count);
  130. for (NSUInteger i = 0; i < count; i++) {
  131. const char *typeEncoding = ivar_getTypeEncoding(vars[i]);
  132. // Check to see if the ivar is an object.
  133. if (strncmp(typeEncoding, "@", 1) == 0) {
  134. id ivarObject = object_getIvar(object, vars[i]);
  135. [array addObject:ivarObject];
  136. }
  137. }
  138. free(vars);
  139. return array;
  140. }
  141. @end