view examples/lsclass.py @ 155:6570b15d12d4 tip

Fix _DEX_ProtoId.__cmp__() with correct logic rules
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 17 Aug 2011 09:27:22 +0800
parents 9a1677d03417
children
line wrap: on
line source

class method(object):
    name = None
    
    def to_str(self):
        return self.name
    pass


class field(object):
    name = None
    
    def to_str(self):
        return self.name
    pass


class classinfo(object):
    external = True
    typeidx = None
    name = None
    methods = None
    fields = None
    
    def to_str(self):
        methods = '\n    '.join([method.to_str() for method in self.methods])
        fields = '\n    '.join([field.to_str() for field in self.fields])
        r = 'class %s (0x%x)' % (self.name, self.typeidx)
        if fields:
            r = r + '\n  FIELDS\n    ' + fields
            pass
        if methods:
            r = r + '\n  METHODS\n    ' + methods
            pass
        return r

    def fill(self, dex):
        def make_field(fieldid):
            f = field()
            f.name = DEXFile_linked.get_fieldid_name(fieldid)
            return f

        def make_method(methodid):
            m = field()
            m.name = DEXFile_linked.get_methodid_name(methodid)
            return m

        typeid = dex.typeIds.items[self.typeidx]
        self.name = DEXFile_linked.get_typeid_name(typeid)
        
        fields = [make_field(fieldid)
                  for fieldid in dex.fieldIds.items
                  if fieldid.classIdx == typeid]
        self.fields = fields
        
        methods = [make_method(methodid)
                   for methodid in dex.methodIds.items
                   if methodid.classIdx == typeid]
        self.methods = methods
        pass
    pass


def lsclass(dex):
    classes = [classinfo() for i in range(len(dex.typeIds.items))]
    for typeid, clazz in map(None, dex.typeIds.items, classes):
        clazz.typeidx = dex.get_idx_typeid(typeid)
        clazz.fill(dex)
        pass
    return classes


if __name__ == '__main__':
    from paraspace.dexfile import DEXFile, DEXFile_linked
    from paraspace.dex_deptracker import prepare_dep_decls
    from sys import argv

    assert len(argv) == 2
    fname = argv[1]
    decls = prepare_dep_decls()
    dex = DEXFile.open(fname)
    linked = DEXFile_linked.build_dependencies(dex, decls)

    classes = lsclass(linked)
    for clazz in classes:
        print clazz.to_str()
        print
        pass
    pass