32 lines
1018 B
Objective-C
32 lines
1018 B
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "FBReflectionUtils.h"
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
void FBReplaceMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
|
|
Method originalMethod = class_getInstanceMethod(class, originalSelector);
|
|
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
|
|
|
|
BOOL didAddMethod =
|
|
class_addMethod(class,
|
|
originalSelector,
|
|
method_getImplementation(swizzledMethod),
|
|
method_getTypeEncoding(swizzledMethod));
|
|
|
|
if (didAddMethod) {
|
|
class_replaceMethod(class,
|
|
swizzledSelector,
|
|
method_getImplementation(originalMethod),
|
|
method_getTypeEncoding(originalMethod));
|
|
} else {
|
|
method_exchangeImplementations(originalMethod, swizzledMethod);
|
|
}
|
|
}
|