javaclass

Changeset

33:ef53ab16c02e
2004-11-11 Paul Boddie raw files shortlog changelog graph Added type information to method names.
classfile.py (file)
     1.1 --- a/classfile.py	Thu Nov 11 20:30:29 2004 +0100
     1.2 +++ b/classfile.py	Thu Nov 11 20:30:46 2004 +0100
     1.3 @@ -32,15 +32,35 @@
     1.4  
     1.5  # Useful mix-ins.
     1.6  
     1.7 -class PythonNameUtils:
     1.8 +class PythonMethodUtils:
     1.9      def get_python_name(self):
    1.10          name = self.get_name()
    1.11          if str(name) == "<init>":
    1.12 -            return "__init__"
    1.13 +            name = "__init__"
    1.14          else:
    1.15 -            return name
    1.16 +            name = str(name)
    1.17 +        return name + "$" + self._get_descriptor_as_name()
    1.18 +
    1.19 +    def _get_descriptor_as_name(self):
    1.20 +        l = []
    1.21 +        for descriptor_type in self.get_descriptor()[0]:
    1.22 +            l.append(self._get_type_as_name(descriptor_type))
    1.23 +        return "$".join(l)
    1.24  
    1.25 -class NameUtils(PythonNameUtils):
    1.26 +    def _get_type_as_name(self, descriptor_type, s=""):
    1.27 +        base_type, object_type, array_type = descriptor_type
    1.28 +        if base_type == "L":
    1.29 +            return object_type + s
    1.30 +        elif base_type == "[":
    1.31 +            return self._get_type_as_name(array_type, s + "[]")
    1.32 +        else:
    1.33 +            return "<" + base_type + ">" + s
    1.34 +
    1.35 +class PythonNameUtils:
    1.36 +    def get_python_name(self):
    1.37 +        return self.get_name()
    1.38 +
    1.39 +class NameUtils:
    1.40      def get_name(self):
    1.41          if self.name_index != 0:
    1.42              return self.class_file.constants[self.name_index - 1]
    1.43 @@ -48,7 +68,7 @@
    1.44              # Some name indexes are zero to indicate special conditions.
    1.45              return None
    1.46  
    1.47 -class NameAndTypeUtils(PythonNameUtils):
    1.48 +class NameAndTypeUtils:
    1.49      def get_name(self):
    1.50          if self.name_and_type_index != 0:
    1.51              return self.class_file.constants[self.name_and_type_index - 1].get_name()
    1.52 @@ -129,7 +149,7 @@
    1.53  # Constant information.
    1.54  # Objects of these classes are not directly aware of the class they reside in.
    1.55  
    1.56 -class ClassInfo(NameUtils):
    1.57 +class ClassInfo(NameUtils, PythonNameUtils):
    1.58      def init(self, data, class_file):
    1.59          self.class_file = class_file
    1.60          self.name_index = u2(data[0:2])
    1.61 @@ -142,19 +162,18 @@
    1.62          self.name_and_type_index = u2(data[2:4])
    1.63          return data[4:]
    1.64  
    1.65 -class FieldRefInfo(RefInfo):
    1.66 +class FieldRefInfo(RefInfo, PythonNameUtils):
    1.67      def get_descriptor(self):
    1.68          return RefInfo.get_field_descriptor(self)
    1.69  
    1.70 -class MethodRefInfo(RefInfo):
    1.71 +class MethodRefInfo(RefInfo, PythonMethodUtils):
    1.72      def get_descriptor(self):
    1.73          return RefInfo.get_method_descriptor(self)
    1.74  
    1.75 -class InterfaceMethodRefInfo(RefInfo):
    1.76 -    def get_descriptor(self):
    1.77 -        return RefInfo.get_method_descriptor(self)
    1.78 +class InterfaceMethodRefInfo(MethodRefInfo):
    1.79 +    pass
    1.80  
    1.81 -class NameAndTypeInfo(NameUtils, DescriptorUtils):
    1.82 +class NameAndTypeInfo(NameUtils, DescriptorUtils, PythonMethodUtils):
    1.83      def init(self, data, class_file):
    1.84          self.class_file = class_file
    1.85          self.name_index = u2(data[0:2])
    1.86 @@ -218,7 +237,7 @@
    1.87  # Other information.
    1.88  # Objects of these classes are generally aware of the class they reside in.
    1.89  
    1.90 -class ItemInfo(NameUtils, DescriptorUtils):
    1.91 +class ItemInfo(NameUtils, DescriptorUtils, PythonMethodUtils):
    1.92      def init(self, data, class_file):
    1.93          self.class_file = class_file
    1.94          self.access_flags = u2(data[0:2])
    1.95 @@ -243,7 +262,7 @@
    1.96  
    1.97  # NOTE: Decode the different attribute formats.
    1.98  
    1.99 -class SourceFileAttributeInfo(AttributeInfo, NameUtils):
   1.100 +class SourceFileAttributeInfo(AttributeInfo, NameUtils, PythonNameUtils):
   1.101      def init(self, data, class_file):
   1.102          self.class_file = class_file
   1.103          self.attribute_length = u4(data[0:4])
   1.104 @@ -367,7 +386,7 @@
   1.105          self.line_number = u2(data[2:4])
   1.106          return data[4:]
   1.107  
   1.108 -class LocalVariableInfo(NameUtils):
   1.109 +class LocalVariableInfo(NameUtils, PythonNameUtils):
   1.110      def init(self, data, class_file):
   1.111          self.class_file = class_file
   1.112          self.start_pc = u2(data[0:2])