paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Java class file decoder. Specification found at the following URL: |
paul@0 | 5 | http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html |
paul@0 | 6 | """ |
paul@0 | 7 | |
paul@4 | 8 | import struct # for general decoding of class files |
paul@0 | 9 | |
paul@1 | 10 | # Utility functions. |
paul@1 | 11 | |
paul@1 | 12 | def u1(data): |
paul@1 | 13 | return struct.unpack(">B", data[0:1])[0] |
paul@1 | 14 | |
paul@1 | 15 | def u2(data): |
paul@1 | 16 | return struct.unpack(">H", data[0:2])[0] |
paul@1 | 17 | |
paul@1 | 18 | def u4(data): |
paul@1 | 19 | return struct.unpack(">L", data[0:4])[0] |
paul@1 | 20 | |
paul@4 | 21 | def s4(data): |
paul@4 | 22 | return struct.unpack(">l", data[0:4])[0] |
paul@4 | 23 | |
paul@4 | 24 | def s8(data): |
paul@4 | 25 | return struct.unpack(">q", data[0:8])[0] |
paul@4 | 26 | |
paul@4 | 27 | def f4(data): |
paul@4 | 28 | return struct.unpack(">f", data[0:4])[0] |
paul@4 | 29 | |
paul@4 | 30 | def f8(data): |
paul@4 | 31 | return struct.unpack(">d", data[0:8])[0] |
paul@4 | 32 | |
paul@2 | 33 | # Useful mix-ins. |
paul@2 | 34 | |
paul@2 | 35 | class NameUtils: |
paul@2 | 36 | def get_name(self): |
paul@2 | 37 | if self.name_index != 0: |
paul@10 | 38 | return self.class_file.constants[self.name_index - 1] |
paul@2 | 39 | else: |
paul@2 | 40 | # Some name indexes are zero to indicate special conditions. |
paul@2 | 41 | return None |
paul@2 | 42 | |
paul@7 | 43 | class NameAndTypeUtils: |
paul@7 | 44 | def get_name(self): |
paul@7 | 45 | if self.name_and_type_index != 0: |
paul@7 | 46 | return self.class_file.constants[self.name_and_type_index - 1].get_name() |
paul@7 | 47 | else: |
paul@7 | 48 | # Some name indexes are zero to indicate special conditions. |
paul@7 | 49 | return None |
paul@7 | 50 | |
paul@7 | 51 | def get_field_descriptor(self): |
paul@7 | 52 | if self.name_and_type_index != 0: |
paul@7 | 53 | return self.class_file.constants[self.name_and_type_index - 1].get_field_descriptor() |
paul@7 | 54 | else: |
paul@7 | 55 | # Some name indexes are zero to indicate special conditions. |
paul@7 | 56 | return None |
paul@7 | 57 | |
paul@7 | 58 | def get_method_descriptor(self): |
paul@7 | 59 | if self.name_and_type_index != 0: |
paul@7 | 60 | return self.class_file.constants[self.name_and_type_index - 1].get_method_descriptor() |
paul@7 | 61 | else: |
paul@7 | 62 | # Some name indexes are zero to indicate special conditions. |
paul@7 | 63 | return None |
paul@7 | 64 | |
paul@7 | 65 | class DescriptorUtils: |
paul@7 | 66 | |
paul@7 | 67 | "Symbol parsing." |
paul@7 | 68 | |
paul@7 | 69 | def _get_method_descriptor(self, s): |
paul@7 | 70 | assert s[0] == "(" |
paul@7 | 71 | params = [] |
paul@7 | 72 | s = s[1:] |
paul@7 | 73 | while s[0] != ")": |
paul@7 | 74 | parameter_descriptor, s = self._get_parameter_descriptor(s) |
paul@7 | 75 | params.append(parameter_descriptor) |
paul@7 | 76 | if s[1] != "V": |
paul@7 | 77 | return_type, s = self._get_field_type(s[1:]) |
paul@7 | 78 | else: |
paul@7 | 79 | return_type, s = None, s[1:] |
paul@7 | 80 | return params, return_type |
paul@7 | 81 | |
paul@7 | 82 | def _get_parameter_descriptor(self, s): |
paul@7 | 83 | return self._get_field_type(s) |
paul@7 | 84 | |
paul@7 | 85 | def _get_field_descriptor(self, s): |
paul@7 | 86 | return self._get_field_type(s) |
paul@7 | 87 | |
paul@7 | 88 | def _get_component_type(self, s): |
paul@7 | 89 | return self._get_field_type(s) |
paul@7 | 90 | |
paul@7 | 91 | def _get_field_type(self, s): |
paul@7 | 92 | base_type, s = self._get_base_type(s) |
paul@7 | 93 | object_type = None |
paul@7 | 94 | array_type = None |
paul@7 | 95 | if base_type == "L": |
paul@7 | 96 | object_type, s = self._get_object_type(s) |
paul@7 | 97 | elif base_type == "[": |
paul@7 | 98 | array_type, s = self._get_array_type(s) |
paul@7 | 99 | return (base_type, object_type, array_type), s |
paul@7 | 100 | |
paul@7 | 101 | def _get_base_type(self, s): |
paul@7 | 102 | if len(s) > 0: |
paul@7 | 103 | return s[0], s[1:] |
paul@7 | 104 | else: |
paul@7 | 105 | return None, s |
paul@7 | 106 | |
paul@7 | 107 | def _get_object_type(self, s): |
paul@7 | 108 | if len(s) > 0: |
paul@7 | 109 | s_end = s.find(";") |
paul@7 | 110 | assert s_end != -1 |
paul@7 | 111 | return s[:s_end], s[s_end+1:] |
paul@7 | 112 | else: |
paul@7 | 113 | return None, s |
paul@7 | 114 | |
paul@7 | 115 | def _get_array_type(self, s): |
paul@7 | 116 | if len(s) > 0: |
paul@7 | 117 | return self._get_component_type(s) |
paul@7 | 118 | else: |
paul@7 | 119 | return None, s |
paul@7 | 120 | |
paul@0 | 121 | # Constant information. |
paul@1 | 122 | # Objects of these classes are not directly aware of the class they reside in. |
paul@0 | 123 | |
paul@2 | 124 | class ClassInfo(NameUtils): |
paul@2 | 125 | def init(self, data, class_file): |
paul@2 | 126 | self.class_file = class_file |
paul@1 | 127 | self.name_index = u2(data[0:2]) |
paul@0 | 128 | return data[2:] |
paul@0 | 129 | |
paul@7 | 130 | class RefInfo(NameAndTypeUtils): |
paul@2 | 131 | def init(self, data, class_file): |
paul@2 | 132 | self.class_file = class_file |
paul@1 | 133 | self.class_index = u2(data[0:2]) |
paul@1 | 134 | self.name_and_type_index = u2(data[2:4]) |
paul@0 | 135 | return data[4:] |
paul@0 | 136 | |
paul@0 | 137 | class FieldRefInfo(RefInfo): |
paul@7 | 138 | def get_descriptor(self): |
paul@7 | 139 | return RefInfo.get_field_descriptor(self) |
paul@0 | 140 | |
paul@0 | 141 | class MethodRefInfo(RefInfo): |
paul@7 | 142 | def get_descriptor(self): |
paul@7 | 143 | return RefInfo.get_method_descriptor(self) |
paul@0 | 144 | |
paul@0 | 145 | class InterfaceMethodRefInfo(RefInfo): |
paul@7 | 146 | def get_descriptor(self): |
paul@7 | 147 | return RefInfo.get_method_descriptor(self) |
paul@0 | 148 | |
paul@7 | 149 | class NameAndTypeInfo(NameUtils, DescriptorUtils): |
paul@2 | 150 | def init(self, data, class_file): |
paul@2 | 151 | self.class_file = class_file |
paul@1 | 152 | self.name_index = u2(data[0:2]) |
paul@1 | 153 | self.descriptor_index = u2(data[2:4]) |
paul@0 | 154 | return data[4:] |
paul@0 | 155 | |
paul@7 | 156 | def get_field_descriptor(self): |
paul@7 | 157 | return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1])) |
paul@7 | 158 | |
paul@7 | 159 | def get_method_descriptor(self): |
paul@7 | 160 | return self._get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1])) |
paul@7 | 161 | |
paul@0 | 162 | class Utf8Info: |
paul@2 | 163 | def init(self, data, class_file): |
paul@2 | 164 | self.class_file = class_file |
paul@1 | 165 | self.length = u2(data[0:2]) |
paul@0 | 166 | self.bytes = data[2:2+self.length] |
paul@0 | 167 | return data[2+self.length:] |
paul@0 | 168 | |
paul@0 | 169 | def __str__(self): |
paul@0 | 170 | return self.bytes |
paul@0 | 171 | |
paul@0 | 172 | def __unicode__(self): |
paul@0 | 173 | return unicode(self.bytes, "utf-8") |
paul@0 | 174 | |
paul@0 | 175 | class StringInfo: |
paul@2 | 176 | def init(self, data, class_file): |
paul@2 | 177 | self.class_file = class_file |
paul@1 | 178 | self.string_index = u2(data[0:2]) |
paul@0 | 179 | return data[2:] |
paul@0 | 180 | |
paul@0 | 181 | class SmallNumInfo: |
paul@2 | 182 | def init(self, data, class_file): |
paul@2 | 183 | self.class_file = class_file |
paul@4 | 184 | self.bytes = data[0:4] |
paul@0 | 185 | return data[4:] |
paul@0 | 186 | |
paul@0 | 187 | class IntegerInfo(SmallNumInfo): |
paul@4 | 188 | def get_value(self): |
paul@4 | 189 | return s4(self.bytes) |
paul@0 | 190 | |
paul@0 | 191 | class FloatInfo(SmallNumInfo): |
paul@4 | 192 | def get_value(self): |
paul@4 | 193 | return f4(self.bytes) |
paul@0 | 194 | |
paul@0 | 195 | class LargeNumInfo: |
paul@2 | 196 | def init(self, data, class_file): |
paul@2 | 197 | self.class_file = class_file |
paul@1 | 198 | self.high_bytes = u4(data[0:4]) |
paul@1 | 199 | self.low_bytes = u4(data[4:8]) |
paul@0 | 200 | return data[8:] |
paul@0 | 201 | |
paul@0 | 202 | class LongInfo(LargeNumInfo): |
paul@4 | 203 | def get_value(self): |
paul@4 | 204 | return s8(self.high_bytes + self.low_bytes) |
paul@0 | 205 | |
paul@0 | 206 | class DoubleInfo(LargeNumInfo): |
paul@4 | 207 | def get_value(self): |
paul@4 | 208 | return f8(self.high_bytes + self.low_bytes) |
paul@0 | 209 | |
paul@0 | 210 | # Other information. |
paul@1 | 211 | # Objects of these classes are generally aware of the class they reside in. |
paul@0 | 212 | |
paul@7 | 213 | class ItemInfo(NameUtils, DescriptorUtils): |
paul@0 | 214 | def init(self, data, class_file): |
paul@0 | 215 | self.class_file = class_file |
paul@1 | 216 | self.access_flags = u2(data[0:2]) |
paul@1 | 217 | self.name_index = u2(data[2:4]) |
paul@1 | 218 | self.descriptor_index = u2(data[4:6]) |
paul@0 | 219 | self.attributes, data = self.class_file._get_attributes(data[6:]) |
paul@0 | 220 | return data |
paul@0 | 221 | |
paul@0 | 222 | class FieldInfo(ItemInfo): |
paul@0 | 223 | def get_descriptor(self): |
paul@0 | 224 | return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1])) |
paul@0 | 225 | |
paul@0 | 226 | class MethodInfo(ItemInfo): |
paul@0 | 227 | def get_descriptor(self): |
paul@0 | 228 | return self._get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1])) |
paul@0 | 229 | |
paul@0 | 230 | class AttributeInfo: |
paul@0 | 231 | def init(self, data, class_file): |
paul@1 | 232 | self.attribute_length = u4(data[0:4]) |
paul@0 | 233 | self.info = data[4:4+self.attribute_length] |
paul@0 | 234 | return data[4+self.attribute_length:] |
paul@0 | 235 | |
paul@0 | 236 | # NOTE: Decode the different attribute formats. |
paul@0 | 237 | |
paul@2 | 238 | class SourceFileAttributeInfo(AttributeInfo, NameUtils): |
paul@2 | 239 | def init(self, data, class_file): |
paul@2 | 240 | self.class_file = class_file |
paul@2 | 241 | self.attribute_length = u4(data[0:4]) |
paul@2 | 242 | # Permit the NameUtils mix-in. |
paul@2 | 243 | self.name_index = self.sourcefile_index = u2(data[4:6]) |
paul@0 | 244 | |
paul@0 | 245 | class ConstantValueAttributeInfo(AttributeInfo): |
paul@0 | 246 | def init(self, data, class_file): |
paul@4 | 247 | self.class_file = class_file |
paul@1 | 248 | self.attribute_length = u4(data[0:4]) |
paul@1 | 249 | self.constant_value_index = u2(data[4:6]) |
paul@0 | 250 | assert 4+self.attribute_length == 6 |
paul@0 | 251 | return data[4+self.attribute_length:] |
paul@0 | 252 | |
paul@4 | 253 | def get_value(self): |
paul@4 | 254 | return self.class_file.constants[self.constant_value_index - 1].get_value() |
paul@4 | 255 | |
paul@0 | 256 | class CodeAttributeInfo(AttributeInfo): |
paul@0 | 257 | def init(self, data, class_file): |
paul@0 | 258 | self.class_file = class_file |
paul@1 | 259 | self.attribute_length = u4(data[0:4]) |
paul@1 | 260 | self.max_stack = u2(data[4:6]) |
paul@1 | 261 | self.max_locals = u2(data[6:8]) |
paul@1 | 262 | self.code_length = u4(data[8:12]) |
paul@0 | 263 | end_of_code = 12+self.code_length |
paul@0 | 264 | self.code = data[12:end_of_code] |
paul@1 | 265 | self.exception_table_length = u2(data[end_of_code:end_of_code+2]) |
paul@0 | 266 | self.exception_table = [] |
paul@0 | 267 | data = data[end_of_code + 2:] |
paul@0 | 268 | for i in range(0, self.exception_table_length): |
paul@0 | 269 | exception = ExceptionInfo() |
paul@0 | 270 | data = exception.init(data) |
paul@2 | 271 | self.exception_table.append(exception) |
paul@0 | 272 | self.attributes, data = self.class_file._get_attributes(data) |
paul@0 | 273 | return data |
paul@0 | 274 | |
paul@0 | 275 | class ExceptionsAttributeInfo(AttributeInfo): |
paul@1 | 276 | def init(self, data, class_file): |
paul@1 | 277 | self.class_file = class_file |
paul@1 | 278 | self.attribute_length = u4(data[0:4]) |
paul@1 | 279 | self.number_of_exceptions = u2(data[4:6]) |
paul@1 | 280 | self.exception_index_table = [] |
paul@1 | 281 | index = 6 |
paul@1 | 282 | for i in range(0, self.number_of_exceptions): |
paul@1 | 283 | self.exception_index_table.append(u2(data[index:index+2])) |
paul@1 | 284 | index += 2 |
paul@1 | 285 | return data[index:] |
paul@1 | 286 | |
paul@1 | 287 | def get_exception(self, i): |
paul@1 | 288 | exception_index = self.exception_index_table[i] |
paul@1 | 289 | return self.class_file.constants[exception_index - 1] |
paul@0 | 290 | |
paul@0 | 291 | class InnerClassesAttributeInfo(AttributeInfo): |
paul@2 | 292 | def init(self, data, class_file): |
paul@2 | 293 | self.class_file = class_file |
paul@2 | 294 | self.attribute_length = u4(data[0:4]) |
paul@2 | 295 | self.number_of_classes = u2(data[4:6]) |
paul@2 | 296 | self.classes = [] |
paul@2 | 297 | data = data[6:] |
paul@2 | 298 | for i in range(0, self.number_of_classes): |
paul@2 | 299 | inner_class = InnerClassInfo() |
paul@2 | 300 | data = inner_class.init(data, self.class_file) |
paul@2 | 301 | self.classes.append(inner_class) |
paul@2 | 302 | return data |
paul@0 | 303 | |
paul@0 | 304 | class SyntheticAttributeInfo(AttributeInfo): |
paul@0 | 305 | pass |
paul@0 | 306 | |
paul@0 | 307 | class LineNumberAttributeInfo(AttributeInfo): |
paul@2 | 308 | def init(self, data, class_file): |
paul@2 | 309 | self.class_file = class_file |
paul@2 | 310 | self.attribute_length = u4(data[0:4]) |
paul@2 | 311 | self.line_number_table_length = u2(data[4:6]) |
paul@2 | 312 | self.line_number_table = [] |
paul@2 | 313 | data = data[6:] |
paul@2 | 314 | for i in range(0, self.line_number_table_length): |
paul@2 | 315 | line_number = LineNumberInfo() |
paul@2 | 316 | data = line_number.init(data) |
paul@2 | 317 | self.line_number_table.append(line_number) |
paul@2 | 318 | return data |
paul@0 | 319 | |
paul@0 | 320 | class LocalVariableAttributeInfo(AttributeInfo): |
paul@2 | 321 | def init(self, data, class_file): |
paul@2 | 322 | self.class_file = class_file |
paul@2 | 323 | self.attribute_length = u4(data[0:4]) |
paul@2 | 324 | self.local_variable_table_length = u2(data[4:6]) |
paul@2 | 325 | self.local_variable_table = [] |
paul@2 | 326 | data = data[6:] |
paul@2 | 327 | for i in range(0, self.local_variable_table_length): |
paul@2 | 328 | local_variable = LocalVariableInfo() |
paul@2 | 329 | data = local_variable.init(data) |
paul@2 | 330 | self.local_variable_table.append(local_variable) |
paul@2 | 331 | return data |
paul@0 | 332 | |
paul@0 | 333 | class DeprecatedAttributeInfo(AttributeInfo): |
paul@0 | 334 | pass |
paul@0 | 335 | |
paul@2 | 336 | # Child classes of the attribute information classes. |
paul@2 | 337 | |
paul@0 | 338 | class ExceptionInfo: |
paul@0 | 339 | def init(self, data): |
paul@1 | 340 | self.start_pc = u2(data[0:2]) |
paul@1 | 341 | self.end_pc = u2(data[2:4]) |
paul@1 | 342 | self.handler_pc = u2(data[4:6]) |
paul@1 | 343 | self.catch_type = u2(data[6:8]) |
paul@0 | 344 | return data[8:] |
paul@0 | 345 | |
paul@2 | 346 | class InnerClassInfo(NameUtils): |
paul@2 | 347 | def init(self, data, class_file): |
paul@2 | 348 | self.class_file = class_file |
paul@2 | 349 | self.inner_class_info_index = u2(data[0:2]) |
paul@2 | 350 | self.outer_class_info_index = u2(data[2:4]) |
paul@2 | 351 | # Permit the NameUtils mix-in. |
paul@2 | 352 | self.name_index = self.inner_name_index = u2(data[4:6]) |
paul@2 | 353 | self.inner_class_access_flags = u2(data[6:8]) |
paul@2 | 354 | return data[8:] |
paul@2 | 355 | |
paul@2 | 356 | class LineNumberInfo: |
paul@2 | 357 | def init(self, data): |
paul@2 | 358 | self.start_pc = u2(data[0:2]) |
paul@2 | 359 | self.line_number = u2(data[2:4]) |
paul@2 | 360 | return data[4:] |
paul@2 | 361 | |
paul@2 | 362 | class LocalVariableInfo(NameUtils): |
paul@2 | 363 | def init(self, data, class_file): |
paul@2 | 364 | self.class_file = class_file |
paul@2 | 365 | self.start_pc = u2(data[0:2]) |
paul@2 | 366 | self.length = u2(data[2:4]) |
paul@2 | 367 | self.name_index = u2(data[4:6]) |
paul@2 | 368 | self.descriptor_index = u2(data[6:8]) |
paul@2 | 369 | self.index = u2(data[8:10]) |
paul@2 | 370 | return data[10:] |
paul@2 | 371 | |
paul@2 | 372 | def get_descriptor(self): |
paul@2 | 373 | return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1])) |
paul@2 | 374 | |
paul@2 | 375 | # Exceptions. |
paul@2 | 376 | |
paul@0 | 377 | class UnknownTag(Exception): |
paul@0 | 378 | pass |
paul@0 | 379 | |
paul@0 | 380 | class UnknownAttribute(Exception): |
paul@0 | 381 | pass |
paul@0 | 382 | |
paul@0 | 383 | # Abstractions for the main structures. |
paul@0 | 384 | |
paul@0 | 385 | class ClassFile: |
paul@0 | 386 | |
paul@0 | 387 | "A class representing a Java class file." |
paul@0 | 388 | |
paul@0 | 389 | def __init__(self, s): |
paul@0 | 390 | |
paul@0 | 391 | """ |
paul@0 | 392 | Process the given string 's', populating the object with the class |
paul@0 | 393 | file's details. |
paul@0 | 394 | """ |
paul@0 | 395 | |
paul@0 | 396 | self.constants, s = self._get_constants(s[8:]) |
paul@0 | 397 | self.access_flags, s = self._get_access_flags(s) |
paul@0 | 398 | self.this_class, s = self._get_this_class(s) |
paul@0 | 399 | self.super_class, s = self._get_super_class(s) |
paul@0 | 400 | self.interfaces, s = self._get_interfaces(s) |
paul@0 | 401 | self.fields, s = self._get_fields(s) |
paul@0 | 402 | self.methods, s = self._get_methods(s) |
paul@0 | 403 | self.attributes, s = self._get_attributes(s) |
paul@0 | 404 | |
paul@0 | 405 | def _decode_const(self, s): |
paul@1 | 406 | tag = u1(s[0:1]) |
paul@0 | 407 | if tag == 1: |
paul@0 | 408 | const = Utf8Info() |
paul@0 | 409 | elif tag == 3: |
paul@0 | 410 | const = IntegerInfo() |
paul@0 | 411 | elif tag == 4: |
paul@0 | 412 | const = FloatInfo() |
paul@0 | 413 | elif tag == 5: |
paul@0 | 414 | const = LongInfo() |
paul@0 | 415 | elif tag == 6: |
paul@0 | 416 | const = DoubleInfo() |
paul@0 | 417 | elif tag == 7: |
paul@0 | 418 | const = ClassInfo() |
paul@0 | 419 | elif tag == 8: |
paul@0 | 420 | const = StringInfo() |
paul@0 | 421 | elif tag == 9: |
paul@0 | 422 | const = FieldRefInfo() |
paul@0 | 423 | elif tag == 10: |
paul@0 | 424 | const = MethodRefInfo() |
paul@0 | 425 | elif tag == 11: |
paul@0 | 426 | const = InterfaceMethodRefInfo() |
paul@0 | 427 | elif tag == 12: |
paul@0 | 428 | const = NameAndTypeInfo() |
paul@0 | 429 | else: |
paul@0 | 430 | raise UnknownTag, tag |
paul@2 | 431 | |
paul@2 | 432 | # Initialise the constant object. |
paul@2 | 433 | |
paul@2 | 434 | s = const.init(s[1:], self) |
paul@0 | 435 | return const, s |
paul@0 | 436 | |
paul@0 | 437 | def _get_constants_from_table(self, count, s): |
paul@0 | 438 | l = [] |
paul@0 | 439 | # Have to skip certain entries specially. |
paul@0 | 440 | i = 1 |
paul@0 | 441 | while i < count: |
paul@0 | 442 | c, s = self._decode_const(s) |
paul@0 | 443 | l.append(c) |
paul@0 | 444 | # Add a blank entry after "large" entries. |
paul@0 | 445 | if isinstance(c, LargeNumInfo): |
paul@0 | 446 | l.append(None) |
paul@0 | 447 | i += 1 |
paul@0 | 448 | i += 1 |
paul@0 | 449 | return l, s |
paul@0 | 450 | |
paul@0 | 451 | def _get_items_from_table(self, cls, number, s): |
paul@0 | 452 | l = [] |
paul@0 | 453 | for i in range(0, number): |
paul@0 | 454 | f = cls() |
paul@0 | 455 | s = f.init(s, self) |
paul@0 | 456 | l.append(f) |
paul@0 | 457 | return l, s |
paul@0 | 458 | |
paul@0 | 459 | def _get_methods_from_table(self, number, s): |
paul@0 | 460 | return self._get_items_from_table(MethodInfo, number, s) |
paul@0 | 461 | |
paul@0 | 462 | def _get_fields_from_table(self, number, s): |
paul@0 | 463 | return self._get_items_from_table(FieldInfo, number, s) |
paul@0 | 464 | |
paul@0 | 465 | def _get_attribute_from_table(self, s): |
paul@1 | 466 | attribute_name_index = u2(s[0:2]) |
paul@0 | 467 | constant_name = self.constants[attribute_name_index - 1].bytes |
paul@0 | 468 | if constant_name == "SourceFile": |
paul@0 | 469 | attribute = SourceFileAttributeInfo() |
paul@0 | 470 | elif constant_name == "ConstantValue": |
paul@0 | 471 | attribute = ConstantValueAttributeInfo() |
paul@0 | 472 | elif constant_name == "Code": |
paul@0 | 473 | attribute = CodeAttributeInfo() |
paul@0 | 474 | elif constant_name == "Exceptions": |
paul@0 | 475 | attribute = ExceptionsAttributeInfo() |
paul@0 | 476 | elif constant_name == "InnerClasses": |
paul@0 | 477 | attribute = InnerClassesAttributeInfo() |
paul@0 | 478 | elif constant_name == "Synthetic": |
paul@0 | 479 | attribute = SyntheticAttributeInfo() |
paul@0 | 480 | elif constant_name == "LineNumberTable": |
paul@0 | 481 | attribute = LineNumberAttributeInfo() |
paul@0 | 482 | elif constant_name == "LocalVariableTable": |
paul@0 | 483 | attribute = LocalVariableAttributeInfo() |
paul@0 | 484 | elif constant_name == "Deprecated": |
paul@0 | 485 | attribute = DeprecatedAttributeInfo() |
paul@0 | 486 | else: |
paul@0 | 487 | raise UnknownAttribute, constant_name |
paul@0 | 488 | s = attribute.init(s[2:], self) |
paul@0 | 489 | return attribute, s |
paul@0 | 490 | |
paul@0 | 491 | def _get_attributes_from_table(self, number, s): |
paul@0 | 492 | attributes = [] |
paul@0 | 493 | for i in range(0, number): |
paul@0 | 494 | attribute, s = self._get_attribute_from_table(s) |
paul@0 | 495 | attributes.append(attribute) |
paul@0 | 496 | return attributes, s |
paul@0 | 497 | |
paul@0 | 498 | def _get_constants(self, s): |
paul@1 | 499 | count = u2(s[0:2]) |
paul@0 | 500 | return self._get_constants_from_table(count, s[2:]) |
paul@0 | 501 | |
paul@0 | 502 | def _get_access_flags(self, s): |
paul@1 | 503 | return u2(s[0:2]), s[2:] |
paul@0 | 504 | |
paul@0 | 505 | def _get_this_class(self, s): |
paul@1 | 506 | index = u2(s[0:2]) |
paul@0 | 507 | return self.constants[index - 1], s[2:] |
paul@0 | 508 | |
paul@0 | 509 | _get_super_class = _get_this_class |
paul@0 | 510 | |
paul@0 | 511 | def _get_interfaces(self, s): |
paul@0 | 512 | interfaces = [] |
paul@1 | 513 | number = u2(s[0:2]) |
paul@0 | 514 | s = s[2:] |
paul@0 | 515 | for i in range(0, number): |
paul@1 | 516 | index = u2(s[0:2]) |
paul@0 | 517 | interfaces.append(self.constants[index - 1]) |
paul@0 | 518 | s = s[2:] |
paul@0 | 519 | return interfaces, s |
paul@0 | 520 | |
paul@0 | 521 | def _get_fields(self, s): |
paul@1 | 522 | number = u2(s[0:2]) |
paul@0 | 523 | return self._get_fields_from_table(number, s[2:]) |
paul@0 | 524 | |
paul@0 | 525 | def _get_attributes(self, s): |
paul@1 | 526 | number = u2(s[0:2]) |
paul@0 | 527 | return self._get_attributes_from_table(number, s[2:]) |
paul@0 | 528 | |
paul@0 | 529 | def _get_methods(self, s): |
paul@1 | 530 | number = u2(s[0:2]) |
paul@0 | 531 | return self._get_methods_from_table(number, s[2:]) |
paul@0 | 532 | |
paul@0 | 533 | if __name__ == "__main__": |
paul@0 | 534 | import sys |
paul@0 | 535 | f = open(sys.argv[1]) |
paul@0 | 536 | c = ClassFile(f.read()) |
paul@0 | 537 | |
paul@0 | 538 | # vim: tabstop=4 expandtab shiftwidth=4 |