-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathMethodReplace.h
37 lines (30 loc) · 1.37 KB
/
MethodReplace.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// COMMON FILE: Common
//
// MethodReplace.h
// XcodeColors
//
// Created by Uncle MiF on 9/15/10.
// Copyright 2010 Deep IT. All rights reserved.
//
/* The Black Magic begins here */
#import <objc/runtime.h>
typedef enum { CLASS_METHOD, INSTANCE_METHOD} t_target;
#define ReplaceClassMethod(sourceClassName,sourceSelName,destinationClassName) ReplaceMethod(CLASS_METHOD, [sourceClassName class], @selector(sourceSelName), [destinationClassName class], 0)
#define ReplaceInstanceMethod(sourceClassName,sourceSelName,destinationClassName) ReplaceMethod(INSTANCE_METHOD, [sourceClassName class], @selector(sourceSelName), [destinationClassName class], 0)
#define DynamicMessage(targetMode,targetClassName,instance,targetSelName,...) \
do\
{\
Method (*get_targetMethod)(Class,SEL) = targetMode == CLASS_METHOD ? class_getClassMethod : class_getInstanceMethod;\
Method method = get_targetMethod([targetClassName class],@selector(targetSelName));\
if (method)\
{\
IMP imp = method_getImplementation(method);\
if (imp)\
imp(instance,@selector(targetSelName),##__VA_ARGS__);\
}\
} while(0)
#define DynamicClassMessage(...) DynamicMessage(CLASS_METHOD,##__VA_ARGS__)
#define DynamicInstanceMessage(...) DynamicMessage(INSTANCE_METHOD,##__VA_ARGS__)
IMP ReplaceMethod(t_target target,
Class sourceClass,SEL sourceSel,
Class destinationClass,SEL destinationSel);