OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #import "CDOCProperty.h" |
| 7 |
| 8 #import "NSString-Extensions.h" |
| 9 #import "CDTypeParser.h" |
| 10 #import "CDTypeLexer.h" |
| 11 |
| 12 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Ar
ticles/ocrtPropertyIntrospection.html |
| 13 |
| 14 static BOOL debug = NO; |
| 15 |
| 16 @implementation CDOCProperty |
| 17 |
| 18 - (id)initWithName:(NSString *)aName attributes:(NSString *)someAttributes; |
| 19 { |
| 20 if ([super init] == nil) |
| 21 return nil; |
| 22 |
| 23 name = [aName retain]; |
| 24 attributeString = [someAttributes retain]; |
| 25 type = nil; |
| 26 attributes = [[NSMutableArray alloc] init]; |
| 27 |
| 28 hasParsedAttributes = NO; |
| 29 attributeStringAfterType = nil; |
| 30 customGetter = nil; |
| 31 customSetter = nil; |
| 32 |
| 33 flags.isReadOnly = NO; |
| 34 flags.isDynamic = NO; |
| 35 |
| 36 [self _parseAttributes]; |
| 37 |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (void)dealloc; |
| 42 { |
| 43 [name release]; |
| 44 [attributeString release]; |
| 45 [type release]; |
| 46 [attributes release]; |
| 47 |
| 48 [attributeStringAfterType release]; |
| 49 [customGetter release]; |
| 50 [customSetter release]; |
| 51 |
| 52 [super dealloc]; |
| 53 } |
| 54 |
| 55 - (NSString *)name; |
| 56 { |
| 57 return name; |
| 58 } |
| 59 |
| 60 - (NSString *)attributeString; |
| 61 { |
| 62 return attributeString; |
| 63 } |
| 64 |
| 65 - (CDType *)type; |
| 66 { |
| 67 return type; |
| 68 } |
| 69 |
| 70 - (NSArray *)attributes; |
| 71 { |
| 72 return attributes; |
| 73 } |
| 74 |
| 75 - (NSString *)attributeStringAfterType; |
| 76 { |
| 77 return attributeStringAfterType; |
| 78 } |
| 79 |
| 80 - (void)_setAttributeStringAfterType:(NSString *)newValue; |
| 81 { |
| 82 if (newValue == attributeStringAfterType) |
| 83 return; |
| 84 |
| 85 [attributeStringAfterType release]; |
| 86 attributeStringAfterType = [newValue retain]; |
| 87 } |
| 88 |
| 89 - (NSString *)defaultGetter; |
| 90 { |
| 91 return name; |
| 92 } |
| 93 |
| 94 - (NSString *)defaultSetter; |
| 95 { |
| 96 return [NSString stringWithFormat:@"set%@:", [name capitalizeFirstCharacter]
]; |
| 97 } |
| 98 |
| 99 - (NSString *)customGetter; |
| 100 { |
| 101 return customGetter; |
| 102 } |
| 103 |
| 104 - (void)_setCustomGetter:(NSString *)newStr; |
| 105 { |
| 106 if (newStr == customGetter) |
| 107 return; |
| 108 |
| 109 [customGetter release]; |
| 110 customGetter = [newStr retain]; |
| 111 } |
| 112 |
| 113 - (NSString *)customSetter; |
| 114 { |
| 115 return customSetter; |
| 116 } |
| 117 |
| 118 - (void)_setCustomSetter:(NSString *)newStr; |
| 119 { |
| 120 if (newStr == customSetter) |
| 121 return; |
| 122 |
| 123 [customSetter release]; |
| 124 customSetter = [newStr retain]; |
| 125 } |
| 126 |
| 127 - (NSString *)getter; |
| 128 { |
| 129 if (customGetter != nil) |
| 130 return customGetter; |
| 131 |
| 132 return [self defaultGetter]; |
| 133 } |
| 134 |
| 135 - (NSString *)setter; |
| 136 { |
| 137 if (customSetter != nil) |
| 138 return customSetter; |
| 139 |
| 140 return [self defaultSetter]; |
| 141 } |
| 142 |
| 143 - (BOOL)isReadOnly; |
| 144 { |
| 145 return flags.isReadOnly; |
| 146 } |
| 147 |
| 148 - (BOOL)isDynamic; |
| 149 { |
| 150 return flags.isDynamic; |
| 151 } |
| 152 |
| 153 - (NSString *)description; |
| 154 { |
| 155 return [NSString stringWithFormat:@"<%@:%p> name: %@, attributeString: %@", |
| 156 NSStringFromClass([self class]), self, |
| 157 name, attributeString]; |
| 158 } |
| 159 |
| 160 - (NSComparisonResult)ascendingCompareByName:(CDOCProperty *)otherProperty; |
| 161 { |
| 162 return [name compare:[otherProperty name]]; |
| 163 } |
| 164 |
| 165 // TODO (2009-07-09): Really, I don't need to require the "T" at the start. |
| 166 - (void)_parseAttributes; |
| 167 { |
| 168 NSScanner *scanner; |
| 169 |
| 170 // On 10.6, Finder's TTaskErrorViewController class has a property with a na
sty C++ type. I just knew someone would make this difficult. |
| 171 scanner = [[NSScanner alloc] initWithString:attributeString]; |
| 172 |
| 173 if ([scanner scanString:@"T" intoString:NULL]) { |
| 174 NSError *error; |
| 175 NSRange typeRange; |
| 176 CDTypeParser *parser; |
| 177 |
| 178 typeRange.location = [scanner scanLocation]; |
| 179 parser = [[CDTypeParser alloc] initWithType:[[scanner string] substringF
romIndex:[scanner scanLocation]]]; |
| 180 type = [[parser parseType:&error] retain]; |
| 181 if (type != nil) { |
| 182 NSString *str; |
| 183 |
| 184 typeRange.length = [[[parser lexer] scanner] scanLocation]; |
| 185 |
| 186 str = [attributeString substringFromIndex:NSMaxRange(typeRange)]; |
| 187 |
| 188 // Filter out so we don't get an empty string as an attribute. |
| 189 if ([str hasPrefix:@","]) |
| 190 str = [str substringFromIndex:1]; |
| 191 |
| 192 [self _setAttributeStringAfterType:str]; |
| 193 if ([attributeStringAfterType length] > 0) { |
| 194 [attributes addObjectsFromArray:[attributeStringAfterType compon
entsSeparatedByString:@","]]; |
| 195 } else { |
| 196 // For a simple case like "Ti", we'd get the empty string. |
| 197 // Then, using componentsSeparatedByString:, since it has no sep
arator we'd get back an array containing the (empty) string |
| 198 } |
| 199 } |
| 200 |
| 201 [parser release]; |
| 202 } else { |
| 203 if (debug) NSLog(@"Error: Property attributes should begin with the type
('T') attribute, property name: %@", name); |
| 204 } |
| 205 |
| 206 [scanner release]; |
| 207 |
| 208 for (NSString *attr in attributes) { |
| 209 if ([attr hasPrefix:@"R"]) |
| 210 flags.isReadOnly = YES; |
| 211 else if ([attr hasPrefix:@"D"]) |
| 212 flags.isDynamic = YES; |
| 213 else if ([attr hasPrefix:@"G"]) |
| 214 [self _setCustomGetter:[attr substringFromIndex:1]]; |
| 215 else if ([attr hasPrefix:@"S"]) |
| 216 [self _setCustomSetter:[attr substringFromIndex:1]]; |
| 217 } |
| 218 |
| 219 hasParsedAttributes = YES; |
| 220 // And then if parsedType is nil, we know we couldn't parse the type. |
| 221 } |
| 222 |
| 223 @end |
OLD | NEW |