Mercurial > paraspace
annotate examples/inject_redir.py @ 121:7644cb633d4b
Synchronize value of fields of DEXFile.
- It keeps field value of header and maps consistence with latest
data layout.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sat, 06 Aug 2011 08:49:30 +0800 |
parents | 4f508f84c8a1 |
children | ff6f869273b7 |
rev | line source |
---|---|
119
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
1 ## \file |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
2 # |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
3 # This is an example of inject a class from source to destinate DEX, and |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
4 # rewrite all references of a specified class with the injected one and its |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
5 # methods. All calls of specified class will be redirected to the |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
6 # injected class. |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
7 # |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
8 from paraspace.dexfile import DEXFile, DEXFile_linked |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
9 from paraspace.dex_deptracker import prepare_dep_decls |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
10 from paraspace.dex_deptracker import restore_dependencies |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
11 from paraspace.injection import inject_classdefs, make_methodidxs_redir_map |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
12 from paraspace.injection import dexfile_redirect_types |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
13 import sys |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
14 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
15 if len(sys.argv) != 6: |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
16 print >> sys.stderr, \ |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
17 'usage: %s <src dex> <inject class> ' \ |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
18 '<dst dex> <redirect class> <output>' % \ |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
19 (sys.argv[0]) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
20 sys.exit(1) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
21 pass |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
22 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
23 src_dexname = sys.argv[1] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
24 inj_classname = sys.argv[2] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
25 dst_dexname = sys.argv[3] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
26 redir_classname = sys.argv[4] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
27 output_name = sys.argv[5] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
28 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
29 decls = prepare_dep_decls() |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
30 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
31 src_dex = DEXFile.open(src_dexname) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
32 src_linked = DEXFile_linked.build_dependencies(src_dex, decls) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
33 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
34 dst_dex = DEXFile.open(dst_dexname) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
35 dst_linked = DEXFile_linked.build_dependencies(dst_dex, decls) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
36 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
37 inj_classdef = src_linked.find_class_name(inj_classname) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
38 injected_classdefs = inject_classdefs(dst_linked, src_linked, [inj_classdef]) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
39 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
40 redir_typeid = dst_linked.find_typeid_name(redir_classname) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
41 redir_typeidx = dst_linked.get_idx_typeid(redir_typeid) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
42 inj_typeid = dst_linked.find_typeid_name(inj_classname) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
43 inj_typeidx = dst_linked.get_idx_typeid(inj_typeid) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
44 typeidxs_redir = {redir_typeidx: inj_typeidx} |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
45 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
46 methodidxs_redir = \ |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
47 make_methodidxs_redir_map(dst_linked, dst_linked, typeidxs_redir) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
48 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
49 injected_typeidxs = [dst_linked.get_idx_classdef(classdef) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
50 for classdef in injected_classdefs] |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
51 dexfile_redirect_types(dst_linked, typeidxs_redir, methodidxs_redir, |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
52 excludes=injected_typeidxs) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
53 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
54 restore_dependencies(dst_linked, decls) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
55 dst_linked.make_checksum() |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
56 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
57 restore_raw = dst_linked.to_str() |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
58 |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
59 ofile = open(output_name, 'w') |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
60 ofile.write(restore_raw) |
4f508f84c8a1
Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
61 ofile.close() |