Commit 5da724a1 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Swizzled method has to exist in ScopedMethodSwizzler

This CL adds a DCHECK to make sure that the method to be swizzled in
ScopedMethodSwizzler exists.
Previously the method was added.

Bug: none
Change-Id: I9d6a9d935f7cf15cb644df1e70c44f8188f3bdf3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1913252
Auto-Submit: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714917}
parent 7e3e753a
......@@ -14,6 +14,7 @@ class ScopedMethodSwizzler {
// Constructs a new ScopedMethodSwizzler object and replaces the
// implementation of |selector_to_replace| on the |target| class with the
// given |replacing_selector|. ScopedMethodSwizzler swizzles instance methods.
// |selector_to_replace| has to be implemented on the class.
ScopedMethodSwizzler(Class target,
SEL selector_to_replace,
SEL replacing_selector);
......
......@@ -4,6 +4,8 @@
#import "ios/testing/scoped_method_swizzler.h"
#include "base/logging.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
......@@ -12,17 +14,9 @@ ScopedMethodSwizzler::ScopedMethodSwizzler(Class klass,
SEL selector_to_replace,
SEL replacing_selector) {
original_method_ = class_getInstanceMethod(klass, selector_to_replace);
DCHECK(original_method_);
replacing_method_ = class_getInstanceMethod(klass, replacing_selector);
BOOL method_added = class_addMethod(
klass, selector_to_replace, method_getImplementation(replacing_method_),
method_getTypeEncoding(replacing_method_));
if (method_added) {
class_replaceMethod(klass, replacing_selector,
method_getImplementation(original_method_),
method_getTypeEncoding(original_method_));
} else {
method_exchangeImplementations(original_method_, replacing_method_);
}
method_exchangeImplementations(original_method_, replacing_method_);
}
ScopedMethodSwizzler::~ScopedMethodSwizzler() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment