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