NSObject+Reflection.h 11 KB

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