forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDOCProtocol.m
More file actions
266 lines (216 loc) · 8.33 KB
/
CDOCProtocol.m
File metadata and controls
266 lines (216 loc) · 8.33 KB
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
// 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-2006 Steve Nygard
#import "CDOCProtocol.h"
#import <Foundation/Foundation.h>
#import "NSArray-Extensions.h"
#import "CDClassDump.h"
#import "CDOCMethod.h"
#import "CDOCSymtab.h"
#import "CDSymbolReferences.h"
#import "CDTypeParser.h"
@implementation CDOCProtocol
- (id)init;
{
if ([super init] == nil)
return nil;
name = nil;
protocols = [[NSMutableArray alloc] init];
classMethods = nil;
instanceMethods = nil;
adoptedProtocolNames = [[NSMutableSet alloc] init];
return self;
}
- (void)dealloc;
{
[name release];
[protocols release];
[classMethods release];
[instanceMethods release];
[adoptedProtocolNames release];
[super dealloc];
}
- (NSString *)name;
{
return name;
}
- (void)setName:(NSString *)newName;
{
if (newName == name)
return;
[name release];
name = [newName retain];
}
- (NSArray *)protocols;
{
return protocols;
}
// This assumes that the protocol name doesn't change after it's been added to this.
- (void)addProtocol:(CDOCProtocol *)aProtocol;
{
if ([adoptedProtocolNames containsObject:[aProtocol name]] == NO) {
[protocols addObject:aProtocol];
[adoptedProtocolNames addObject:[aProtocol name]];
}
}
- (void)removeProtocol:(CDOCProtocol *)aProtocol;
{
[adoptedProtocolNames removeObject:[aProtocol name]];
[protocols removeObject:aProtocol];
}
- (void)addProtocolsFromArray:(NSArray *)newProtocols;
{
int count, index;
count = [newProtocols count];
for (index = 0; index < count; index++)
[self addProtocol:[newProtocols objectAtIndex:index]];
}
- (NSArray *)classMethods;
{
return classMethods;
}
- (void)setClassMethods:(NSArray *)newClassMethods;
{
if (newClassMethods == classMethods)
return;
[classMethods release];
classMethods = [newClassMethods retain];
}
- (NSArray *)instanceMethods;
{
return instanceMethods;
}
- (void)setInstanceMethods:(NSArray *)newInstanceMethods;
{
if (newInstanceMethods == instanceMethods)
return;
[instanceMethods release];
instanceMethods = [newInstanceMethods retain];
}
- (NSString *)description;
{
return [NSString stringWithFormat:@"[%@] name: %@, protocols: %d, class methods: %d, instance methods: %d",
NSStringFromClass([self class]), name, [protocols count], [classMethods count], [instanceMethods count]];
}
- (void)addToXMLElement:(NSXMLElement *)xmlElement classDump:(CDClassDump *)aClassDump symbolReferences:(CDSymbolReferences *)symbolReferences;
{
if ([aClassDump shouldMatchRegex] == YES && [aClassDump regexMatchesString:[self name]] == NO)
return;
NSXMLElement *protocolElement = [NSXMLElement elementWithName:@"protocol"];
[protocolElement addChild:[NSXMLElement elementWithName:@"name" stringValue:name]];
if ([protocols count] > 0) {
int count, index;
NSArray *protocolsArray = [protocols arrayByMappingSelector:@selector(name)];
count = [protocolsArray count];
NSMutableArray *incorporatedProtocolElements = [NSMutableArray arrayWithCapacity:count];
for (index = 0; index < count; index++) {
[incorporatedProtocolElements addObject:[NSXMLElement elementWithName:@"protocol" stringValue:[protocolsArray objectAtIndex:index]]];
}
[protocolElement addChild:[NSXMLElement elementWithName:@"incorporates" children:incorporatedProtocolElements attributes:nil]];
[symbolReferences addProtocolNamesFromArray:[protocols arrayByMappingSelector:@selector(name)]];
}
[self addMethodsToXMLElement:protocolElement classDump:aClassDump symbolReferences:symbolReferences];
[xmlElement addChild:protocolElement];
}
- (void)appendToString:(NSMutableString *)resultString classDump:(CDClassDump *)aClassDump symbolReferences:(CDSymbolReferences *)symbolReferences;
{
if ([aClassDump shouldMatchRegex] == YES && [aClassDump regexMatchesString:[self name]] == NO)
return;
[resultString appendFormat:@"@protocol %@", name];
if ([protocols count] > 0) {
[resultString appendFormat:@" <%@>", [[protocols arrayByMappingSelector:@selector(name)] componentsJoinedByString:@", "]];
[symbolReferences addProtocolNamesFromArray:[protocols arrayByMappingSelector:@selector(name)]];
}
[resultString appendString:@"\n"];
[self appendMethodsToString:resultString classDump:aClassDump symbolReferences:symbolReferences];
[resultString appendString:@"@end\n\n"];
}
- (void)addMethodsToXMLElement:(NSXMLElement *)xmlElement classDump:(CDClassDump *)aClassDump symbolReferences:(CDSymbolReferences *)symbolReferences;
{
int count, index;
NSArray *methods;
if ([aClassDump shouldSortMethods] == YES)
methods = [classMethods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
else
methods = classMethods;
count = [methods count];
if (count > 0) {
for (index = 0; index < count; index++)
[[methods objectAtIndex:index] addToXMLElement:xmlElement asClassMethod:YES classDump:aClassDump symbolReferences:symbolReferences];
}
if ([aClassDump shouldSortMethods] == YES)
methods = [instanceMethods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
else
methods = instanceMethods;
count = [methods count];
if (count > 0) {
for (index = 0; index < count; index++) {
[[methods objectAtIndex:index] addToXMLElement:xmlElement asClassMethod:NO classDump:aClassDump symbolReferences:symbolReferences];
}
}
}
- (void)appendMethodsToString:(NSMutableString *)resultString classDump:(CDClassDump *)aClassDump symbolReferences:(CDSymbolReferences *)symbolReferences;
{
int count, index;
NSArray *methods;
if ([aClassDump shouldSortMethods] == YES)
methods = [classMethods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
else
methods = classMethods;
count = [methods count];
if (count > 0) {
for (index = 0; index < count; index++) {
[resultString appendString:@"+ "];
[[methods objectAtIndex:index] appendToString:resultString classDump:aClassDump symbolReferences:symbolReferences];
[resultString appendString:@"\n"];
}
}
if ([aClassDump shouldSortMethods] == YES)
methods = [instanceMethods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
else
methods = instanceMethods;
count = [methods count];
if (count > 0) {
for (index = 0; index < count; index++) {
[resultString appendString:@"- "];
[[methods objectAtIndex:index] appendToString:resultString classDump:aClassDump symbolReferences:symbolReferences];
[resultString appendString:@"\n"];
}
}
}
- (void)registerStructuresWithObject:(id <CDStructureRegistration>)anObject phase:(int)phase;
{
[self registerStructuresFromMethods:classMethods withObject:anObject phase:phase];
[self registerStructuresFromMethods:instanceMethods withObject:anObject phase:phase];
}
- (void)registerStructuresFromMethods:(NSArray *)methods withObject:(id <CDStructureRegistration>)anObject phase:(int)phase;
{
int count, index;
CDTypeParser *parser;
NSArray *methodTypes;
count = [methods count];
for (index = 0; index < count; index++) {
parser = [[CDTypeParser alloc] initWithType:[(CDOCMethod *)[methods objectAtIndex:index] type]];
methodTypes = [parser parseMethodType];
if (methodTypes == nil)
NSLog(@"Warning: Parsing method types failed, %@", [(CDOCMethod *)[methods objectAtIndex:index] name]);
[self registerStructuresFromMethodTypes:methodTypes withObject:anObject phase:phase];
[parser release];
}
}
- (void)registerStructuresFromMethodTypes:(NSArray *)methodTypes withObject:(id <CDStructureRegistration>)anObject phase:(int)phase;
{
int count, index;
count = [methodTypes count];
for (index = 0; index < count; index++) {
[[methodTypes objectAtIndex:index] registerStructuresWithObject:anObject phase:phase];
}
}
- (NSString *)sortableName;
{
return name;
}
- (NSComparisonResult)ascendingCompareByName:(CDOCProtocol *)otherProtocol;
{
return [[self sortableName] compare:[otherProtocol sortableName]];
}
@end