forked from Polidea/ios-class-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDMultiFileVisitor.m
389 lines (300 loc) · 12.4 KB
/
CDMultiFileVisitor.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2015 Steve Nygard.
#import "CDMultiFileVisitor.h"
#import "CDClassDump.h"
#import "CDClassFrameworkVisitor.h"
#import "CDOCCategory.h"
#import "CDOCClass.h"
#import "CDOCProtocol.h"
#import "CDOCInstanceVariable.h"
#import "CDTypeController.h"
@interface CDMultiFileVisitor ()
// NSString (class name) -> NSString (framework name)
@property (strong) NSDictionary *frameworkNamesByClassName;
// NSString (protocol name) -> NSString (framework name)
@property (strong) NSDictionary *frameworkNamesByProtocolName;
// Location in output string to insert the protocol imports and forward class declarations.
// We don't know what classes and protocols will be referenced until the rest of the output is generated.
@property (assign) NSUInteger referenceLocation;
// Class and protocol references
@property (readonly) NSMutableSet *referencedClassNames;
@property (readonly) NSMutableSet *referencedProtocolNames;
@property (readonly) NSMutableSet *weaklyReferencedProtocolNames; // Protocols that can be forward-declared instead of imported
@property (nonatomic, readonly) NSArray *referencedClassNamesSortedByName;
@property (nonatomic, readonly) NSArray *referencedProtocolNamesSortedByName;
@property (nonatomic, readonly) NSArray *weaklyReferencedProtocolNamesSortedByName;
@property (nonatomic, readonly) NSString *referenceString;
@end
#pragma mark -
@implementation CDMultiFileVisitor
{
NSString *_outputPath;
NSDictionary *_frameworkNamesByClassName;
NSMutableSet *_referencedClassNames;
NSMutableSet *_referencedProtocolNames;
NSUInteger _referenceLocation;
}
- (id)init;
{
if ((self = [super init])) {
_referencedClassNames = [[NSMutableSet alloc] init];
_referencedProtocolNames = [[NSMutableSet alloc] init];
_weaklyReferencedProtocolNames = [[NSMutableSet alloc] init];
}
return self;
}
#pragma mark -
- (void)willBeginVisiting;
{
[super willBeginVisiting];
[self.classDump appendHeaderToString:self.resultString];
if (self.classDump.hasObjectiveCRuntimeInfo) {
[self buildClassFrameworks];
[self createOutputPathIfNecessary];
[self generateStructureHeader];
} else {
// TODO: (2007-06-14) Make sure this generates no output files in this case.
NSLog(@"Warning: This file does not contain any Objective-C runtime information.");
}
}
- (void)willVisitClass:(CDOCClass *)aClass;
{
// First, we set up some context...
[self.resultString setString:@""];
[self.classDump appendHeaderToString:self.resultString];
[self removeAllClassNameProtocolNameReferences];
NSString *str = [self importStringForClassName:aClass.superClassName];
if (str != nil) {
[self.resultString appendString:str];
[self.resultString appendString:@"\n"];
}
self.referenceLocation = [self.resultString length];
// And then generate the regular output
[super willVisitClass:aClass];
[self addReferencesToProtocolNamesInArray:aClass.protocolNames];
}
- (void)didVisitClass:(CDOCClass *)aClass;
{
// Generate the regular output
[super didVisitClass:aClass];
// Then insert the imports and write the file.
[self removeReferenceToClassName:aClass.name];
[self removeReferenceToClassName:aClass.superClassName];
NSString *referenceString = self.referenceString;
if (referenceString != nil)
[self.resultString insertString:referenceString atIndex:self.referenceLocation];
NSString *filename = [NSString stringWithFormat:@"%@.h", aClass.name];
if (self.outputPath != nil)
filename = [self.outputPath stringByAppendingPathComponent:filename];
[[self.resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
- (void)willVisitCategory:(CDOCCategory *)category;
{
// First, we set up some context...
[self.resultString setString:@""];
[self.classDump appendHeaderToString:self.resultString];
[self removeAllClassNameProtocolNameReferences];
NSString *str = [self importStringForClassName:category.className];
if (str != nil) {
[self.resultString appendString:str];
[self.resultString appendString:@"\n"];
}
self.referenceLocation = [self.resultString length];
// And then generate the regular output
[super willVisitCategory:category];
[self addReferencesToProtocolNamesInArray:category.protocolNames];
}
- (void)didVisitCategory:(CDOCCategory *)category;
{
// Generate the regular output
[super didVisitCategory:category];
// Then insert the imports and write the file.
[self removeReferenceToClassName:category.className];
NSString *referenceString = self.referenceString;
if (referenceString != nil)
[self.resultString insertString:referenceString atIndex:self.referenceLocation];
NSString *filename = [NSString stringWithFormat:@"%@-%@.h", category.className, category.name];
if (self.outputPath != nil)
filename = [self.outputPath stringByAppendingPathComponent:filename];
[[self.resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
- (void)willVisitProtocol:(CDOCProtocol *)protocol;
{
[self.resultString setString:@""];
[self.classDump appendHeaderToString:self.resultString];
[self removeAllClassNameProtocolNameReferences];
self.referenceLocation = [self.resultString length];
// And then generate the regular output
[super willVisitProtocol:protocol];
[self addReferencesToProtocolNamesInArray:protocol.protocolNames];
}
- (void)didVisitProtocol:(CDOCProtocol *)protocol;
{
// Generate the regular output
[super didVisitProtocol:protocol];
// Then insert the imports and write the file.
NSString *referenceString = self.referenceString;
if (referenceString != nil)
[self.resultString insertString:referenceString atIndex:self.referenceLocation];
NSString *filename = [NSString stringWithFormat:@"%@-Protocol.h", protocol.name];
if (self.outputPath != nil)
filename = [self.outputPath stringByAppendingPathComponent:filename];
[[self.resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
#pragma mark - CDTypeControllerDelegate
- (void)typeController:(CDTypeController *)typeController didReferenceClassName:(NSString *)name;
{
[self addReferenceToClassName:name];
}
- (void)typeController:(CDTypeController *)typeController didReferenceProtocolNames:(NSArray *)names;
{
[self addWeakReferencesToProtocolNamesInArray:names];
}
#pragma mark -
- (NSString *)frameworkForClassName:(NSString *)name;
{
NSString *framework = self.frameworkNamesByClassName[name];
// Map public CoreFoundation classes to Foundation, because that is where the headers are exposed
if ([framework isEqualToString:@"CoreFoundation"] && [name hasPrefix:@"NS"]) {
framework = @"Foundation";
}
return framework;
}
- (NSString *)frameworkForProtocolName:(NSString *)name;
{
return self.frameworkNamesByProtocolName[name];
}
- (NSString *)importStringForClassName:(NSString *)name;
{
if (name != nil) {
NSString *framework = [self frameworkForClassName:name];
if (framework == nil)
return [NSString stringWithFormat:@"#import \"%@.h\"\n", name];
else
return [NSString stringWithFormat:@"#import <%@/%@.h>\n", framework, name];
}
return nil;
}
- (NSString *)importStringForProtocolName:(NSString *)name;
{
if (name != nil) {
NSString *framework = [self frameworkForProtocolName:name];
NSString *headerName = [name stringByAppendingString:@"-Protocol.h"];
if (framework == nil)
return [NSString stringWithFormat:@"#import \"%@\"\n", headerName];
else
return [NSString stringWithFormat:@"#import <%@/%@>\n", framework, headerName];
}
return nil;
}
#pragma mark - Class and Protocol name tracking
- (NSArray *)referencedClassNamesSortedByName;
{
return [[self.referencedClassNames allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (NSArray *)referencedProtocolNamesSortedByName;
{
return [[self.referencedProtocolNames allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (NSArray *)weaklyReferencedProtocolNamesSortedByName;
{
return [[self.weaklyReferencedProtocolNames allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (void)addReferenceToClassName:(NSString *)className;
{
[self.referencedClassNames addObject:className];
}
- (void)removeReferenceToClassName:(NSString *)className;
{
if (className != nil)
[self.referencedClassNames removeObject:className];
}
- (void)addReferencesToProtocolNamesInArray:(NSArray *)protocolNames;
{
[self.referencedProtocolNames addObjectsFromArray:protocolNames];
}
- (void)addWeakReferencesToProtocolNamesInArray:(NSArray *)protocolNames;
{
[self.weaklyReferencedProtocolNames addObjectsFromArray:protocolNames];
}
- (void)removeAllClassNameProtocolNameReferences;
{
[self.referencedClassNames removeAllObjects];
[self.referencedProtocolNames removeAllObjects];
[self.weaklyReferencedProtocolNames removeAllObjects];
}
#pragma mark -
- (void)createOutputPathIfNecessary;
{
if (self.outputPath != nil) {
BOOL isDirectory;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:self.outputPath isDirectory:&isDirectory] == NO) {
NSError *error = nil;
BOOL result = [fileManager createDirectoryAtPath:self.outputPath withIntermediateDirectories:YES attributes:nil error:&error];
if (result == NO) {
NSLog(@"Error: Couldn't create output directory: %@", self.outputPath);
NSLog(@"error: %@", error); // TODO: Test this
return;
}
} else if (isDirectory == NO) {
NSLog(@"Error: File exists at output path: %@", self.outputPath);
return;
}
}
}
#pragma mark -
// - imports for each referenced protocol
// - forward declarations for each referenced class
- (NSString *)referenceString;
{
NSMutableString *referenceString = [[NSMutableString alloc] init];
if ([self.referencedProtocolNames count] > 0) {
for (NSString *name in self.referencedProtocolNamesSortedByName) {
NSString *str = [self importStringForProtocolName:name];
if (str != nil)
[referenceString appendString:str];
}
[referenceString appendString:@"\n"];
}
BOOL addNewline = NO;
if ([self.referencedClassNames count] > 0) {
[referenceString appendFormat:@"@class %@;\n", [self.referencedClassNamesSortedByName componentsJoinedByString:@", "]];
addNewline = YES;
}
if ([self.weaklyReferencedProtocolNames count] > 0) {
[referenceString appendFormat:@"@protocol %@;\n", [self.weaklyReferencedProtocolNamesSortedByName componentsJoinedByString:@", "]];
addNewline = YES;
}
if (addNewline)
[referenceString appendString:@"\n"];
if ([referenceString length] == 0)
return nil;
return [referenceString copy];
}
#pragma mark -
- (void)buildClassFrameworks;
{
CDClassFrameworkVisitor *visitor = [[CDClassFrameworkVisitor alloc] init];
visitor.classDump = self.classDump;
[self.classDump recursivelyVisit:visitor];
self.frameworkNamesByClassName = visitor.frameworkNamesByClassName;
self.frameworkNamesByProtocolName = visitor.frameworkNamesByProtocolName;
}
- (void)generateStructureHeader;
{
[self.resultString setString:@""];
[self.classDump appendHeaderToString:self.resultString];
[self removeAllClassNameProtocolNameReferences];
self.referenceLocation = [self.resultString length];
[[self.classDump typeController] appendStructuresToString:self.resultString];
NSString *referenceString = [self referenceString];
if (referenceString != nil)
[self.resultString insertString:referenceString atIndex:self.referenceLocation];
NSString *filename = @"CDStructures.h";
if (self.outputPath != nil)
filename = [self.outputPath stringByAppendingPathComponent:filename];
[[self.resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
@end