-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathMethodReplace.m
42 lines (31 loc) · 1.04 KB
/
MethodReplace.m
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
38
39
40
41
42
// COMMON FILE: Common
//
// MethodReplace.m
// XcodeColors
//
// Created by Uncle MiF on 9/15/10.
// Copyright 2010 Deep IT. All rights reserved.
//
#import "MethodReplace.h"
IMP ReplaceMethod(t_target target,
Class sourceClass,SEL sourceSel,
Class destinationClass,SEL destinationSel)
{
if (!sourceSel || !sourceClass || !destinationClass)
return nil;
if (!destinationSel)
destinationSel = sourceSel;
Method (*get_targetMethod)(Class,SEL) = target == CLASS_METHOD ? class_getClassMethod : class_getInstanceMethod;
Method sourceMethod = get_targetMethod(sourceClass, sourceSel);
if (!sourceMethod)
return nil;
IMP prevImplementation = method_getImplementation(sourceMethod);
Method destinationMethod = get_targetMethod(destinationClass, destinationSel);
if (!destinationMethod)
return nil;
IMP newImplementation = method_getImplementation(destinationMethod);
if (!newImplementation)
return nil;
method_setImplementation(sourceMethod, newImplementation);
return prevImplementation;
}