1 |
efrain |
1 |
// Copyright 2018 Google LLC
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
#import <objc/runtime.h>
|
|
|
16 |
|
|
|
17 |
#import "GoogleUtilities/ISASwizzler/GULObjectSwizzler+Internal.h"
|
|
|
18 |
#import "GoogleUtilities/ISASwizzler/Public/GoogleUtilities/GULSwizzledObject.h"
|
|
|
19 |
|
|
|
20 |
NSString *kGULSwizzlerAssociatedObjectKey = @"gul_objectSwizzler";
|
|
|
21 |
|
|
|
22 |
@interface GULSwizzledObject ()
|
|
|
23 |
|
|
|
24 |
@end
|
|
|
25 |
|
|
|
26 |
@implementation GULSwizzledObject
|
|
|
27 |
|
|
|
28 |
+ (void)copyDonorSelectorsUsingObjectSwizzler:(GULObjectSwizzler *)objectSwizzler {
|
|
|
29 |
[objectSwizzler copySelector:@selector(gul_objectSwizzler) fromClass:self isClassSelector:NO];
|
|
|
30 |
[objectSwizzler copySelector:@selector(gul_class) fromClass:self isClassSelector:NO];
|
|
|
31 |
|
|
|
32 |
// This is needed because NSProxy objects usually override -[NSObjectProtocol respondsToSelector:]
|
|
|
33 |
// and ask this question to the underlying object. Since we don't swizzle the underlying object
|
|
|
34 |
// but swizzle the proxy, when someone calls -[NSObjectProtocol respondsToSelector:] on the proxy,
|
|
|
35 |
// the answer ends up being NO even if we added new methods to the subclass through ISA Swizzling.
|
|
|
36 |
// To solve that, we override -[NSObjectProtocol respondsToSelector:] in such a way that takes
|
|
|
37 |
// into account the fact that we've added new methods.
|
|
|
38 |
if ([objectSwizzler isSwizzlingProxyObject]) {
|
|
|
39 |
[objectSwizzler copySelector:@selector(respondsToSelector:) fromClass:self isClassSelector:NO];
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
- (instancetype)init {
|
|
|
44 |
NSAssert(NO, @"Do not instantiate this class, it's only a donor class");
|
|
|
45 |
return nil;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
- (GULObjectSwizzler *)gul_objectSwizzler {
|
|
|
49 |
return [GULObjectSwizzler getAssociatedObject:self key:kGULSwizzlerAssociatedObjectKey];
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
#pragma mark - Donor methods
|
|
|
53 |
|
|
|
54 |
- (Class)gul_class {
|
|
|
55 |
return [[self gul_objectSwizzler] generatedClass];
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Only added to a class when we detect it is a proxy.
|
|
|
59 |
- (BOOL)respondsToSelector:(SEL)aSelector {
|
|
|
60 |
Class gulClass = [[self gul_objectSwizzler] generatedClass];
|
|
|
61 |
return [gulClass instancesRespondToSelector:aSelector] || [super respondsToSelector:aSelector];
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
@end
|