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