RLMTestUtils.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Foundation/Foundation.h>
  19. #import "RLMTestUtils.h"
  20. #import <objc/runtime.h>
  21. static void RLMSwapOutMethod(Class class,
  22. SEL original, Method originalMethod,
  23. SEL swizzled, Method swizzledMethod) {
  24. if (class_addMethod(class,
  25. original,
  26. method_getImplementation(swizzledMethod),
  27. method_getTypeEncoding(swizzledMethod))) {
  28. class_replaceMethod(class,
  29. swizzled,
  30. method_getImplementation(originalMethod),
  31. method_getTypeEncoding(originalMethod));
  32. } else {
  33. method_exchangeImplementations(originalMethod, swizzledMethod);
  34. }
  35. }
  36. void RLMSwapOutClassMethod(id classObject, SEL original, SEL swizzled) {
  37. Class class = object_getClass((id)classObject);
  38. RLMSwapOutMethod(class,
  39. original, class_getClassMethod(class, original),
  40. swizzled, class_getClassMethod(class, swizzled));
  41. }
  42. void RLMSwapOutInstanceMethod(id classObject, SEL original, SEL swizzled) {
  43. Class class = [classObject class];
  44. RLMSwapOutMethod(class,
  45. original, class_getInstanceMethod(class, original),
  46. swizzled, class_getInstanceMethod(class, swizzled));
  47. }