FIRObjectSwizzler.mm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2017 Google
  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 "Private/FIRObjectSwizzler.h"
  15. #import <objc/runtime.h>
  16. #import "Private/FIRSwizzledObject.h"
  17. #import "Private/FIRSwizzlingCaches.h"
  18. #import "Private/FIRSwizzler.h"
  19. @implementation FIRObjectSwizzler {
  20. // The swizzled object.
  21. __weak id _swizzledObject;
  22. // The original class of the object.
  23. Class _originalClass;
  24. // The dynamically generated subclass of _originalClass.
  25. Class _generatedClass;
  26. }
  27. #pragma mark - Class methods
  28. + (void)setAssociatedObject:(id)object
  29. key:(NSString *)key
  30. value:(nullable id)value
  31. association:(FIR_ASSOCIATION)association {
  32. objc_AssociationPolicy resolvedAssociation;
  33. switch (association) {
  34. case FIR_ASSOCIATION_ASSIGN:
  35. resolvedAssociation = OBJC_ASSOCIATION_ASSIGN;
  36. break;
  37. case FIR_ASSOCIATION_RETAIN_NONATOMIC:
  38. resolvedAssociation = OBJC_ASSOCIATION_RETAIN_NONATOMIC;
  39. break;
  40. case FIR_ASSOCIATION_COPY_NONATOMIC:
  41. resolvedAssociation = OBJC_ASSOCIATION_COPY_NONATOMIC;
  42. break;
  43. case FIR_ASSOCIATION_RETAIN:
  44. resolvedAssociation = OBJC_ASSOCIATION_RETAIN;
  45. break;
  46. case FIR_ASSOCIATION_COPY:
  47. resolvedAssociation = OBJC_ASSOCIATION_COPY;
  48. break;
  49. default:
  50. break;
  51. }
  52. objc_setAssociatedObject(object, key.UTF8String, value, resolvedAssociation);
  53. }
  54. + (nullable id)getAssociatedObject:(id)object key:(NSString *)key {
  55. return objc_getAssociatedObject(object, key.UTF8String);
  56. }
  57. #pragma mark - Instance methods
  58. /** Instantiates an instance of this class.
  59. *
  60. * @param object The object to swizzle.
  61. * @return An instance of this class.
  62. */
  63. - (instancetype)initWithObject:(id)object {
  64. self = [super init];
  65. if (self) {
  66. __strong id swizzledObject = object;
  67. if (swizzledObject) {
  68. _swizzledObject = swizzledObject;
  69. _originalClass = [swizzledObject class];
  70. NSString *newClassName = [NSString stringWithFormat:@"fpr_%p_%@",
  71. swizzledObject,
  72. NSStringFromClass(_originalClass)];
  73. _generatedClass = objc_allocateClassPair(_originalClass, newClassName.UTF8String, 0);
  74. NSAssert(_generatedClass, @"Wasn't able to allocate the class pair.");
  75. } else {
  76. return nil;
  77. }
  78. }
  79. return self;
  80. }
  81. - (void)copySelector:(SEL)selector
  82. fromClass:(Class)aClass
  83. isClassSelector:(BOOL)isClassSelector {
  84. NSAssert(_generatedClass, @"This object has already been unswizzled.");
  85. Method method = isClassSelector ? class_getClassMethod(aClass, selector) :
  86. class_getInstanceMethod(aClass, selector);
  87. Class targetClass = isClassSelector ? object_getClass(_generatedClass) : _generatedClass;
  88. IMP implementation = method_getImplementation(method);
  89. const char *typeEncoding = method_getTypeEncoding(method);
  90. BOOL success = class_addMethod(targetClass, selector, implementation, typeEncoding);
  91. NSAssert(success,
  92. @"Unable to add selector %@ to class %@",
  93. NSStringFromSelector(selector),
  94. NSStringFromClass(targetClass));
  95. }
  96. - (void)setAssociatedObjectWithKey:(NSString *)key
  97. value:(id)value
  98. association:(FIR_ASSOCIATION)association {
  99. __strong id swizzledObject = _swizzledObject;
  100. if (swizzledObject) {
  101. [[self class] setAssociatedObject:swizzledObject
  102. key:key
  103. value:value
  104. association:association];
  105. }
  106. }
  107. - (nullable id)getAssociatedObjectForKey:(NSString *)key {
  108. __strong id swizzledObject = _swizzledObject;
  109. if (swizzledObject) {
  110. return [[self class] getAssociatedObject:swizzledObject key:key];
  111. }
  112. return nil;
  113. }
  114. - (void)swizzle {
  115. __strong id swizzledObject = _swizzledObject;
  116. if (swizzledObject) {
  117. [FIRObjectSwizzler setAssociatedObject:swizzledObject
  118. key:kSwizzlerAssociatedObjectKey
  119. value:self
  120. association:FIR_ASSOCIATION_RETAIN_NONATOMIC];
  121. [FIRSwizzledObject copyDonorSelectorsUsingObjectSwizzler:self];
  122. NSAssert(_originalClass == [_swizzledObject class],
  123. @"The original class is not the reported class now.");
  124. NSAssert(class_getInstanceSize(_originalClass) == class_getInstanceSize(_generatedClass),
  125. @"The instance size of the generated class must be equal to the original class.");
  126. objc_registerClassPair(_generatedClass);
  127. Class doubleCheckOriginalClass = object_setClass(_swizzledObject, _generatedClass);
  128. NSAssert(_originalClass == doubleCheckOriginalClass,
  129. @"The original class must be the same as the class returned by object_setClass");
  130. } else {
  131. NSAssert(NO, @"You can't swizzle a nil object");
  132. }
  133. }
  134. - (void)dealloc {
  135. objc_disposeClassPair(_generatedClass);
  136. }
  137. @end