FLEXMethod.h 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // FLEXMethod.h
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 6/30/15.
  7. // Copyright (c) 2020 FLEX Team. All rights reserved.
  8. //
  9. #import "FLEXRuntimeConstants.h"
  10. #import "FLEXMethodBase.h"
  11. NS_ASSUME_NONNULL_BEGIN
  12. /// A class representing a concrete method which already exists in a class.
  13. /// This class contains helper methods for swizzling or invoking the method.
  14. ///
  15. /// Any of the initializers will return nil if the type encoding
  16. /// of the method is unsupported by `NSMethodSignature`. In general,
  17. /// any method whose return type or parameters involve a struct with
  18. /// bitfields or arrays is unsupported.
  19. ///
  20. /// I do not remember why I didn't include \c signature in the base class
  21. /// when I originally wrote this, but I probably had a good reason. We can
  22. /// always go back and move it to \c FLEXMethodBase if we find we need to.
  23. @interface FLEXMethod : FLEXMethodBase
  24. /// Defaults to instance method
  25. + (nullable instancetype)method:(Method)method;
  26. + (nullable instancetype)method:(Method)method isInstanceMethod:(BOOL)isInstanceMethod;
  27. /// Constructs an \c FLEXMethod for the given method on the given class.
  28. /// @param cls the class, or metaclass if this is a class method
  29. /// @return The newly constructed \c FLEXMethod object, or \c nil if the
  30. /// specified class or its superclasses do not contain a method with the specified selector.
  31. + (nullable instancetype)selector:(SEL)selector class:(Class)cls;
  32. /// Constructs an \c FLEXMethod for the given method on the given class,
  33. /// only if the given class itself defines or overrides the desired method.
  34. /// @param cls the class, or metaclass if this is a class method
  35. /// @return The newly constructed \c FLEXMethod object, or \c nil \e if the
  36. /// specified class does not define or override, or if the specified class
  37. /// or its superclasses do not contain, a method with the specified selector.
  38. + (nullable instancetype)selector:(SEL)selector implementedInClass:(Class)cls;
  39. @property (nonatomic, readonly) Method objc_method;
  40. /// The implementation of the method.
  41. /// @discussion Setting \c implementation will change the implementation of this method
  42. /// for the entire class which implements said method. It will also not modify the selector of said method.
  43. @property (nonatomic ) IMP implementation;
  44. /// Whether the method is an instance method or not.
  45. @property (nonatomic, readonly) BOOL isInstanceMethod;
  46. /// The number of arguments to the method.
  47. @property (nonatomic, readonly) NSUInteger numberOfArguments;
  48. /// The \c NSMethodSignature object corresponding to the method's type encoding.
  49. @property (nonatomic, readonly) NSMethodSignature *signature;
  50. /// Same as \e typeEncoding but with parameter sizes up front and offsets after the types.
  51. @property (nonatomic, readonly) NSString *signatureString;
  52. /// The return type of the method.
  53. @property (nonatomic, readonly) FLEXTypeEncoding *returnType;
  54. /// The return size of the method.
  55. @property (nonatomic, readonly) NSUInteger returnSize;
  56. /// The full path of the image that contains this method definition,
  57. /// or \c nil if this ivar was probably defined at runtime.
  58. @property (nonatomic, readonly) NSString *imagePath;
  59. /// Like @code - (void)foo:(int)bar @endcode
  60. @property (nonatomic, readonly) NSString *description;
  61. /// Like @code -[Class foo:] @endcode
  62. - (NSString *)debugNameGivenClassName:(NSString *)name;
  63. /// Swizzles the recieving method with the given method.
  64. - (void)swapImplementations:(FLEXMethod *)method;
  65. #define FLEXMagicNumber 0xdeadbeef
  66. #define FLEXArg(expr) FLEXMagicNumber,/// @encode(__typeof__(expr)), (__typeof__(expr) []){ expr }
  67. /// Sends a message to \e target, and returns it's value, or \c nil if not applicable.
  68. /// @discussion You may send any message with this method. Primitive return values will be wrapped
  69. /// in instances of \c NSNumber and \c NSValue. \c void and bitfield returning methods return \c nil.
  70. /// \c SEL return types are converted to strings using \c NSStringFromSelector.
  71. /// @return The object returned by this method, or an instance of \c NSValue or \c NSNumber containing
  72. /// the primitive return type, or a string for \c SEL return types.
  73. - (id)sendMessage:(id)target, ...;
  74. /// Used internally by \c sendMessage:target,. Pass \c NULL to the first parameter for void methods.
  75. - (void)getReturnValue:(void *)retPtr forMessageSend:(id)target, ...;
  76. @end
  77. @interface FLEXMethod (Comparison)
  78. - (NSComparisonResult)compare:(FLEXMethod *)method;
  79. @end
  80. NS_ASSUME_NONNULL_END