This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathACAspectManager.m
208 lines (170 loc) · 6.44 KB
/
ACAspectManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (c) 2006 Jacob Burkhart ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#import "ACAspectManager.h"
#import "ACAdviceList.h"
#import "ACAspect.h"
#import "ACInvocation.h"
#import "ACIMP.h"
@implementation ACAspectManager
static int loggingStyle = 0;
static ACAspectManager *_ac_aspect_manager_instance = nil;
+(ACAspectManager *)sharedManager{
if( _ac_aspect_manager_instance == nil){
_ac_aspect_manager_instance = [[[self alloc] init] retain];
/*
NSLog(@"loading signature aspect");
ACPointCut * pointCut = [[ACPointCut alloc] init];
[pointCut setClassScope: [[ACClassScope alloc] init]];
[[pointCut getClassScope] addClassAndMeta: [NSObject class]];
[[pointCut getMethodFilter] filterAllMethods];
[[pointCut getMethodFilter] allowMethodsNamed:@"methodSignatureForSelector:"];
ACLookAspect * signatureAspect = [[ACLookAspect alloc] initWithPointCut: pointCut andAdviceObject: [ACSignatureFix new]];
[signatureAspect load];
NSLog(@"loaded signature aspect");
*/
}
return _ac_aspect_manager_instance;
}
+(void)enableAllAdvice{
[ACInvocation enableAllAdvice];
}
+(void)disableAllAdvice{
[ACInvocation disableAllAdvice];
}
+(void)protectAdvice{
[ACInvocation protectAdvice];
}
+(void)unprotectAdvice{
[ACInvocation unprotectAdvice];
}
+(void)quiet{
loggingStyle = 0;
}
+(void)genOnly{
loggingStyle = 1;
}
+(void)verbose{
loggingStyle = 2;
}
+(BOOL)loggingGen{
return loggingStyle >= 1;
}
+(BOOL)loggingAll{
return loggingStyle == 2;
}
- (id)init {
self = [super init];
if (self) {
adviceLookup = [[ACAdviceLookup new] retain];
methodSigLookup = [[ACIMPLookup new] retain];
forwardInvLookup = [[ACIMPLookup new] retain];
classWrappedWithDepth = [[ACBOOLLookup new] retain];
classWrappedWtihForwarding = [[ACBOOLLookup new] retain];
}
return self;
}
- (void)dealloc{
[adviceLookup release];
[methodSigLookup release];
[forwardInvLookup release];
[classWrappedWithDepth release];
[classWrappedWtihForwarding release];
[super dealloc];
}
-(void)setWrappedWithDepth:(BOOL) b forClass: (Class) c{
[classWrappedWithDepth saveBOOL: b forClass: c];
}
-(BOOL)isClassWrappedWithDepth:(Class)c{
return [classWrappedWithDepth getBOOLforClass: c];
}
-(void)setWrappedWithForwarding:(BOOL) b forClass: (Class) c{
[classWrappedWtihForwarding saveBOOL: b forClass: c];
}
-(BOOL)isClassWrappedWithForwarding:(Class)c{
return [classWrappedWtihForwarding getBOOLforClass: c];
}
/*
-(void)set:(BOOL) b forClassReady: (Class) c{
[classReadyLookup saveBOOL: b forClass: c];
}
-(BOOL)isClassWrappedWithDepth:(Class)c{
return [classReadyLookup getBOOLforClass: c];
}
*/
/*
-(void)wrap:(ACMethodWrapper *) wrapper withAdvice: (id)advice{
if([ACAspectManager loggingAll])
NSLog(@"---wrapping selector %s on class %s", method->method_name, class->name);
[adviceLookup addAdvice: advice forMethod: method onClass: class];
}
*/
-(void)addAdviceObject:(id)advice forMethod: (struct objc_method *)method onClass: (Class)class{
//store the original IMP and the advice for the 2 keys selectorKey and classKey
//if an IMP is already being stored there, then just add the new advice
if([ACAspectManager loggingAll])
NSLog(@"---wrapping selector %s on class %s", method->method_name, class->name);
[adviceLookup addAdvice: advice forMethod: method onClass: class];
}
-(void)removeAdviceObject:(id)advice forMethod: (struct objc_method *)method onClass: (Class)class{
//remove the advice from the store
//if no advice left, also restore the IMP to method and remove the ACAdviceListWithIMP object
if([ACAspectManager loggingAll])
NSLog(@"---unwrapping selector %s on class %s", method->method_name, class->name);
[adviceLookup removeAdvice: advice forMethod: method onClass: class];
}
-(ACAdviceList *)adviceListForSelector: (SEL)selector onClass: (Class) toLookup{
ACAdviceList * toReturn = [adviceLookup adviceListforSelector: selector onClass: toLookup];
if(toReturn == nil)
NSLog(@"nil advice %s",toLookup->name);
return toReturn;
}
-(NSMethodSignature *)savedMethodSignatureForSelector: (SEL)selector onClass: (Class) toLookup{
ACAdviceList * adviceList = [adviceLookup adviceListforSelector: selector onClass: toLookup];
if(adviceList == nil)
return nil;
else
return [adviceList getSig];
}
//+(ACAdviceList *)adviceListForSelector: (SEL)selector onObject: (id)object depth: (int) d{
// return [[self sharedManager] adviceListForSelector: selector onObject: object];
//}
-(void)saveSig: (Method) sig forClass: (Class) c{
[methodSigLookup saveIMP: sig->method_imp forClass: c];
}
-(void)saveInv: (Method) inv forClass: (Class) c{
[forwardInvLookup saveIMP: inv->method_imp forClass: c];
}
-(ACIMP *)getIMPforClass: (Class) c onLookup: (ACIMPLookup *) impLookup methodDepth: (int) md objectDepth: (int) od{
Class toLookup = c;
while(md < od){
md ++;
toLookup = toLookup->super_class;
}
return [impLookup getIMPforClass: toLookup];
}
-(ACIMP *)getSigforClass: (Class) c methodDepth: (int) md objectDepth: (int) od{
return [self getIMPforClass: c onLookup: methodSigLookup methodDepth: md objectDepth: od];
}
-(ACIMP *)getInvforClass: (Class) c methodDepth: (int) md objectDepth: (int) od{
return [self getIMPforClass: c onLookup: forwardInvLookup methodDepth: md objectDepth: od];
}
@end