comparison 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
comparison
equal deleted inserted replaced
152:bc213cb88636 153:9a1677d03417
1 class method(object):
2 name = None
3
4 def to_str(self):
5 return self.name
6 pass
7
8
9 class field(object):
10 name = None
11
12 def to_str(self):
13 return self.name
14 pass
15
16
17 class classinfo(object):
18 external = True
19 typeidx = None
20 name = None
21 methods = None
22 fields = None
23
24 def to_str(self):
25 methods = '\n '.join([method.to_str() for method in self.methods])
26 fields = '\n '.join([field.to_str() for field in self.fields])
27 r = 'class %s (0x%x)' % (self.name, self.typeidx)
28 if fields:
29 r = r + '\n FIELDS\n ' + fields
30 pass
31 if methods:
32 r = r + '\n METHODS\n ' + methods
33 pass
34 return r
35
36 def fill(self, dex):
37 def make_field(fieldid):
38 f = field()
39 f.name = DEXFile_linked.get_fieldid_name(fieldid)
40 return f
41
42 def make_method(methodid):
43 m = field()
44 m.name = DEXFile_linked.get_methodid_name(methodid)
45 return m
46
47 typeid = dex.typeIds.items[self.typeidx]
48 self.name = DEXFile_linked.get_typeid_name(typeid)
49
50 fields = [make_field(fieldid)
51 for fieldid in dex.fieldIds.items
52 if fieldid.classIdx == typeid]
53 self.fields = fields
54
55 methods = [make_method(methodid)
56 for methodid in dex.methodIds.items
57 if methodid.classIdx == typeid]
58 self.methods = methods
59 pass
60 pass
61
62
63 def lsclass(dex):
64 classes = [classinfo() for i in range(len(dex.typeIds.items))]
65 for typeid, clazz in map(None, dex.typeIds.items, classes):
66 clazz.typeidx = dex.get_idx_typeid(typeid)
67 clazz.fill(dex)
68 pass
69 return classes
70
71
72 if __name__ == '__main__':
73 from paraspace.dexfile import DEXFile, DEXFile_linked
74 from paraspace.dex_deptracker import prepare_dep_decls
75 from sys import argv
76
77 assert len(argv) == 2
78 fname = argv[1]
79 decls = prepare_dep_decls()
80 dex = DEXFile.open(fname)
81 linked = DEXFile_linked.build_dependencies(dex, decls)
82
83 classes = lsclass(linked)
84 for clazz in classes:
85 print clazz.to_str()
86 print
87 pass
88 pass