changeset 153:9a1677d03417

add lsclass
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 16 Aug 2011 20:25:02 +0800
parents bc213cb88636
children c659b8c7d6cb
files examples/lsclass.py paraspace/dexfile.py
diffstat 2 files changed, 95 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/lsclass.py	Tue Aug 16 20:25:02 2011 +0800
@@ -0,0 +1,88 @@
+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
--- a/paraspace/dexfile.py	Tue Aug 16 18:10:11 2011 +0800
+++ b/paraspace/dexfile.py	Tue Aug 16 20:25:02 2011 +0800
@@ -2362,6 +2362,13 @@
     ## \brief Return _DEX_FieldId of given index.
     def find_fieldid_idx(self, idx):
         return self.fieldIds.items[idx]
+    
+    @staticmethod
+    def get_fieldid_name(fieldid):
+        namestrid = fieldid.nameIdx
+        namestrdata = namestrid.stringDataOff
+        name_str = namestrdata.data.data
+        return name_str
     pass