1.1 --- a/classfile.py Sun Nov 21 00:20:48 2004 +0100
1.2 +++ b/classfile.py Sun Nov 21 23:21:30 2004 +0100
1.3 @@ -85,7 +85,24 @@
1.4
1.5 class PythonNameUtils:
1.6 def get_python_name(self):
1.7 - return str(self.get_name()).replace("/", ".")
1.8 + # NOTE: This may not be comprehensive.
1.9 + if not str(self.get_name()).startswith("["):
1.10 + return str(self.get_name()).replace("/", ".")
1.11 + else:
1.12 + return self._get_type_name(
1.13 + get_field_descriptor(
1.14 + str(self.get_name())
1.15 + )
1.16 + ).replace("/", ".")
1.17 +
1.18 + def _get_type_name(self, descriptor_type):
1.19 + base_type, object_type, array_type = descriptor_type
1.20 + if base_type == "L":
1.21 + return object_type
1.22 + elif base_type == "[":
1.23 + return self._get_type_name(array_type)
1.24 + else:
1.25 + return descriptor_base_type_mapping[base_type]
1.26
1.27 class NameUtils:
1.28 def get_name(self):
1.29 @@ -120,64 +137,61 @@
1.30 def get_class(self):
1.31 return self.class_file.constants[self.class_index - 1]
1.32
1.33 -class DescriptorUtils:
1.34 -
1.35 - "Symbol parsing."
1.36 +# Symbol parsing.
1.37
1.38 - def _get_method_descriptor(self, s):
1.39 - assert s[0] == "("
1.40 - params = []
1.41 - s = s[1:]
1.42 - while s[0] != ")":
1.43 - parameter_descriptor, s = self._get_parameter_descriptor(s)
1.44 - params.append(parameter_descriptor)
1.45 - if s[1] != "V":
1.46 - return_type, s = self._get_field_type(s[1:])
1.47 - else:
1.48 - return_type, s = None, s[1:]
1.49 - return params, return_type
1.50 +def get_method_descriptor(s):
1.51 + assert s[0] == "("
1.52 + params = []
1.53 + s = s[1:]
1.54 + while s[0] != ")":
1.55 + parameter_descriptor, s = _get_parameter_descriptor(s)
1.56 + params.append(parameter_descriptor)
1.57 + if s[1] != "V":
1.58 + return_type, s = _get_field_type(s[1:])
1.59 + else:
1.60 + return_type, s = None, s[1:]
1.61 + return params, return_type
1.62
1.63 - def _get_parameter_descriptor(self, s):
1.64 - return self._get_field_type(s)
1.65 +def get_field_descriptor(s):
1.66 + return _get_field_type(s)[0]
1.67
1.68 - def _get_field_descriptor(self, s):
1.69 - return self._get_field_type(s)
1.70 +def _get_parameter_descriptor(s):
1.71 + return _get_field_type(s)
1.72
1.73 - def _get_component_type(self, s):
1.74 - return self._get_field_type(s)
1.75 +def _get_component_type(s):
1.76 + return _get_field_type(s)
1.77
1.78 - def _get_field_type(self, s):
1.79 - base_type, s = self._get_base_type(s)
1.80 - object_type = None
1.81 - array_type = None
1.82 - if base_type == "L":
1.83 - object_type, s = self._get_object_type(s)
1.84 - elif base_type == "[":
1.85 - array_type, s = self._get_array_type(s)
1.86 - return (base_type, object_type, array_type), s
1.87 +def _get_field_type(s):
1.88 + base_type, s = _get_base_type(s)
1.89 + object_type = None
1.90 + array_type = None
1.91 + if base_type == "L":
1.92 + object_type, s = _get_object_type(s)
1.93 + elif base_type == "[":
1.94 + array_type, s = _get_array_type(s)
1.95 + return (base_type, object_type, array_type), s
1.96
1.97 - def _get_base_type(self, s):
1.98 - if len(s) > 0:
1.99 - return s[0], s[1:]
1.100 - else:
1.101 - return None, s
1.102 +def _get_base_type(s):
1.103 + if len(s) > 0:
1.104 + return s[0], s[1:]
1.105 + else:
1.106 + return None, s
1.107
1.108 - def _get_object_type(self, s):
1.109 - if len(s) > 0:
1.110 - s_end = s.find(";")
1.111 - assert s_end != -1
1.112 - return s[:s_end], s[s_end+1:]
1.113 - else:
1.114 - return None, s
1.115 +def _get_object_type(s):
1.116 + if len(s) > 0:
1.117 + s_end = s.find(";")
1.118 + assert s_end != -1
1.119 + return s[:s_end], s[s_end+1:]
1.120 + else:
1.121 + return None, s
1.122
1.123 - def _get_array_type(self, s):
1.124 - if len(s) > 0:
1.125 - return self._get_component_type(s)
1.126 - else:
1.127 - return None, s
1.128 +def _get_array_type(s):
1.129 + if len(s) > 0:
1.130 + return _get_component_type(s)
1.131 + else:
1.132 + return None, s
1.133
1.134 # Constant information.
1.135 -# Objects of these classes are not directly aware of the class they reside in.
1.136
1.137 class ClassInfo(NameUtils, PythonNameUtils):
1.138 def init(self, data, class_file):
1.139 @@ -203,7 +217,7 @@
1.140 class InterfaceMethodRefInfo(MethodRefInfo):
1.141 pass
1.142
1.143 -class NameAndTypeInfo(NameUtils, DescriptorUtils, PythonNameUtils):
1.144 +class NameAndTypeInfo(NameUtils, PythonNameUtils):
1.145 def init(self, data, class_file):
1.146 self.class_file = class_file
1.147 self.name_index = u2(data[0:2])
1.148 @@ -211,10 +225,10 @@
1.149 return data[4:]
1.150
1.151 def get_field_descriptor(self):
1.152 - return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.153 + return get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.154
1.155 def get_method_descriptor(self):
1.156 - return self._get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.157 + return get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.158
1.159 class Utf8Info:
1.160 def init(self, data, class_file):
1.161 @@ -279,7 +293,7 @@
1.162 # Other information.
1.163 # Objects of these classes are generally aware of the class they reside in.
1.164
1.165 -class ItemInfo(NameUtils, DescriptorUtils):
1.166 +class ItemInfo(NameUtils):
1.167 def init(self, data, class_file):
1.168 self.class_file = class_file
1.169 self.access_flags = u2(data[0:2])
1.170 @@ -290,11 +304,11 @@
1.171
1.172 class FieldInfo(ItemInfo, PythonNameUtils):
1.173 def get_descriptor(self):
1.174 - return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.175 + return get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.176
1.177 class MethodInfo(ItemInfo, PythonMethodUtils):
1.178 def get_descriptor(self):
1.179 - return self._get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.180 + return get_method_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.181
1.182 class AttributeInfo:
1.183 def init(self, data, class_file):
1.184 @@ -440,7 +454,7 @@
1.185 return data[10:]
1.186
1.187 def get_descriptor(self):
1.188 - return self._get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.189 + return get_field_descriptor(unicode(self.class_file.constants[self.descriptor_index - 1]))
1.190
1.191 # Exceptions.
1.192