FBReflectionUtils.m 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "FBReflectionUtils.h"
  10. #import <objc/runtime.h>
  11. void FBReplaceMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
  12. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  13. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  14. BOOL didAddMethod =
  15. class_addMethod(class,
  16. originalSelector,
  17. method_getImplementation(swizzledMethod),
  18. method_getTypeEncoding(swizzledMethod));
  19. if (didAddMethod) {
  20. class_replaceMethod(class,
  21. swizzledSelector,
  22. method_getImplementation(originalMethod),
  23. method_getTypeEncoding(originalMethod));
  24. } else {
  25. method_exchangeImplementations(originalMethod, swizzledMethod);
  26. }
  27. }