From cd12f13ffc2e67e674d82060076a450051b0371b Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Thu, 28 Feb 2013 23:02:22 -0800 Subject: [PATCH] Annotate ClassDataItems --- .../dexlib2/dexbacked/raw/ClassDataItem.java | 162 ++++++++++++++++++ .../dexlib2/dexbacked/raw/ClassDefItem.java | 26 +++ .../jf/dexlib2/dexbacked/raw/RawDexFile.java | 25 ++- .../dexbacked/raw/SectionAnnotator.java | 2 +- 4 files changed, 208 insertions(+), 7 deletions(-) create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDataItem.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDataItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDataItem.java new file mode 100644 index 00000000..1c98cf3d --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDataItem.java @@ -0,0 +1,162 @@ +/* + * Copyright 2013, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.dexbacked.raw; + +import com.google.common.base.Joiner; +import org.jf.dexlib2.AccessFlags; +import org.jf.dexlib2.dexbacked.DexReader; +import org.jf.dexlib2.util.AnnotatedBytes; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Map; + +public class ClassDataItem { + private abstract static class ClassDataItemAnnotator extends SectionAnnotator { + public ClassDataItemAnnotator() { + } + + @Nonnull @Override public String getItemName() { + return "class_data_item"; + } + + @Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { + DexReader reader = dexFile.readerAt(out.getCursor()); + + int mark = reader.getOffset(); + int staticFieldsSize = reader.readSmallUleb128(); + out.annotate(reader.getOffset() - mark, "static_fields_size = %d", staticFieldsSize); + + mark = reader.getOffset(); + int instanceFieldsSize = reader.readSmallUleb128(); + out.annotate(reader.getOffset() - mark, "instance_fields_size = %d", instanceFieldsSize); + + mark = reader.getOffset(); + int directMethodsSize = reader.readSmallUleb128(); + out.annotate(reader.getOffset() - mark, "direct_methods_size = %d", directMethodsSize); + + mark = reader.getOffset(); + int virtualMethodsSize = reader.readSmallUleb128(); + out.annotate(reader.getOffset() - mark, "virtual_methods_size = %d", virtualMethodsSize); + + int previousIndex = 0; + for (int i=0; i classTypeMap = ClassDefItem.getClassDataTypeMap(dexFile); + + SectionAnnotator annotator = new ClassDataItemAnnotator() { + @Nullable @Override + public String getItemIdentity(@Nonnull RawDexFile dexFile, int itemIndex, int itemOffset) { + if (classTypeMap != null) { + return classTypeMap.get(itemOffset); + } + return null; + } + }; + annotator.annotateSection(out, dexFile, itemCount); + } + }; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java index f93476d7..4ee758f1 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java @@ -32,10 +32,13 @@ package org.jf.dexlib2.dexbacked.raw; import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.util.AnnotatedBytes; import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Map; public class ClassDefItem { public static final int ITEM_SIZE = 32; @@ -102,4 +105,27 @@ public class ClassDefItem { } }; } + + @Nullable + public static Map getClassDataTypeMap(@Nonnull RawDexFile dexFile) { + MapItem classDefSection = dexFile.getMapItemForSection(ItemType.CLASS_DEF_ITEM); + if (classDefSection != null) { + int startOffset = classDefSection.getOffset(); + + ImmutableMap.Builder builder = ImmutableMap.builder(); + + for (int i=0; i getMapItems() { final int mapOffset = getMapOffset(); final int mapSize = readSmallUint(mapOffset); @@ -79,12 +90,14 @@ public class RawDexFile extends DexBackedDexFile.Impl { private static final Map annotators; static { - annotators = ImmutableMap.of( - ItemType.TYPE_LIST, TypeListItem.getAnnotator(), - ItemType.ANNOTATION_SET_REF_LIST, AnnotationSetRefList.getAnnotator(), - ItemType.MAP_LIST, MapItem.getAnnotator(), - ItemType.ANNOTATION_SET_ITEM, AnnotationSetItem.getAnnotator(), - ItemType.ANNOTATION_ITEM, AnnotationItem.getAnnotator()); + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.put(ItemType.TYPE_LIST, TypeListItem.getAnnotator()); + builder.put(ItemType.ANNOTATION_SET_REF_LIST, AnnotationSetRefList.getAnnotator()); + builder.put(ItemType.MAP_LIST, MapItem.getAnnotator()); + builder.put(ItemType.ANNOTATION_SET_ITEM, AnnotationSetItem.getAnnotator()); + builder.put(ItemType.ANNOTATION_ITEM, AnnotationItem.getAnnotator()); + builder.put(ItemType.CLASS_DATA_ITEM, ClassDataItem.getAnnotator()); + annotators = builder.build(); } public void dumpTo(Writer out, int width) throws IOException { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java index 087524e9..320f4488 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java @@ -60,7 +60,7 @@ public abstract class SectionAnnotator { for (int i=0; i