GULSwizzler.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2018 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. /** This class handles the runtime manipulation necessary to instrument selectors. It stores the
  19. * classes and selectors that have been swizzled, and runs all operations on its own queue.
  20. */
  21. @interface GULSwizzler : NSObject
  22. /** Manipulates the Objective-C runtime to replace the original IMP with the supplied block.
  23. *
  24. * @param aClass The class to swizzle.
  25. * @param selector The selector of the class to swizzle.
  26. * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
  27. * @param block The block that replaces the original IMP.
  28. */
  29. + (void)swizzleClass:(Class)aClass
  30. selector:(SEL)selector
  31. isClassSelector:(BOOL)isClassSelector
  32. withBlock:(nullable id)block;
  33. /** Returns the current IMP for the given class and selector.
  34. *
  35. * @param aClass The class to use.
  36. * @param selector The selector to find the implementation of.
  37. * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
  38. * @return The implementation of the selector in the runtime.
  39. */
  40. + (nullable IMP)currentImplementationForClass:(Class)aClass
  41. selector:(SEL)selector
  42. isClassSelector:(BOOL)isClassSelector;
  43. /** Checks the runtime to see if a selector exists on a class. If a property is declared as
  44. * @dynamic, we have a reverse swizzling situation, where the implementation of a method exists
  45. * only in concrete subclasses, and NOT in the superclass. We can detect that situation using
  46. * this helper method. Similarly, we can detect situations where a class doesn't implement a
  47. * protocol method.
  48. *
  49. * @param selector The selector to check for.
  50. * @param aClass The class to check.
  51. * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
  52. * @return YES if the method was found in this selector/class combination, NO otherwise.
  53. */
  54. + (BOOL)selector:(SEL)selector existsInClass:(Class)aClass isClassSelector:(BOOL)isClassSelector;
  55. /** Returns a list of all Objective-C (and not primitive) ivars contained by the given object.
  56. *
  57. * @param object The object whose ivars will be iterated.
  58. * @return The list of ivar objects.
  59. */
  60. + (NSArray<id> *)ivarObjectsForObject:(id)object;
  61. @end
  62. NS_ASSUME_NONNULL_END