javaclass

Change of classfile.py

4:668cb12070c2
classfile.py
     1.1 --- a/classfile.py	Thu Oct 28 16:38:33 2004 +0200
     1.2 +++ b/classfile.py	Thu Oct 28 20:47:25 2004 +0200
     1.3 @@ -5,7 +5,7 @@
     1.4  http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
     1.5  """
     1.6  
     1.7 -import struct
     1.8 +import struct # for general decoding of class files
     1.9  
    1.10  # Utility functions.
    1.11  
    1.12 @@ -18,6 +18,18 @@
    1.13  def u4(data):
    1.14      return struct.unpack(">L", data[0:4])[0]
    1.15  
    1.16 +def s4(data):
    1.17 +    return struct.unpack(">l", data[0:4])[0]
    1.18 +
    1.19 +def s8(data):
    1.20 +    return struct.unpack(">q", data[0:8])[0]
    1.21 +
    1.22 +def f4(data):
    1.23 +    return struct.unpack(">f", data[0:4])[0]
    1.24 +
    1.25 +def f8(data):
    1.26 +    return struct.unpack(">d", data[0:8])[0]
    1.27 +
    1.28  # Useful mix-ins.
    1.29  
    1.30  class NameUtils:
    1.31 @@ -82,14 +94,16 @@
    1.32  class SmallNumInfo:
    1.33      def init(self, data, class_file):
    1.34          self.class_file = class_file
    1.35 -        self.bytes = u4(data[0:4])
    1.36 +        self.bytes = data[0:4]
    1.37          return data[4:]
    1.38  
    1.39  class IntegerInfo(SmallNumInfo):
    1.40 -    pass
    1.41 +    def get_value(self):
    1.42 +        return s4(self.bytes)
    1.43  
    1.44  class FloatInfo(SmallNumInfo):
    1.45 -    pass
    1.46 +    def get_value(self):
    1.47 +        return f4(self.bytes)
    1.48  
    1.49  class LargeNumInfo:
    1.50      def init(self, data, class_file):
    1.51 @@ -99,10 +113,12 @@
    1.52          return data[8:]
    1.53  
    1.54  class LongInfo(LargeNumInfo):
    1.55 -    pass
    1.56 +    def get_value(self):
    1.57 +        return s8(self.high_bytes + self.low_bytes)
    1.58  
    1.59  class DoubleInfo(LargeNumInfo):
    1.60 -    pass
    1.61 +    def get_value(self):
    1.62 +        return f8(self.high_bytes + self.low_bytes)
    1.63  
    1.64  # Other information.
    1.65  # Objects of these classes are generally aware of the class they reside in.
    1.66 @@ -195,11 +211,15 @@
    1.67  
    1.68  class ConstantValueAttributeInfo(AttributeInfo):
    1.69      def init(self, data, class_file):
    1.70 +        self.class_file = class_file
    1.71          self.attribute_length = u4(data[0:4])
    1.72          self.constant_value_index = u2(data[4:6])
    1.73          assert 4+self.attribute_length == 6
    1.74          return data[4+self.attribute_length:]
    1.75  
    1.76 +    def get_value(self):
    1.77 +        return self.class_file.constants[self.constant_value_index - 1].get_value()
    1.78 +
    1.79  class CodeAttributeInfo(AttributeInfo):
    1.80      def init(self, data, class_file):
    1.81          self.class_file = class_file