changeset 110:6380730a80b4

Add collect_typeidxs_mentioned_by_class()
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 01 Aug 2011 15:00:29 +0800
parents 835336632aba
children 3820379b34e8
files paraspace/dexfile.py paraspace/injection.py paraspace/tests/injection_test.py
diffstat 3 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/dexfile.py	Mon Aug 01 14:37:04 2011 +0800
+++ b/paraspace/dexfile.py	Mon Aug 01 15:00:29 2011 +0800
@@ -1915,6 +1915,16 @@
         insns = code.insns.data
         return insns
 
+    ## \brief Return all method of given class definition.
+    @staticmethod
+    def get_methods_classdef(classdef):
+        if not classdef.classDataOffRef.is_true:
+            return []
+        classdata = classdef.classDataOffRef.value
+        methods = classdata.directMethods.items + \
+            classdata.virtualMethods.items
+        return methods
+
     ## \brief Dump content of a proto ID.
     @staticmethod
     def dump_protoid(protoid):
--- a/paraspace/injection.py	Mon Aug 01 14:37:04 2011 +0800
+++ b/paraspace/injection.py	Mon Aug 01 15:00:29 2011 +0800
@@ -369,6 +369,7 @@
 ## \brief Collect all type indices mentioned in the code of given method.
 #
 # \param method is a \ref _DEX_Method.
+# \return a list of type indices mentioned in the code.
 #
 def collect_typeidxs_in_method(dex, method):
     from paraspace.dexfile import _DEX_Method, DEXFile_linked
@@ -416,3 +417,19 @@
     typeidxs = list(chain(*types_insns))
     
     return typeidxs
+
+
+## \brief Collect all type indices mentioned by the code of given class.
+def collect_typeidxs_mentioned_by_class(dex, classdef):
+    from paraspace.dexfile import DEXFile_linked
+
+    assert isinstance(dex, DEXFile_linked)
+    
+    typeidxs = set()
+    methods = DEXFile_linked.get_methods_classdef(classdef)
+    for method in methods:
+        method_typeidxs = collect_typeidxs_in_method(dex, method)
+        typeidxs.update(method_typeidxs)
+        pass
+    
+    return list(typeidxs)
--- a/paraspace/tests/injection_test.py	Mon Aug 01 14:37:04 2011 +0800
+++ b/paraspace/tests/injection_test.py	Mon Aug 01 15:00:29 2011 +0800
@@ -177,6 +177,8 @@
 def collect_types_in_method_test():
     from paraspace.dex_deptracker import prepare_dep_decls
     from paraspace.injection import collect_typeidxs_in_method
+    from paraspace.injection import collect_typeidxs_mentioned_by_class
+    from paraspace.dexfile import DEXFile_linked
     
     _install_dexfile_4_deptracker()
     
@@ -198,4 +200,16 @@
 
     typeidxs = collect_typeidxs_in_method(fakefile_linked, init_method)
     assert len(typeidxs) == 1
+
+    typeid = fakefile_linked.find_typeid_idx(typeidxs[0])
+    typeid_name = DEXFile_linked.get_typeid_name(typeid)
+    assert typeid_name == 'Ljava/io/File;'
+    
+    typeidxs = collect_typeidxs_mentioned_by_class(fakefile_linked,
+                                                   fakefile_def)
+    assert len(typeidxs) == 1
+
+    typeid = fakefile_linked.find_typeid_idx(typeidxs[0])
+    typeid_name = DEXFile_linked.get_typeid_name(typeid)
+    assert typeid_name == 'Ljava/io/File;'
     pass