Mercurial > paraspace
view examples/lsclass.py @ 153:9a1677d03417
add lsclass
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 16 Aug 2011 20:25:02 +0800 |
parents | |
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