2 * Copyright 2018 Google LLC
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import <Foundation/Foundation.h>
19 NS_ASSUME_NONNULL_BEGIN
21 /** This class handles the runtime manipulation necessary to instrument selectors. It stores the
22 * classes and selectors that have been swizzled, and runs all operations on its own queue.
24 @interface GULSwizzler : NSObject
26 /** Manipulates the Objective-C runtime to replace the original IMP with the supplied block.
28 * @param aClass The class to swizzle.
29 * @param selector The selector of the class to swizzle.
30 * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
31 * @param block The block that replaces the original IMP.
33 + (void)swizzleClass:(Class)aClass
34 selector:(SEL)selector
35 isClassSelector:(BOOL)isClassSelector
36 withBlock:(nullable id)block;
38 /** Restores the original implementation.
40 * @param aClass The class to unswizzle.
41 * @param selector The selector to restore the original implementation of.
42 * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
44 + (void)unswizzleClass:(Class)aClass selector:(SEL)selector isClassSelector:(BOOL)isClassSelector;
46 /** Returns the current IMP for the given class and selector.
48 * @param aClass The class to use.
49 * @param selector The selector to find the implementation of.
50 * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
51 * @return The implementation of the selector in the runtime.
53 + (nullable IMP)currentImplementationForClass:(Class)aClass
54 selector:(SEL)selector
55 isClassSelector:(BOOL)isClassSelector;
57 /** Returns the original IMP for the given class and selector.
59 * @param aClass The class to use.
60 * @param selector The selector to find the implementation of.
61 * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
62 * @return The implementation of the selector in the runtime before any consumer or GULSwizzler
65 + (nullable IMP)originalImplementationForClass:(Class)aClass
66 selector:(SEL)selector
67 isClassSelector:(BOOL)isClassSelector;
69 /** Checks the runtime to see if a selector exists on a class. If a property is declared as
70 * @dynamic, we have a reverse swizzling situation, where the implementation of a method exists
71 * only in concrete subclasses, and NOT in the superclass. We can detect that situation using
72 * this helper method. Similarly, we can detect situations where a class doesn't implement a
75 * @param selector The selector to check for.
76 * @param aClass The class to check.
77 * @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
78 * @return YES if the method was found in this selector/class combination, NO otherwise.
80 + (BOOL)selector:(SEL)selector existsInClass:(Class)aClass isClassSelector:(BOOL)isClassSelector;
82 /** Returns a list of all Objective-C (and not primitive) ivars contained by the given object.
84 * @param object The object whose ivars will be iterated.
85 * @return The list of ivar objects.
87 + (NSArray<id> *)ivarObjectsForObject:(id)object;