FPRSwizzledObject.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "FirebasePerformance/Sources/ISASwizzler/FPRObjectSwizzler+Internal.h"
  16. #import "FirebasePerformance/Sources/ISASwizzler/FPRSwizzledObject.h"
  17. const NSString *const kGULSwizzlerAssociatedObjectKey = @"gul_objectSwizzler";
  18. @interface FPRSwizzledObject ()
  19. @end
  20. @implementation FPRSwizzledObject
  21. + (void)copyDonorSelectorsUsingObjectSwizzler:(FPRObjectSwizzler *)objectSwizzler {
  22. [objectSwizzler copySelector:@selector(gul_objectSwizzler) fromClass:self isClassSelector:NO];
  23. [objectSwizzler copySelector:@selector(gul_class) fromClass:self isClassSelector:NO];
  24. // This is needed because NSProxy objects usually override -[NSObjectProtocol respondsToSelector:]
  25. // and ask this question to the underlying object. Since we don't swizzle the underlying object
  26. // but swizzle the proxy, when someone calls -[NSObjectProtocol respondsToSelector:] on the proxy,
  27. // the answer ends up being NO even if we added new methods to the subclass through ISA Swizzling.
  28. // To solve that, we override -[NSObjectProtocol respondsToSelector:] in such a way that takes
  29. // into account the fact that we've added new methods.
  30. if ([objectSwizzler isSwizzlingProxyObject]) {
  31. [objectSwizzler copySelector:@selector(respondsToSelector:) fromClass:self isClassSelector:NO];
  32. }
  33. }
  34. - (instancetype)init {
  35. NSAssert(NO, @"Do not instantiate this class, it's only a donor class");
  36. return nil;
  37. }
  38. - (FPRObjectSwizzler *)gul_objectSwizzler {
  39. return [FPRObjectSwizzler getAssociatedObject:self key:&kGULSwizzlerAssociatedObjectKey];
  40. }
  41. #pragma mark - Donor methods
  42. - (Class)gul_class {
  43. return [[self gul_objectSwizzler] generatedClass];
  44. }
  45. // Only added to a class when we detect it is a proxy.
  46. - (BOOL)respondsToSelector:(SEL)aSelector {
  47. Class gulClass = [[self gul_objectSwizzler] generatedClass];
  48. return [gulClass instancesRespondToSelector:aSelector] || [super respondsToSelector:aSelector];
  49. }
  50. @end