NSObject+Reflection.h 11 KB

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