changeset 102:7fcd555d802b

Add the function for finding method with a name
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 27 Jul 2011 09:07:36 +0800
parents b2db11aed6b8
children 8a53e6f7f517
files paraspace/dexfile.py paraspace/tests/dexfile_test.py
diffstat 2 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/dexfile.py	Tue Jul 26 12:46:24 2011 +0800
+++ b/paraspace/dexfile.py	Wed Jul 27 09:07:36 2011 +0800
@@ -1741,6 +1741,34 @@
 
         clone = dexfile_insert_class(self, classdef)
         return clone
+
+    ## \brief Get name string of given method.
+    @staticmethod
+    def get_method_name(method):
+        methodid = method.methodIdx
+        namestrid = methodid.nameIdx
+        namestrdata = namestrid.stringDataOff
+        name_str = namestrdata.data.data
+        return name_str
+
+    ## \brief Find the method of given method name and class definition.
+    #
+    # \param method_name is the method name.
+    # \param classdef is a _DEX_ClassDef.
+    # \return the corresponding _DEX_Method of given method_name and classdef.
+    #
+    def find_method_name(self, method_name, classdef):
+        if not classdef.classDataOffRef.is_true:
+            return
+
+        classdata = classdef.classDataOffRef.value
+        for wmethod in classdata.directMethods.items + \
+                classdata.virtualMethods.items:
+            wmethod_name = DEXFile_linked.get_method_name(wmethod)
+            if method_name == wmethod_name:
+                return wmethod
+            pass
+        pass
     pass
 
 
--- a/paraspace/tests/dexfile_test.py	Tue Jul 26 12:46:24 2011 +0800
+++ b/paraspace/tests/dexfile_test.py	Wed Jul 27 09:07:36 2011 +0800
@@ -375,3 +375,29 @@
     # assert dex.header.checksum == saved_checksum
     assert dex.header.checksum == 0xc5efe529
     pass
+
+
+def find_method_name_test():
+    from paraspace.dex_deptracker import prepare_dep_decls
+    
+    _install_dexfile_4_deptracker()
+    
+    all_dep_decls = prepare_dep_decls()
+    
+    srcdir = os.path.dirname(__file__)
+    srcroot = os.path.join(srcdir, '..', '..')
+    
+    fakefile_fn = os.path.join(srcroot, 'data', 'fakefile.dex')
+    fakefile_dex = dexfile.DEXFile.open(fakefile_fn)
+    fakefile_linked = \
+        dexfile.DEXFile_linked. \
+        build_dependencies(fakefile_dex, all_dep_decls)
+    
+    fakefile_def = fakefile_linked. \
+        find_class_name('Lcom/codemud/fakefile/fakefile;')
+    fakefile_cstr = fakefile_linked.find_method_name('<init>', fakefile_def)
+    assert fakefile_cstr
+
+    method_name = dexfile.DEXFile_linked.get_method_name(fakefile_cstr)
+    assert method_name == '<init>'
+    pass