1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2018 Google LLC
|
|
|
3 |
*
|
|
|
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
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
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.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import <Foundation/Foundation.h>
|
|
|
18 |
|
|
|
19 |
NS_ASSUME_NONNULL_BEGIN
|
|
|
20 |
|
|
|
21 |
/** Enums that map to their OBJC-prefixed counterparts. */
|
|
|
22 |
typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
|
|
|
23 |
|
|
|
24 |
// Is a weak association.
|
|
|
25 |
GUL_ASSOCIATION_ASSIGN,
|
|
|
26 |
|
|
|
27 |
// Is a nonatomic strong association.
|
|
|
28 |
GUL_ASSOCIATION_RETAIN_NONATOMIC,
|
|
|
29 |
|
|
|
30 |
// Is a nonatomic copy association.
|
|
|
31 |
GUL_ASSOCIATION_COPY_NONATOMIC,
|
|
|
32 |
|
|
|
33 |
// Is an atomic strong association.
|
|
|
34 |
GUL_ASSOCIATION_RETAIN,
|
|
|
35 |
|
|
|
36 |
// Is an atomic copy association.
|
|
|
37 |
GUL_ASSOCIATION_COPY};
|
|
|
38 |
|
|
|
39 |
/** This class handles swizzling a specific instance of a class by generating a
|
|
|
40 |
* dynamic subclass and installing selectors and properties onto the dynamic
|
|
|
41 |
* subclass. Then, the instance's class is set to the dynamic subclass. There
|
|
|
42 |
* should be a 1:1 ratio of object swizzlers to swizzled instances.
|
|
|
43 |
*/
|
|
|
44 |
@interface GULObjectSwizzler : NSObject
|
|
|
45 |
|
|
|
46 |
/** The subclass that is generated. */
|
|
|
47 |
@property(nullable, nonatomic, readonly) Class generatedClass;
|
|
|
48 |
|
|
|
49 |
/** Sets an associated object in the runtime. This mechanism can be used to
|
|
|
50 |
* simulate adding properties.
|
|
|
51 |
*
|
|
|
52 |
* @param object The object that will be queried for the associated object.
|
|
|
53 |
* @param key The key of the associated object.
|
|
|
54 |
* @param value The value to associate to the swizzled object.
|
|
|
55 |
* @param association The mechanism to use when associating the objects.
|
|
|
56 |
*/
|
|
|
57 |
+ (void)setAssociatedObject:(id)object
|
|
|
58 |
key:(NSString *)key
|
|
|
59 |
value:(nullable id)value
|
|
|
60 |
association:(GUL_ASSOCIATION)association;
|
|
|
61 |
|
|
|
62 |
/** Gets an associated object in the runtime. This mechanism can be used to
|
|
|
63 |
* simulate adding properties.
|
|
|
64 |
*
|
|
|
65 |
* @param object The object that will be queried for the associated object.
|
|
|
66 |
* @param key The key of the associated object.
|
|
|
67 |
*/
|
|
|
68 |
+ (nullable id)getAssociatedObject:(id)object key:(NSString *)key;
|
|
|
69 |
|
|
|
70 |
/** Please use the designated initializer. */
|
|
|
71 |
- (instancetype)init NS_UNAVAILABLE;
|
|
|
72 |
|
|
|
73 |
/** Instantiates an object swizzler using an object it will operate on.
|
|
|
74 |
* Generates a new class pair.
|
|
|
75 |
*
|
|
|
76 |
* @note There is no need to store this object. After calling -swizzle, this
|
|
|
77 |
* object can be found by calling -gul_objectSwizzler
|
|
|
78 |
*
|
|
|
79 |
* @param object The object to be swizzled.
|
|
|
80 |
* @return An instance of this class.
|
|
|
81 |
*/
|
|
|
82 |
- (instancetype)initWithObject:(id)object NS_DESIGNATED_INITIALIZER;
|
|
|
83 |
|
|
|
84 |
/** Sets an associated object in the runtime. This mechanism can be used to
|
|
|
85 |
* simulate adding properties.
|
|
|
86 |
*
|
|
|
87 |
* @param key The key of the associated object.
|
|
|
88 |
* @param value The value to associate to the swizzled object.
|
|
|
89 |
* @param association The mechanism to use when associating the objects.
|
|
|
90 |
*/
|
|
|
91 |
- (void)setAssociatedObjectWithKey:(NSString *)key
|
|
|
92 |
value:(id)value
|
|
|
93 |
association:(GUL_ASSOCIATION)association;
|
|
|
94 |
|
|
|
95 |
/** Gets an associated object in the runtime. This mechanism can be used to
|
|
|
96 |
* simulate adding properties.
|
|
|
97 |
*
|
|
|
98 |
* @param key The key of the associated object.
|
|
|
99 |
*/
|
|
|
100 |
- (nullable id)getAssociatedObjectForKey:(NSString *)key;
|
|
|
101 |
|
|
|
102 |
/** Copies a selector from an existing class onto the generated dynamic subclass
|
|
|
103 |
* that this object will adopt. This mechanism can be used to add methods to
|
|
|
104 |
* specific instances of a class.
|
|
|
105 |
*
|
|
|
106 |
* @note Should not be called after calling -swizzle.
|
|
|
107 |
* @param selector The selector to add to the instance.
|
|
|
108 |
* @param aClass The class supplying an implementation of the method.
|
|
|
109 |
* @param isClassSelector A BOOL specifying whether the selector is a class or
|
|
|
110 |
* instance selector.
|
|
|
111 |
*/
|
|
|
112 |
- (void)copySelector:(SEL)selector fromClass:(Class)aClass isClassSelector:(BOOL)isClassSelector;
|
|
|
113 |
|
|
|
114 |
/** Swizzles the object, changing its class to the generated class. Registers
|
|
|
115 |
* the class pair. */
|
|
|
116 |
- (void)swizzle;
|
|
|
117 |
|
|
|
118 |
/** @return The value of -[objectBeingSwizzled isProxy] */
|
|
|
119 |
- (BOOL)isSwizzlingProxyObject;
|
|
|
120 |
|
|
|
121 |
@end
|
|
|
122 |
|
|
|
123 |
NS_ASSUME_NONNULL_END
|