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
/
ACMethodSignature.m
261 lines (232 loc) · 8.91 KB
/
ACMethodSignature.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (c) 2006 Jacob Burkhart (JacobBurkhart@gmail.com)
*
* 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 "ACMethodSignature.h"
#import </usr/include/objc/objc-class.h>
@implementation ACMethodSignature
-(id) initWithMethod:(struct objc_method *)meth signature: (NSMethodSignature*) sig depth: (int) d{
self = [super init];
if (self){
method = meth;
signature = [sig retain];
depth = d;
}
return self;
}
-(void)dealloc{
[signature release];
[super dealloc];
}
-(NSString *)description{
return [NSString stringWithFormat: @"%s %@", method->method_name, signature];
}
-(int) getDepth{
return depth;
}
-(struct objc_method *) getMethod{
return method;
}
-(NSMethodSignature *) getSignature{
return signature;
}
-(SEL)getSelector{
return method->method_name;
}
- (SEL) getReplacementSEL {
return sel_registerName([[NSString stringWithFormat: @"%@%i%@",
[self returnSelector],
depth,
[self argsSelector]] cString]);
}
-(NSString *)stringSelectorSansDepth{
return [NSString stringWithFormat: @"%@%@", [self returnSelector], [self argsSelector]];
}
-(NSString *)stringSelectorSansColin{
return [NSString stringWithFormat: @"%@%@", [self returnSelector], [self argsSelectorSansColin]];
}
-(NSString *)argsSelector{
NSString * selectorString = @"";
int i;
for(i=2; i<[signature numberOfArguments]; i++){
NSString * nextArgType = [[NSString alloc] initWithCString: [signature getArgumentTypeAtIndex: i]];
selectorString = [NSString stringWithFormat: @"%@%@:", selectorString ,[self stringForEncodedType: nextArgType]];
}
return selectorString;
}
-(NSString *)argsSelectorSansColin{
NSString * selectorString = @"";
int i;
for(i=2; i<[signature numberOfArguments]; i++){
NSString * nextArgType = [[NSString alloc] initWithCString: [signature getArgumentTypeAtIndex: i]];
selectorString = [NSString stringWithFormat: @"%@%@", selectorString ,[self stringForEncodedType: nextArgType]];
}
return selectorString;
}
-(NSString *)returnSelector{
NSString * selectorString = @"replac";
NSString * returnType = [[NSString alloc] initWithCString: [signature methodReturnType]];
return [NSString stringWithFormat: @"%@%@", selectorString ,[self stringForEncodedType: returnType]];
}
-(NSString *)argsSelectorAt:(int) i{
NSString * argType = [[NSString alloc] initWithCString: [signature getArgumentTypeAtIndex: i+2]];
return [NSString stringWithFormat: @"%@:", [self stringForEncodedType: argType]];
}
-(NSString *)codeReturnType{
NSString * returnType = [[NSString alloc] initWithCString: [signature methodReturnType]];
return [self codeForEncodedType: returnType];
}
-(NSString *)codeTypeAt:(int) i{
NSString * argType = [[NSString alloc] initWithCString: [signature getArgumentTypeAtIndex: i+2]];
return [self codeForEncodedType: argType];
}
-(NSString *)varAt:(int) i{
char arg = 'a' + i;
return [NSString stringWithFormat: @"%c", arg];
}
-(int)argCount{
return [signature numberOfArguments]-2;
}
-(BOOL)canBeCoded{
return ([[self stringSelectorSansDepth] rangeOfString: @"?"].location == NSNotFound);
}
-(NSString*)normalizeStruct:(NSString *)structName{
if([structName hasPrefix: @"_NS"]){
if([structName isEqualToString: @"_NSRange"])
return @"NSRange";
if([structName isEqualToString: @"_NSSize"])
return @"NSSize";
if([structName isEqualToString: @"_NSPoint"])
return @"NSPoint";
if([structName isEqualToString: @"_NSRect"])
return @"NSRect";
}
NSLog(@"couldn't find code/string for type %@", structName);
return @"???";
// if([structName isEqualToString: @"NSButtonState"]){
// if([structName isEqualToString: @"_CGSEvent"]){
return structName;
}
-(NSString*)stringForEncodedType:(NSString *)type{
//as far as we can tell the signed and unsigned types work interchangable
if([type isEqualToString: @"c"] ||
[type isEqualToString: @"C"] ||
[type isEqualToString: @"i"] ||
[type isEqualToString: @"I"] ||
[type isEqualToString: @"s"] ||
[type isEqualToString: @"S"] ||
[type isEqualToString: @"b"])
return @"Char";
//int, short, char, BOOL are all the same to objc_msgSend
if([type isEqualToString: @"l"] ||
[type isEqualToString: @"L"])
return @"Long";
if([type isEqualToString: @"q"] ||
[type isEqualToString: @"Q"])
return @"LongLong";
if([type isEqualToString: @"f"] ||
[type isEqualToString: @"d"])
return @"Double";
//a double is also a float
if([type hasPrefix: @"{"] || [type hasPrefix: @"("]){
//might look like {_NSRect={_NSPoint=ff}{_NSSize=ff}}
//we should parse out the actual name of the struct or union and use it appropriately
int from;
if([type hasPrefix: @"{"])
from = [type rangeOfString: @"{"].location;
else
from = [type rangeOfString: @"("].location;
int to = [type rangeOfString: @"="].location;
return [self normalizeStruct: [type substringWithRange: NSMakeRange(from + 1, to - 1)]];
}
if([type hasPrefix: @"@"])
return @"Object";
if([type hasPrefix: @"^"])
return @"Pointer";
if([type hasPrefix: @":"])
return @"Selector";
if([type hasPrefix: @"v"] || [type hasPrefix: @"V"])
return @"Void";
if([type hasPrefix: @"#"])
return @"Class";
if([type hasPrefix: @"*"])
return @"CharStar";
NSLog(@"couldn't find string for type %@", type);
return @"???";
// if([type hasPrefix: @"?"])
// return @"?";
//the rest of types should be basically pointers..
// if([type isEqualToString: @"*"] || [type isEqualToString: @":"])//a method SEL == char *
//"^", "@", "[", "#", "v" (void), "*" , ":"....
}
-(NSString*)codeForEncodedType:(NSString *)type{
//as far as we can tell the signed and unsigned types work interchangable
if([type isEqualToString: @"c"] ||
[type isEqualToString: @"C"] ||
[type isEqualToString: @"i"] ||
[type isEqualToString: @"I"] ||
[type isEqualToString: @"s"] ||
[type isEqualToString: @"S"] ||
[type isEqualToString: @"b"])
return @"char";
//int, short, char, BOOL are all the same to objc_msgSend
if([type isEqualToString: @"l"] ||
[type isEqualToString: @"L"])
return @"long";
if([type isEqualToString: @"q"] ||
[type isEqualToString: @"Q"])
return @"long long";
if([type isEqualToString: @"f"] ||
[type isEqualToString: @"d"])
return @"double";
//a double is also a float
if([type hasPrefix: @"{"] || [type hasPrefix: @"("]){
//might look like {_NSRect={_NSPoint=ff}{_NSSize=ff}}
//we should parse out the actual name of the struct or union and use it appropriately
int from;
if([type hasPrefix: @"{"])
from = [type rangeOfString: @"{"].location;
else
from = [type rangeOfString: @"("].location;
int to = [type rangeOfString: @"="].location;
return [self normalizeStruct: [type substringWithRange: NSMakeRange(from + 1, to - 1)]];
}
if([type hasPrefix: @"@"])
return @"id";
if([type hasPrefix: @"^"])
return @"void *";
if([type hasPrefix: @":"])
return @"SEL";
if([type hasPrefix: @"v"] || [type hasPrefix: @"V"])
return @"void";
if([type hasPrefix: @"#"])
return @"Class";
if([type hasPrefix: @"*"])
return @"char *";
NSLog(@"couldn't find code for type %@", type);
return @"???";
// if([type hasPrefix: @"?"])
// return @"?";
//the rest of types should be basically pointers..
// if([type isEqualToString: @"*"] || [type isEqualToString: @":"])//a method SEL == char *
//"^", "@", "[", "#", "v" (void), "*" , ":"....
// return @"void *";
}
@end