FPRObjectSwizzler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /** Enums that map to their OBJC-prefixed counterparts. */
  19. typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
  20. // Is a weak association.
  21. GUL_ASSOCIATION_ASSIGN,
  22. // Is a nonatomic strong association.
  23. GUL_ASSOCIATION_RETAIN_NONATOMIC,
  24. // Is a nonatomic copy association.
  25. GUL_ASSOCIATION_COPY_NONATOMIC,
  26. // Is an atomic strong association.
  27. GUL_ASSOCIATION_RETAIN,
  28. // Is an atomic copy association.
  29. GUL_ASSOCIATION_COPY};
  30. /** This class handles swizzling a specific instance of a class by generating a
  31. * dynamic subclass and installing selectors and properties onto the dynamic
  32. * subclass. Then, the instance's class is set to the dynamic subclass. There
  33. * should be a 1:1 ratio of object swizzlers to swizzled instances.
  34. */
  35. @interface FPRObjectSwizzler : NSObject
  36. /** The subclass that is generated. */
  37. @property(nullable, nonatomic, readonly) Class generatedClass;
  38. /** Sets an associated object in the runtime. This mechanism can be used to
  39. * simulate adding properties.
  40. *
  41. * @param object The object that will be queried for the associated object.
  42. * @param key The key of the associated object.
  43. * @param value The value to associate to the swizzled object.
  44. * @param association The mechanism to use when associating the objects.
  45. */
  46. + (void)setAssociatedObject:(id)object
  47. key:(const void *)key
  48. value:(nullable id)value
  49. association:(GUL_ASSOCIATION)association;
  50. /** Gets an associated object in the runtime. This mechanism can be used to
  51. * simulate adding properties.
  52. *
  53. * @param object The object that will be queried for the associated object.
  54. * @param key The key of the associated object.
  55. */
  56. + (nullable id)getAssociatedObject:(id)object key:(const void *)key;
  57. /** Please use the designated initializer. */
  58. - (instancetype)init NS_UNAVAILABLE;
  59. /** Instantiates an object swizzler using an object it will operate on.
  60. * Generates a new class pair.
  61. *
  62. * @note There is no need to store this object. After calling -swizzle, this
  63. * object can be found by calling -gul_objectSwizzler
  64. *
  65. * @param object The object to be swizzled.
  66. * @return An instance of this class.
  67. */
  68. - (instancetype)initWithObject:(id)object NS_DESIGNATED_INITIALIZER;
  69. /** Sets an associated object in the runtime. This mechanism can be used to
  70. * simulate adding properties.
  71. *
  72. * @param key The key of the associated object.
  73. * @param value The value to associate to the swizzled object.
  74. * @param association The mechanism to use when associating the objects.
  75. */
  76. - (void)setAssociatedObjectWithKey:(const void *)key
  77. value:(id)value
  78. association:(GUL_ASSOCIATION)association;
  79. /** Gets an associated object in the runtime. This mechanism can be used to
  80. * simulate adding properties.
  81. *
  82. * @param key The key of the associated object.
  83. */
  84. - (nullable id)getAssociatedObjectForKey:(const void *)key;
  85. /** Copies a selector from an existing class onto the generated dynamic subclass
  86. * that this object will adopt. This mechanism can be used to add methods to
  87. * specific instances of a class.
  88. *
  89. * @note Should not be called after calling -swizzle.
  90. * @param selector The selector to add to the instance.
  91. * @param aClass The class supplying an implementation of the method.
  92. * @param isClassSelector A BOOL specifying whether the selector is a class or
  93. * instance selector.
  94. */
  95. - (void)copySelector:(SEL)selector fromClass:(Class)aClass isClassSelector:(BOOL)isClassSelector;
  96. /** Swizzles the object, changing its class to the generated class. Registers
  97. * the class pair. */
  98. - (void)swizzle;
  99. /** @return The value of -[objectBeingSwizzled isProxy] */
  100. - (BOOL)isSwizzlingProxyObject;
  101. @end
  102. NS_ASSUME_NONNULL_END