# HG changeset patch # User Thinker K.F. Li # Date 1312182029 -28800 # Node ID 6380730a80b43e79069bfc3e375c28d0c6f3dd90 # Parent 835336632aba8422eebd62e2af587f8efa52562b Add collect_typeidxs_mentioned_by_class() diff -r 835336632aba -r 6380730a80b4 paraspace/dexfile.py --- 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): diff -r 835336632aba -r 6380730a80b4 paraspace/injection.py --- 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) diff -r 835336632aba -r 6380730a80b4 paraspace/tests/injection_test.py --- 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