NSObject+Reflection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // NSObject+Reflection.h
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 6/30/15.
  7. // Copyright (c) 2015 Tanner Bennett. All rights reserved.
  8. //
  9. #import <Foundation/Foundation.h>
  10. #import <objc/runtime.h>
  11. @class FLEXMirror, FLEXMethod, FLEXIvar, FLEXProperty, FLEXMethodBase, FLEXPropertyAttributes, FLEXProtocol;
  12. NS_ASSUME_NONNULL_BEGIN
  13. /// Returns the type encoding string given the encoding for the return type and parameters, if any.
  14. /// @discussion Example usage for a \c void returning method which takes
  15. /// an \c int: @code FLEXTypeEncoding(@encode(void), @encode(int));
  16. /// @param returnType The encoded return type. \c void for exmaple would be \c @encode(void).
  17. /// @param count The number of parameters in this type encoding string.
  18. /// @return The type encoding string, or \c nil if \e returnType is \c NULL.
  19. NSString * FLEXTypeEncodingString(const char *returnType, NSUInteger count, ...);
  20. NSArray<Class> *FLEXGetAllSubclasses(_Nullable Class cls, BOOL includeSelf);
  21. NSArray<Class> *FLEXGetClassHierarchy(_Nullable Class cls, BOOL includeSelf);
  22. NSArray<FLEXProtocol *> *FLEXGetConformedProtocols(_Nullable Class cls);
  23. NSArray<FLEXIvar *> *FLEXGetAllIvars(_Nullable Class cls);
  24. /// @param cls a class object to get instance properties,
  25. /// or a metaclass object to get class properties
  26. NSArray<FLEXProperty *> *FLEXGetAllProperties(_Nullable Class cls);
  27. /// @param cls a class object to get instance methods,
  28. /// or a metaclass object to get class methods
  29. /// @param instance used to mark methods as instance methods or not.
  30. /// Not used to determine whether to get instance or class methods.
  31. NSArray<FLEXMethod *> *FLEXGetAllMethods(_Nullable Class cls, BOOL instance);
  32. #pragma mark Reflection
  33. @interface NSObject (Reflection)
  34. @property (nonatomic, readonly ) FLEXMirror *flex_reflection;
  35. @property (nonatomic, readonly, class) FLEXMirror *flex_reflection;
  36. /// Calls into /c FLEXGetAllSubclasses
  37. /// @return Every subclass of the receiving class, including the receiver itself.
  38. @property (nonatomic, readonly, class) NSArray<Class> *flex_allSubclasses;
  39. /// @return The \c Class object for the metaclass of the recieving class, or \c Nil if the class is Nil or not registered.
  40. @property (nonatomic, readonly, class) Class flex_metaclass;
  41. /// @return The size in bytes of instances of the recieving class, or \c 0 if \e cls is \c Nil.
  42. @property (nonatomic, readonly, class) size_t flex_instanceSize;
  43. /// Changes the class of an object instance.
  44. /// @return The previous value of the objects \c class, or \c Nil if the object is \c nil.
  45. - (Class)flex_setClass:(Class)cls;
  46. /// Sets the recieving class's superclass. "You should not use this method" — Apple.
  47. /// @return The old superclass.
  48. + (Class)flex_setSuperclass:(Class)superclass;
  49. /// Calls into \c FLEXGetClassHierarchy()
  50. /// @return a list of classes going up the class hierarchy,
  51. /// starting with the receiver and ending with the root class.
  52. @property (nonatomic, readonly, class) NSArray<Class> *flex_classHierarchy;
  53. /// Calls into \c FLEXGetConformedProtocols
  54. /// @return a list of protocols this class itself conforms to.
  55. @property (nonatomic, readonly, class) NSArray<FLEXProtocol *> *flex_protocols;
  56. @end
  57. #pragma mark Methods
  58. @interface NSObject (Methods)
  59. /// All instance and class methods specific to the recieving class.
  60. /// @discussion This method will only retrieve methods specific to the recieving class.
  61. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  62. /// @return An array of \c FLEXMethod objects.
  63. @property (nonatomic, readonly, class) NSArray<FLEXMethod *> *flex_allMethods;
  64. /// All instance methods specific to the recieving class.
  65. /// @discussion This method will only retrieve methods specific to the recieving class.
  66. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  67. /// @return An array of \c FLEXMethod objects.
  68. @property (nonatomic, readonly, class) NSArray<FLEXMethod *> *flex_allInstanceMethods;
  69. /// All class methods specific to the recieving class.
  70. /// @discussion This method will only retrieve methods specific to the recieving class.
  71. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  72. /// @return An array of \c FLEXMethod objects.
  73. @property (nonatomic, readonly, class) NSArray<FLEXMethod *> *flex_allClassMethods;
  74. /// Retrieves the class's instance method with the given name.
  75. /// @return An initialized \c FLEXMethod object, or \c nil if the method wasn't found.
  76. + (FLEXMethod *)flex_methodNamed:(NSString *)name;
  77. /// Retrieves the class's class method with the given name.
  78. /// @return An initialized \c FLEXMethod object, or \c nil if the method wasn't found.
  79. + (FLEXMethod *)flex_classMethodNamed:(NSString *)name;
  80. /// Adds a new method to the recieving class with a given name and implementation.
  81. /// @discussion This method will add an override of a superclass's implementation,
  82. /// but will not replace an existing implementation in the class.
  83. /// To change an existing implementation, use \c replaceImplementationOfMethod:with:.
  84. ///
  85. /// Type encodings start with the return type and end with the parameter types in order.
  86. /// The type encoding for \c NSArray's \c count property getter looks like this:
  87. /// @code [NSString stringWithFormat:@"%s%s%s%s", @encode(void), @encode(id), @encode(SEL), @encode(NSUInteger)] @endcode
  88. /// Using the \c FLEXTypeEncoding function for the same method looks like this:
  89. /// @code FLEXTypeEncodingString(@encode(void), 1, @encode(NSUInteger)) @endcode
  90. /// @param typeEncoding The type encoding string. Consider using the \c FLEXTypeEncodingString() function.
  91. /// @param instanceMethod NO to add the method to the class itself or YES to add it as an instance method.
  92. /// @return YES if the method was added successfully, \c NO otherwise
  93. /// (for example, the class already contains a method implementation with that name).
  94. + (BOOL)addMethod:(SEL)selector
  95. typeEncoding:(NSString *)typeEncoding
  96. implementation:(IMP)implementaiton
  97. toInstances:(BOOL)instanceMethod;
  98. /// Replaces the implementation of a method in the recieving class.
  99. /// @param instanceMethod YES to replace the instance method, NO to replace the class method.
  100. /// @note This function behaves in two different ways:
  101. ///
  102. /// - If the method does not yet exist in the recieving class, it is added as if
  103. /// \c addMethod:typeEncoding:implementation were called.
  104. ///
  105. /// - If the method does exist, its \c IMP is replaced.
  106. /// @return The previous \c IMP of \e method.
  107. + (IMP)replaceImplementationOfMethod:(FLEXMethodBase *)method with:(IMP)implementation useInstance:(BOOL)instanceMethod;
  108. /// Swaps the implementations of the given methods.
  109. /// @discussion If one or neither of the given methods exist in the recieving class,
  110. /// they are added to the class with their implementations swapped as if each method did exist.
  111. /// This method will not fail if each \c FLEXSimpleMethod contains a valid selector.
  112. /// @param instanceMethod YES to swizzle the instance method, NO to swizzle the class method.
  113. + (void)swizzle:(FLEXMethodBase *)original with:(FLEXMethodBase *)other onInstance:(BOOL)instanceMethod;
  114. /// Swaps the implementations of the given methods.
  115. /// @param instanceMethod YES to swizzle the instance method, NO to swizzle the class method.
  116. /// @return \c YES if successful, and \c NO if selectors could not be retrieved from the given strings.
  117. + (BOOL)swizzleByName:(NSString *)original with:(NSString *)other onInstance:(BOOL)instanceMethod;
  118. /// Swaps the implementations of methods corresponding to the given selectors.
  119. + (void)swizzleBySelector:(SEL)original with:(SEL)other onInstance:(BOOL)instanceMethod;
  120. @end
  121. #pragma mark Properties
  122. @interface NSObject (Ivars)
  123. /// All of the instance variables specific to the recieving class.
  124. /// @discussion This method will only retrieve instance varibles specific to the recieving class.
  125. /// To retrieve instance variables on a parent class, simply call \c [[self superclass] allIvars].
  126. /// @return An array of \c FLEXIvar objects.
  127. @property (nonatomic, readonly, class) NSArray<FLEXIvar *> *flex_allIvars;
  128. /// Retrieves an instance variable with the corresponding name.
  129. /// @return An initialized \c FLEXIvar object, or \c nil if the Ivar wasn't found.
  130. + (FLEXIvar *)flex_ivarNamed:(NSString *)name;
  131. /// @return The address of the given ivar in the recieving object in memory,
  132. /// or \c NULL if it could not be found.
  133. - (void *)flex_getIvarAddress:(FLEXIvar *)ivar;
  134. /// @return The address of the given ivar in the recieving object in memory,
  135. /// or \c NULL if it could not be found.
  136. - (void *)flex_getIvarAddressByName:(NSString *)name;
  137. /// @discussion This method faster than creating an \c FLEXIvar and calling
  138. /// \c -getIvarAddress: if you already have an \c Ivar on hand
  139. /// @return The address of the given ivar in the recieving object in memory,
  140. /// or \c NULL if it could not be found\.
  141. - (void *)flex_getObjcIvarAddress:(Ivar)ivar;
  142. /// Sets the value of the given instance variable on the recieving object.
  143. /// @discussion Use only when the target instance variable is an object.
  144. - (void)flex_setIvar:(FLEXIvar *)ivar object:(id)value;
  145. /// Sets the value of the given instance variable on the recieving object.
  146. /// @discussion Use only when the target instance variable is an object.
  147. /// @return \c YES if successful, or \c NO if the instance variable could not be found.
  148. - (BOOL)flex_setIvarByName:(NSString *)name object:(id)value;
  149. /// @discussion Use only when the target instance variable is an object.
  150. /// This method is faster than creating an \c FLEXIvar and calling
  151. /// \c -setIvar: if you already have an \c Ivar on hand.
  152. - (void)flex_setObjcIvar:(Ivar)ivar object:(id)value;
  153. /// Sets the value of the given instance variable on the recieving object to the
  154. /// \e size number of bytes of data at \e value.
  155. /// @discussion Use one of the other methods if you can help it.
  156. - (void)flex_setIvar:(FLEXIvar *)ivar value:(void *)value size:(size_t)size;
  157. /// Sets the value of the given instance variable on the recieving object to the
  158. /// \e size number of bytes of data at \e value.
  159. /// @discussion Use one of the other methods if you can help it
  160. /// @return \c YES if successful, or \c NO if the instance variable could not be found.
  161. - (BOOL)flex_setIvarByName:(NSString *)name value:(void *)value size:(size_t)size;
  162. /// Sets the value of the given instance variable on the recieving object to the
  163. /// \e size number of bytes of data at \e value.
  164. /// @discussion This is faster than creating an \c FLEXIvar and calling
  165. /// \c -setIvar:value:size if you already have an \c Ivar on hand.
  166. - (void)flex_setObjcIvar:(Ivar)ivar value:(void *)value size:(size_t)size;
  167. @end
  168. #pragma mark Properties
  169. @interface NSObject (Properties)
  170. /// All instance and class properties specific to the recieving class.
  171. /// @discussion This method will only retrieve properties specific to the recieving class.
  172. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  173. /// @return An array of \c FLEXProperty objects.
  174. @property (nonatomic, readonly, class) NSArray<FLEXProperty *> *flex_allProperties;
  175. /// All instance properties specific to the recieving class.
  176. /// @discussion This method will only retrieve properties specific to the recieving class.
  177. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  178. /// @return An array of \c FLEXProperty objects.
  179. @property (nonatomic, readonly, class) NSArray<FLEXProperty *> *flex_allInstanceProperties;
  180. /// All class properties specific to the recieving class.
  181. /// @discussion This method will only retrieve properties specific to the recieving class.
  182. /// To retrieve instance variables on a parent class, simply call this on \c [self superclass].
  183. /// @return An array of \c FLEXProperty objects.
  184. @property (nonatomic, readonly, class) NSArray<FLEXProperty *> *flex_allClassProperties;
  185. /// Retrieves the class's property with the given name.
  186. /// @return An initialized \c FLEXProperty object, or \c nil if the property wasn't found.
  187. + (FLEXProperty *)flex_propertyNamed:(NSString *)name;
  188. /// @return An initialized \c FLEXProperty object, or \c nil if the property wasn't found.
  189. + (FLEXProperty *)flex_classPropertyNamed:(NSString *)name;
  190. /// Replaces the given property on the recieving class.
  191. + (void)flex_replaceProperty:(FLEXProperty *)property;
  192. /// Replaces the given property on the recieving class. Useful for changing a property's attributes.
  193. + (void)flex_replaceProperty:(NSString *)name attributes:(FLEXPropertyAttributes *)attributes;
  194. @end
  195. NS_ASSUME_NONNULL_END