GULSwizzledObject.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <objc/runtime.h>
  15. #import "GoogleUtilities/ISASwizzler/GULObjectSwizzler+Internal.h"
  16. #import "GoogleUtilities/ISASwizzler/Private/GULSwizzledObject.h"
  17. NSString *kSwizzlerAssociatedObjectKey = @"gul_objectSwizzler";
  18. @interface GULSwizzledObject ()
  19. @end
  20. @implementation GULSwizzledObject
  21. + (void)copyDonorSelectorsUsingObjectSwizzler:(GULObjectSwizzler *)objectSwizzler {
  22. [objectSwizzler copySelector:@selector(gul_objectSwizzler) fromClass:self isClassSelector:NO];
  23. [objectSwizzler copySelector:@selector(gul_class) fromClass:self isClassSelector:NO];
  24. [objectSwizzler copySelector:@selector(dealloc) fromClass:self isClassSelector:NO];
  25. // This is needed because NSProxy objects usually override -[NSObjectProtocol respondsToSelector:]
  26. // and ask this question to the underlying object. Since we don't swizzle the underlying object
  27. // but swizzle the proxy, when someone calls -[NSObjectProtocol respondsToSelector:] on the proxy,
  28. // the answer ends up being NO even if we added new methods to the subclass through ISA Swizzling.
  29. // To solve that, we override -[NSObjectProtocol respondsToSelector:] in such a way that takes
  30. // into account the fact that we've added new methods.
  31. if ([objectSwizzler isSwizzlingProxyObject]) {
  32. [objectSwizzler copySelector:@selector(respondsToSelector:) fromClass:self isClassSelector:NO];
  33. }
  34. }
  35. - (instancetype)init {
  36. NSAssert(NO, @"Do not instantiate this class, it's only a donor class");
  37. return nil;
  38. }
  39. - (GULObjectSwizzler *)gul_objectSwizzler {
  40. return [GULObjectSwizzler getAssociatedObject:self key:kSwizzlerAssociatedObjectKey];
  41. }
  42. #pragma mark - Donor methods
  43. - (Class)gul_class {
  44. return [[self gul_objectSwizzler] generatedClass];
  45. }
  46. // Only added to a class when we detect it is a proxy.
  47. - (BOOL)respondsToSelector:(SEL)aSelector {
  48. Class gulClass = [[self gul_objectSwizzler] generatedClass];
  49. return [gulClass instancesRespondToSelector:aSelector] || [super respondsToSelector:aSelector];
  50. }
  51. - (void)dealloc {
  52. // We need to make sure the swizzler is deallocated after the swizzled object to do the clean up
  53. // only when the swizzled object is not used.
  54. GULObjectSwizzler *swizzler = nil;
  55. BOOL isInstanceOfGeneratedClass = NO;
  56. @autoreleasepool {
  57. Class generatedClass = [self gul_class];
  58. isInstanceOfGeneratedClass = object_getClass(self) == generatedClass;
  59. swizzler = [[self gul_objectSwizzler] retain];
  60. [GULObjectSwizzler setAssociatedObject:self
  61. key:kSwizzlerAssociatedObjectKey
  62. value:nil
  63. association:GUL_ASSOCIATION_RETAIN_NONATOMIC];
  64. }
  65. [super dealloc];
  66. [swizzler swizzledObjectHasBeenDeallocatedWithGeneratedSubclass:isInstanceOfGeneratedClass];
  67. [swizzler release];
  68. }
  69. @end