changeset 27:15cb829ac442

travel_dex_type() recoganizes _dex_type correctly
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 07 Jun 2011 22:30:29 +0800
parents b30a0d29a62f
children 5ce7ca1187b3
files paraspace/dex_deptracker.py paraspace/dexfile.py paraspace/tests/dexfile_test.py
diffstat 3 files changed, 70 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/dex_deptracker.py	Tue Jun 07 15:02:42 2011 +0800
+++ b/paraspace/dex_deptracker.py	Tue Jun 07 22:30:29 2011 +0800
@@ -36,15 +36,26 @@
 
 
 def _travel_dex_type(clazz, name_path):
-    if isinstance(clazz, _marker):
-        clazz = clazz.back_type
-        pass
+    def travelable(attr, attr_name):
+        if attr_name.startswith('_'):
+            return False
+        if callable(attr) and ((not isinstance(attr, dexfile._dex_type)) and
+                               (not (type(attr) == type and
+                                     issubclass(attr, dexfile._dex_type)))):
+            return False
+        return True
     
     travel_queue = [(getattr(clazz, attr_name), name_path + '.' + attr_name)
                     for attr_name in dir(clazz)
-                    if not attr_name.startswith('_')]
+                    if travelable(getattr(clazz, attr_name), attr_name)]
+    print travel_queue
     while travel_queue:
         attr, name_path = travel_queue.pop(0)
+        if isinstance(attr, _marker):
+            child = attr.back_type
+            travel_queue.append((child, name_path))
+            continue
+        
         yield attr, name_path
 
         if isinstance(attr, _marker):
@@ -427,7 +438,8 @@
                         if type(attr) == type and
                         issubclass(attr, dexfile.composite) and
                         attr in marked_types]
-    print marked_type_refs
+    import pprint
+    pprint.pprint(marked_type_refs)
     
     def patch_ref(name_path, depon_path):
         depon, depon_parent = _resolve_name_path(depon_path)
--- a/paraspace/dexfile.py	Tue Jun 07 15:02:42 2011 +0800
+++ b/paraspace/dexfile.py	Tue Jun 07 22:30:29 2011 +0800
@@ -116,7 +116,11 @@
     return total
 
 
-class _rawstr(object):
+class _dex_type(object):
+    pass
+
+
+class _rawstr(_dex_type):
     size = None
     factor = None
     data = None
@@ -168,7 +172,7 @@
     pass
 
 
-class tap(object):
+class tap(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         pass
@@ -183,7 +187,7 @@
     pass
 
 
-class uint32(object):
+class uint32(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v = _to_uint(data[off:off + 4])
@@ -200,7 +204,7 @@
     pass
 
 
-class uint16(object):
+class uint16(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v = _to_uint(data[off:off + 2])
@@ -216,7 +220,7 @@
     pass
 
 
-class uint8(object):
+class uint8(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v = _to_uint(data[off:off + 1])
@@ -232,7 +236,7 @@
     pass
 
 
-class int32(object):
+class int32(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v = _to_int(data[off:off + 4])
@@ -249,7 +253,7 @@
     pass
 
 
-class int16(object):
+class int16(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v = _to_int(data[off:off + 2])
@@ -265,7 +269,7 @@
     pass
 
 
-class uleb128(object):
+class uleb128(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v, sh = _uleb128(data[off:off + 5])
@@ -281,7 +285,7 @@
     pass
 
 
-class leb128(object):
+class leb128(_dex_type):
     @staticmethod
     def parse(parent, data, off):
         v, sh = _leb128(data[off:off + 5])
@@ -297,7 +301,7 @@
     pass
 
 
-class auto_align(object):
+class auto_align(_dex_type):
     bits = None
     
     def __init__(self, bits):
@@ -325,7 +329,7 @@
     return o.__class__.sizeof(o)
 
 
-class relocatable(object):
+class relocatable(_dex_type):
     data_size = None
 
     @staticmethod
@@ -1280,7 +1284,7 @@
     pass
     
 
-class dummy(object):
+class dummy(_dex_type):
     data_size = None
 
     @staticmethod
--- a/paraspace/tests/dexfile_test.py	Tue Jun 07 15:02:42 2011 +0800
+++ b/paraspace/tests/dexfile_test.py	Tue Jun 07 22:30:29 2011 +0800
@@ -131,19 +131,51 @@
 
 
 def travel_dex_type_test():
+    from paraspace.dex_deptracker import collect_all_dep_decls
+    from paraspace.dex_deptracker import _install_markers
     from paraspace.dex_deptracker import _travel_dex_type
+
+    _install_dexfile_4_deptracker()
     
     attr_infos = [attr_info
                   for attr_info in \
                       _travel_dex_type(dexfile._DEX_AnnotationsDirectoryItem,
                                        '_DEX_AnnotationsDirectoryItem')
                   ]
-    print attr_infos
-    assert len(attr_infos) == 10
+    attr_names = set([attr_info[1]
+                      for attr_info in attr_infos])
+    print attr_names
+    assert '_DEX_AnnotationsDirectoryItem.classAnnotationsOff' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldsSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.methodsSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.parametersSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldAnnotationsItems' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldAnnotationsItems.items.*' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.methodAnnotationsItems' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.parameterAnnotationsItems' in attr_names
+    
+    all_dep_decls = collect_all_dep_decls()
+    _install_markers(all_dep_decls)
+    
+    attr_infos = [attr_info
+                  for attr_info in \
+                      _travel_dex_type(dexfile._DEX_AnnotationsDirectoryItem,
+                                       '_DEX_AnnotationsDirectoryItem')
+                  ]
+    attr_names = set([attr_info[1]
+                      for attr_info in attr_infos])
+    assert '_DEX_AnnotationsDirectoryItem.classAnnotationsOff' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldsSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.methodsSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.parametersSize' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldAnnotationsItems' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.fieldAnnotationsItems.items.*' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.methodAnnotationsItems' in attr_names
+    assert '_DEX_AnnotationsDirectoryItem.parameterAnnotationsItems' in attr_names
     pass
 
 
-def install_markers_test():
+def _install_markers_test():
     from paraspace.dex_deptracker import collect_all_dep_decls
     from paraspace.dex_deptracker import _install_markers, _idx_marker
     from paraspace.dex_deptracker import _offset_marker, _rel_offset_marker
@@ -159,13 +191,14 @@
     assert dexfile._DEX_TypeList.__class__ == _offset_marker
 
     _patch_dex_type_markers(all_dep_decls)
+    assert isinstance(dexfile._DEX_TypeList, _offset_marker)
     print all_dep_decls.keys()
     print dexfile.DEXFile.typeLists.child_type
     assert isinstance(dexfile.DEXFile.typeLists.child_type, _offset_marker)
     pass
 
 
-def link_dependencies_test():
+def _link_dependencies_test():
     from paraspace.dex_deptracker import collect_all_dep_decls
     from paraspace.dex_deptracker import _link_dependencies
     from paraspace.dex_deptracker import _install_markers, _idx_marker