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