-
Notifications
You must be signed in to change notification settings - Fork 5
/
SCKIntrospection.m
233 lines (209 loc) · 5.27 KB
/
SCKIntrospection.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
#import "SCKIntrospection.h"
#import "SCKClangSourceFile.h"
#import <EtoileFoundation/EtoileFoundation.h>
#include <objc/runtime.h>
@implementation SCKProgramComponent
@synthesize parent, declaration, definition, documentation, name;
- (BOOL)isForwardDeclaration
{
return (declaration == nil);
}
- (NSString*)description
{
return name;
}
@end
@implementation SCKBundle
@synthesize classes, functions;
- (id)init
{
SUPERINIT;
classes = [NSMutableArray new];
functions = [NSMutableArray new];
return self;
}
- (NSString*)description
{
NSMutableString *str = [self.name mutableCopy];
for (id function in functions)
{
[str appendFormat: @"\n\t%@", function];
}
for (id class in classes)
{
[str appendFormat: @"\n\t%@", class];
}
return str;
}
@end
@implementation SCKClass
@synthesize subclasses, superclass, categories, methods, ivars, properties;
- (NSString*)description
{
NSMutableString *str = [self.name mutableCopy];
for (id ivar in ivars)
{
[str appendFormat: @"\n\t\t%@", ivar];
}
for (id method in methods)
{
[str appendFormat: @"\n\t\t%@", method];
}
for (id property in properties)
{
[str appendFormat: @"\n\t\t%@", property];
}
return str;
}
- (id)init
{
SUPERINIT;
subclasses = [NSMutableArray new];
categories = [NSMutableDictionary new];
methods = [NSMutableDictionary new];
ivars = [NSMutableArray new];
properties = [NSMutableArray new];
return self;
}
- (id)initWithClass: (Class)cls
{
if (nil == (self = [self init])) { return nil; }
unsigned int count;
Ivar *ivarList = class_copyIvarList(cls, &count);
for (unsigned int i=0 ; i<count ; i++)
{
SCKIvar *ivar = [SCKIvar new];
ivar.name = [NSString stringWithUTF8String: ivar_getName(ivarList[i])];
[ivar setTypeEncoding: [NSString stringWithUTF8String: ivar_getTypeEncoding(ivarList[i])]];
ivar.parent = self;
[ivars addObject: ivar];
}
if (count>0)
{
free(ivarList);
}
Method *methodList = class_copyMethodList(cls, &count);
for (unsigned int i=0 ; i<count ; i++)
{
SCKMethod *method = [SCKMethod new];
method.name = [NSString stringWithUTF8String: sel_getName(method_getName(methodList[i]))];
[method setTypeEncoding: [NSString stringWithUTF8String: method_getTypeEncoding(methodList[i])]];
method.parent = self;
[methods setObject: method forKey: method.name];
}
if (count>0)
{
free(methodList);
}
objc_property_t *propertyList = class_copyPropertyList(cls, &count);
for (unsigned int i=0 ; i<count; i++)
{
SCKProperty *property = [SCKProperty new];
[property setName: [NSString stringWithUTF8String: property_getName(propertyList[i])]];
[property setTypeEncoding: [NSString stringWithUTF8String: property_getAttributes(propertyList[i])]];
[property setParent: self];
[properties addObject: property];
}
if (count>0)
{
free(propertyList);
}
self.name = [[NSString alloc] initWithUTF8String: class_getName(cls)];
return self;
}
- (SCKIvar*)ivarForName: (NSString*)name
{
return [[ivars filteredCollectionWithBlock: ^ (SCKIvar *ivar)
{
return [[ivar name] isEqualToString: name];
}] firstObject];
}
- (SCKProperty*)propertyForName: (NSString*)name
{
return [[properties filteredCollectionWithBlock: ^ (SCKProperty *prop)
{
return [[prop name] isEqualToString: name];
}] firstObject];
}
@end
@implementation SCKProtocol
@synthesize requiredMethods, optionalMethods, requiredProperties, optionalProperties;
- (id)init
{
SUPERINIT;
optionalMethods = [NSMutableDictionary new];
requiredMethods = [NSMutableDictionary new];
optionalProperties = [NSMutableArray new];
requiredProperties = [NSMutableArray new];
return self;
}
- (SCKProperty*)requiredPropertyForName: (NSString*)name
{
return [[requiredProperties filteredCollectionWithBlock: ^ (SCKProperty *prop)
{
return [[prop name] isEqualToString: name];
}] firstObject];
}
- (SCKProperty*)optionalPropertyForName: (NSString*)name
{
return [[optionalProperties filteredCollectionWithBlock: ^ (SCKProperty *prop)
{
return [[prop name] isEqualToString: name];
}] firstObject];
}
@end
@implementation SCKCategory : SCKProgramComponent
@synthesize methods, properties;
- (id)init
{
SUPERINIT;
methods = [NSMutableDictionary new];
properties = [NSMutableArray new];
return self;
}
- (NSString*)description
{
NSMutableString *str = [NSMutableString stringWithFormat: @"%@ (%@)", self.parent.name, self.name];
for (id method in methods)
{
[str appendFormat: @"\n\t%@", method];
}
return str;
}
- (SCKProperty*)propertyForName: (NSString*)name
{
return [[properties filteredCollectionWithBlock: ^ (SCKProperty *prop)
{
return [[prop name] isEqualToString: name];
}] firstObject];
}
@end
@implementation SCKMethod
@synthesize isClassMethod;
- (NSString*)description
{
return [NSString stringWithFormat: @"%c%@", isClassMethod ? '+' : '-', self.name];
}
@end
@implementation SCKTypedProgramComponent
@synthesize typeEncoding;
@end
@implementation SCKIvar
@synthesize isIBOutlet;
@end
@implementation SCKFunction @end
@implementation SCKGlobal @end
@implementation SCKProperty
@synthesize attributes, isIBOutlet;
@end
@implementation SCKMacro @end
@implementation SCKEnumeration
@synthesize values;
@end
@implementation SCKEnumerationValue
@synthesize longLongValue, enumerationName;
- (NSString*)description
{
return [NSString stringWithFormat: @"%@ (%lld)", self.name, longLongValue];
}
@end