From 867723e0098cf2b7ad3430bb348fc54ed4091744 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Mon, 25 Feb 2013 18:55:48 -0800 Subject: [PATCH] Annotate ClassDefItems --- .../dexlib2/dexbacked/DexBackedDexFile.java | 9 +- .../dexlib2/dexbacked/raw/ClassDefItem.java | 103 ++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java index f00d37a7..2a89cf62 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java @@ -77,7 +77,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile private final int classCount; private final int classStartOffset; - private static final int CLASS_DEF_ITEM_SIZE = 32; public static final int MAP_ITEM_SIZE = 12; public static final int TYPE_LIST_SIZE_OFFSET = 0; @@ -190,7 +189,7 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile if (classIndex < 0 || classIndex >= classCount) { throw new ExceptionWithContext("Class index out of bounds: %d", classIndex); } - return classStartOffset + classIndex*CLASS_DEF_ITEM_SIZE; + return classStartOffset + classIndex*ClassDefItem.ITEM_SIZE; } public int getClassCount() { @@ -273,6 +272,12 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount); } + if (classCount > 0) { + annotatedBytes.skipTo(getClassDefItemOffset(0)); + annotatedBytes.annotate(0, " "); + ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount); + } + annotatedBytes.writeAnnotations(out, buf); } } 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 new file mode 100644 index 00000000..449394d7 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java @@ -0,0 +1,103 @@ +/* + * 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 org.jf.dexlib2.dexbacked.DexBackedDexFile; +import org.jf.dexlib2.util.AnnotatedBytes; + +import javax.annotation.Nonnull; + +public class ClassDefItem { + public static final int ITEM_SIZE = 32; + + public static final int CLASS_OFFSET = 0; + public static final int ACCESS_FLAGS_OFFSET = 4; + public static final int SUPERCLASS_OFFSET = 8; + public static final int INTERFACES_OFFSET = 12; + public static final int SOURCE_FILE_OFFSET = 16; + public static final int ANNOTATIONS_OFFSET = 20; + public static final int CLASS_DATA_OFFSET = 24; + public static final int STATIC_VALUES_OFFSET = 28; + + @Nonnull + public static SectionAnnotator getAnnotator() { + return new SectionAnnotator() { + @Nonnull @Override public String getItemName() { + return "class_def_item"; + } + + @Override + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + int classIndex = dexFile.readSmallUint(out.getCursor()); + out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); + + int accessFlags = dexFile.readInt(out.getCursor()); + out.annotate(4, "access_flags = 0x%x", accessFlags); + + int superclassIndex = dexFile.readSmallUint(out.getCursor()); + out.annotate(4, "superclass_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, superclassIndex)); + + int interfacesOffset = dexFile.readSmallUint(out.getCursor()); + out.annotate(4, "interfaces_off = %s", TypeListItem.getReferenceAnnotation(dexFile, interfacesOffset)); + + int sourceFileIdx = dexFile.readOptionalUint(out.getCursor()); + if (sourceFileIdx == -1) { + out.annotate(4, "source_file_idx = -1"); + } else { + out.annotate(4, "source_file_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, + sourceFileIdx)); + } + + int annotationsOffset = dexFile.readSmallUint(out.getCursor()); + if (annotationsOffset == 0) { + out.annotate(4, "annotations_off = 0"); + } else { + out.annotate(4, "annotations_off = annotations_directory_item[0x%x]", annotationsOffset); + } + + int classDataOffset = dexFile.readSmallUint(out.getCursor()); + if (classDataOffset == 0) { + out.annotate(4, "class_data_off = 0"); + } else { + out.annotate(4, "class_data_off = class_data_item[0x%x]", classDataOffset); + } + + int staticValuesOffset = dexFile.readSmallUint(out.getCursor()); + if (staticValuesOffset == 0) { + out.annotate(4, "static_values_off = 0"); + } else { + out.annotate(4, "static_values_off = encoded_array_item[0x%x]", staticValuesOffset); + } + } + }; + } +}