Refactor how annotatations are handled a bit, and add annotations for TypeListItems

This commit is contained in:
Ben Gruver 2013-02-26 22:18:32 -08:00
parent 867723e009
commit ff32d5cc14
12 changed files with 368 additions and 104 deletions

View File

@ -41,7 +41,6 @@ public class dump {
if (dumpFileName != null) { if (dumpFileName != null) {
FileWriter writer = null; FileWriter writer = null;
try { try {
writer = new FileWriter(dumpFileName); writer = new FileWriter(dumpFileName);
@ -50,7 +49,7 @@ public class dump {
consoleWidth = 120; consoleWidth = 120;
} }
dexFile.dumpTo(writer, consoleWidth); dexFile.asRaw().dumpTo(writer, consoleWidth);
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("There was an error while dumping the dex file to " + dumpFileName); System.err.println("There was an error while dumping the dex file to " + dumpFileName);
ex.printStackTrace(System.err); ex.printStackTrace(System.err);

View File

@ -34,14 +34,11 @@ package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.raw.*; import org.jf.dexlib2.dexbacked.raw.*;
import org.jf.dexlib2.dexbacked.util.FixedSizeSet; import org.jf.dexlib2.dexbacked.util.FixedSizeSet;
import org.jf.dexlib2.iface.DexFile; import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.util.AnnotatedBytes;
import org.jf.util.ExceptionWithContext; import org.jf.util.ExceptionWithContext;
import org.jf.util.Utf8Utils; import org.jf.util.Utf8Utils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException;
import java.io.Writer;
import java.util.Set; import java.util.Set;
public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile { public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile {
@ -61,7 +58,9 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
@Override @Nonnull public abstract DexReader readerAt(int offset); @Override @Nonnull public abstract DexReader readerAt(int offset);
public abstract void dumpTo(Writer out, int width) throws IOException; @Nonnull public RawDexFile asRaw() {
return new RawDexFile(buf);
}
public static class Impl extends DexBackedDexFile { public static class Impl extends DexBackedDexFile {
private final int stringCount; private final int stringCount;
@ -77,11 +76,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
private final int classCount; private final int classCount;
private final int classStartOffset; private final int classStartOffset;
public static final int MAP_ITEM_SIZE = 12;
public static final int TYPE_LIST_SIZE_OFFSET = 0;
public static final int TYPE_LIST_LIST_OFFSET = 4;
public Impl(@Nonnull byte[] buf) { public Impl(@Nonnull byte[] buf) {
super(buf); super(buf);
@ -237,48 +231,5 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
public DexReader readerAt(int offset) { public DexReader readerAt(int offset) {
return new DexReader(this, offset); return new DexReader(this, offset);
} }
public void dumpTo(Writer out, int width) throws IOException {
AnnotatedBytes annotatedBytes = new AnnotatedBytes(width);
HeaderItem.getAnnotator().annotateSection(annotatedBytes, this, 1);
if (stringCount > 0) {
annotatedBytes.skipTo(getStringIdItemOffset(0));
annotatedBytes.annotate(0, " ");
StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount);
}
if (typeCount > 0) {
annotatedBytes.skipTo(getTypeIdItemOffset(0));
annotatedBytes.annotate(0, " ");
TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount);
}
if (protoCount > 0) {
annotatedBytes.skipTo(getProtoIdItemOffset(0));
annotatedBytes.annotate(0, " ");
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount);
}
if (fieldCount > 0) {
annotatedBytes.skipTo(getFieldIdItemOffset(0));
annotatedBytes.annotate(0, " ");
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount);
}
if (methodCount > 0) {
annotatedBytes.skipTo(getMethodIdItemOffset(0));
annotatedBytes.annotate(0, " ");
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);
}
} }
} }

View File

@ -34,6 +34,7 @@ package org.jf.dexlib2.dexbacked;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import org.jf.dexlib2.base.reference.BaseMethodReference; import org.jf.dexlib2.base.reference.BaseMethodReference;
import org.jf.dexlib2.dexbacked.raw.TypeListItem;
import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory; import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory;
import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.dexbacked.util.ParameterIterator; import org.jf.dexlib2.dexbacked.util.ParameterIterator;
@ -161,9 +162,8 @@ public class DexBackedMethod extends BaseMethodReference implements Method {
public List<String> getParameterTypes() { public List<String> getParameterTypes() {
final int parametersOffset = getParametersOffset(); final int parametersOffset = getParametersOffset();
if (parametersOffset > 0) { if (parametersOffset > 0) {
final int parameterCount = dexFile.readSmallUint(parametersOffset + final int parameterCount = dexFile.readSmallUint(parametersOffset + TypeListItem.SIZE_OFFSET);
DexBackedDexFile.Impl.TYPE_LIST_SIZE_OFFSET); final int paramListStart = parametersOffset + TypeListItem.LIST_OFFSET;
final int paramListStart = parametersOffset + DexBackedDexFile.Impl.TYPE_LIST_LIST_OFFSET;
return new FixedSizeList<String>() { return new FixedSizeList<String>() {
@Nonnull @Nonnull
@Override @Override

View File

@ -31,6 +31,8 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import com.google.common.base.Joiner;
import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
@ -61,7 +63,8 @@ public class ClassDefItem {
out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));
int accessFlags = dexFile.readInt(out.getCursor()); int accessFlags = dexFile.readInt(out.getCursor());
out.annotate(4, "access_flags = 0x%x", accessFlags); out.annotate(4, "access_flags = 0x%x: %s", accessFlags,
Joiner.on('|').join(AccessFlags.getAccessFlagsForClass(accessFlags)));
int superclassIndex = dexFile.readSmallUint(out.getCursor()); int superclassIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "superclass_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, superclassIndex)); out.annotate(4, "superclass_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, superclassIndex));

View File

@ -52,6 +52,8 @@ public class HeaderItem {
public static final int SIGNATURE_OFFSET = 12; public static final int SIGNATURE_OFFSET = 12;
public static final int SIGNATURE_SIZE = 20; public static final int SIGNATURE_SIZE = 20;
public static final int HEADER_SIZE_OFFSET = 36;
public static final int ENDIAN_TAG_OFFSET = 40; public static final int ENDIAN_TAG_OFFSET = 40;
public static final int MAP_OFFSET = 52; public static final int MAP_OFFSET = 52;
@ -74,6 +76,68 @@ public class HeaderItem {
public static final int CLASS_COUNT_OFFSET = 96; public static final int CLASS_COUNT_OFFSET = 96;
public static final int CLASS_START_OFFSET = 100; public static final int CLASS_START_OFFSET = 100;
@Nonnull private DexBackedDexFile dexFile;
public HeaderItem(@Nonnull DexBackedDexFile dexFile) {
this.dexFile = dexFile;
}
public int getMapOffset() {
return dexFile.readSmallUint(MAP_OFFSET);
}
public int getHeaderSize() {
return dexFile.readSmallUint(HEADER_SIZE_OFFSET);
}
public int getStringCount() {
return dexFile.readSmallUint(STRING_COUNT_OFFSET);
}
public int getStringOffset() {
return dexFile.readSmallUint(STRING_START_OFFSET);
}
public int getTypeCount() {
return dexFile.readSmallUint(TYPE_COUNT_OFFSET);
}
public int getTypeOffset() {
return dexFile.readSmallUint(TYPE_START_OFFSET);
}
public int getProtoCount() {
return dexFile.readSmallUint(PROTO_COUNT_OFFSET);
}
public int getProtoOffset() {
return dexFile.readSmallUint(PROTO_START_OFFSET);
}
public int getFieldCount() {
return dexFile.readSmallUint(FIELD_COUNT_OFFSET);
}
public int getFieldOffset() {
return dexFile.readSmallUint(FIELD_START_OFFSET);
}
public int getMethodCount() {
return dexFile.readSmallUint(METHOD_COUNT_OFFSET);
}
public int getMethodOffset() {
return dexFile.readSmallUint(METHOD_START_OFFSET);
}
public int getClassCount() {
return dexFile.readSmallUint(CLASS_COUNT_OFFSET);
}
public int getClassOffset() {
return dexFile.readSmallUint(CLASS_START_OFFSET);
}
@Nonnull @Nonnull
public static SectionAnnotator getAnnotator() { public static SectionAnnotator getAnnotator() {
return new SectionAnnotator() { return new SectionAnnotator() {

View File

@ -29,9 +29,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.jf.dexlib2.writer; package org.jf.dexlib2.dexbacked.raw;
public class DexItemType { import javax.annotation.Nonnull;
public class ItemType {
public static final int HEADER_ITEM = 0x0000; public static final int HEADER_ITEM = 0x0000;
public static final int STRING_ID_ITEM = 0x0001; public static final int STRING_ID_ITEM = 0x0001;
public static final int TYPE_ID_ITEM = 0x0002; public static final int TYPE_ID_ITEM = 0x0002;
@ -51,27 +53,28 @@ public class DexItemType {
public static final int ENCODED_ARRAY_ITEM = 0x2005; public static final int ENCODED_ARRAY_ITEM = 0x2005;
public static final int ANNOTATION_DIRECTORY_ITEM = 0x2006; public static final int ANNOTATION_DIRECTORY_ITEM = 0x2006;
@Nonnull
public static String getItemTypeName(int itemType) { public static String getItemTypeName(int itemType) {
switch (itemType) { switch (itemType) {
case HEADER_ITEM: return "HEADER_ITEM"; case HEADER_ITEM: return "header_item";
case STRING_ID_ITEM: return "STRING_ID_ITEM"; case STRING_ID_ITEM: return "string_id_item";
case TYPE_ID_ITEM: return "TYPE_ID_ITEM"; case TYPE_ID_ITEM: return "type_id_item";
case PROTO_ID_ITEM: return "PROTO_ID_ITEM"; case PROTO_ID_ITEM: return "proto_id_item";
case FIELD_ID_ITEM: return "FIELD_ID_ITEM"; case FIELD_ID_ITEM: return "field_id_item";
case METHOD_ID_ITEM: return "METHOD_ID_ITEM"; case METHOD_ID_ITEM: return "method_id_item";
case CLASS_DEF_ITEM: return "CLASS_DEF_ITEM"; case CLASS_DEF_ITEM: return "class_def_item";
case MAP_LIST: return "MAP_LIST"; case MAP_LIST: return "map_list";
case TYPE_LIST: return "TYPE_LIST"; case TYPE_LIST: return "type_list";
case ANNOTATION_SET_REF_LIST: return "ANNOTATION_SET_REF_LIST"; case ANNOTATION_SET_REF_LIST: return "annotation_set_ref_list";
case ANNOTATION_SET_ITEM: return "ANNOTATION_SET_ITEM"; case ANNOTATION_SET_ITEM: return "annotation_set_item";
case CLASS_DATA_ITEM: return "CLASS_DATA_ITEM"; case CLASS_DATA_ITEM: return "class_data_item";
case CODE_ITEM: return "CODE_ITEM"; case CODE_ITEM: return "code_item";
case STRING_DATA_ITEM: return "STRING_DATA_ITEM"; case STRING_DATA_ITEM: return "string_data_item";
case DEBUG_INFO_ITEM: return "DEBUG_INFO_ITEM"; case DEBUG_INFO_ITEM: return "debug_info_item";
case ANNOTATION_ITEM: return "ANNOTATION_ITEM"; case ANNOTATION_ITEM: return "annotation_item";
case ENCODED_ARRAY_ITEM: return "ENCODED_ARRAY_ITEM"; case ENCODED_ARRAY_ITEM: return "encoded_array_item";
case ANNOTATION_DIRECTORY_ITEM: return "ANNOTATION_DIRECTORY_ITEM"; case ANNOTATION_DIRECTORY_ITEM: return "annotation_directory_item";
default: return "Unknown dex item type"; default: return "unknown dex item type";
} }
} }
} }

View File

@ -0,0 +1,69 @@
/*
* 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 javax.annotation.Nonnull;
public class MapItem {
public static final int ITEM_SIZE = 12;
public static final int TYPE_OFFSET = 0;
public static final int SIZE_OFFSET = 4;
public static final int OFFSET_OFFSET = 8;
private final DexBackedDexFile dexFile;
private final int offset;
public MapItem(DexBackedDexFile dexFile, int offset) {
this.dexFile = dexFile;
this.offset = offset;
}
public int getType() {
return dexFile.readUshort(offset + TYPE_OFFSET);
}
@Nonnull
public String getName() {
return ItemType.getItemTypeName(getType());
}
public int getItemCount() {
return dexFile.readSmallUint(offset + SIZE_OFFSET);
}
public int getOffset() {
return dexFile.readSmallUint(offset + OFFSET_OFFSET);
}
}

View File

@ -0,0 +1,138 @@
/*
* 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.collect.ImmutableMap;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;
public class RawDexFile {
@Nonnull private final byte[] buf;
@Nonnull public final DexBackedDexFile dexFile;
@Nonnull public final HeaderItem headerItem;
public RawDexFile(byte[] buf) {
this.buf = buf;
this.dexFile = new DexBackedDexFile.Impl(buf);
this.headerItem = new HeaderItem(dexFile);
}
public int getMapOffset() {
return headerItem.getMapOffset();
}
public List<MapItem> getMapItems() {
final int mapOffset = getMapOffset();
final int mapSize = dexFile.readSmallUint(mapOffset);
return new FixedSizeList<MapItem>() {
@Override
public MapItem readItem(int index) {
int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE;
return new MapItem(dexFile, mapItemOffset);
}
@Override public int size() {
return mapSize;
}
};
}
private static final Map<Integer, SectionAnnotator> annotators;
static {
annotators = ImmutableMap.of(ItemType.TYPE_LIST, TypeListItem.getAnnotator());
}
public void dumpTo(Writer out, int width) throws IOException {
AnnotatedBytes annotatedBytes = new AnnotatedBytes(width);
HeaderItem.getAnnotator().annotateSection(annotatedBytes, dexFile, 1);
int stringCount = headerItem.getStringCount();
if (stringCount > 0) {
annotatedBytes.skipTo(headerItem.getStringOffset());
annotatedBytes.annotate(0, " ");
StringIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, stringCount);
}
int typeCount = headerItem.getTypeCount();
if (typeCount > 0) {
annotatedBytes.skipTo(headerItem.getTypeOffset());
annotatedBytes.annotate(0, " ");
TypeIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, typeCount);
}
int protoCount = headerItem.getProtoCount();
if (protoCount > 0) {
annotatedBytes.skipTo(headerItem.getProtoOffset());
annotatedBytes.annotate(0, " ");
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, protoCount);
}
int fieldCount = headerItem.getFieldCount();
if (fieldCount > 0) {
annotatedBytes.skipTo(headerItem.getFieldOffset());
annotatedBytes.annotate(0, " ");
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, fieldCount);
}
int methodCount = headerItem.getMethodCount();
if (methodCount > 0) {
annotatedBytes.skipTo(headerItem.getMethodOffset());
annotatedBytes.annotate(0, " ");
MethodIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, methodCount);
}
int classCount = headerItem.getClassCount();
if (classCount > 0) {
annotatedBytes.skipTo(headerItem.getClassOffset());
annotatedBytes.annotate(0, " ");
ClassDefItem.getAnnotator().annotateSection(annotatedBytes, dexFile, classCount);
}
for (MapItem mapItem: getMapItems()) {
SectionAnnotator annotator = annotators.get(mapItem.getType());
if (annotator != null) {
annotatedBytes.skipTo(mapItem.getOffset());
annotator.annotateSection(annotatedBytes, dexFile, mapItem.getItemCount());
}
}
annotatedBytes.writeAnnotations(out, buf);
}
}

View File

@ -33,6 +33,7 @@ package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import org.jf.util.AlignmentUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -46,17 +47,20 @@ public abstract class SectionAnnotator {
* *
* @param out The AnnotatedBytes object to annotate to * @param out The AnnotatedBytes object to annotate to
* @param dexFile The DexBackedDexFile representing the dex file being annotated * @param dexFile The DexBackedDexFile representing the dex file being annotated
* @param length The number of items in the section (from the header/map) * @param itemCount The number of items in the section (from the header/map)
*/ */
public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int length) { public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemCount) {
String itemName = getItemName(); String itemName = getItemName();
if (length > 0) { int itemAlignment = getItemAlignment();
if (itemCount > 0) {
out.annotate(0, "-----------------------------"); out.annotate(0, "-----------------------------");
out.annotate(0, "%s section", itemName); out.annotate(0, "%s section", itemName);
out.annotate(0, "-----------------------------"); out.annotate(0, "-----------------------------");
out.annotate(0, ""); out.annotate(0, "");
for (int i=0; i<length; i++) { for (int i=0; i<itemCount; i++) {
out.skipTo(AlignmentUtils.alignOffset(out.getCursor(), itemAlignment));
String itemIdentity = getItemIdentity(dexFile, out.getCursor(), i); String itemIdentity = getItemIdentity(dexFile, out.getCursor(), i);
if (itemIdentity != null) { if (itemIdentity != null) {
out.annotate(0, "[%d] %s: %s", i, itemName, itemIdentity); out.annotate(0, "[%d] %s: %s", i, itemName, itemIdentity);
@ -73,4 +77,8 @@ public abstract class SectionAnnotator {
@Nullable public String getItemIdentity(@Nonnull DexBackedDexFile dexFile, int itemIndex, int itemOffset) { @Nullable public String getItemIdentity(@Nonnull DexBackedDexFile dexFile, int itemIndex, int itemOffset) {
return null; return null;
} }
public int getItemAlignment() {
return 1;
}
} }

View File

@ -32,10 +32,38 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class TypeListItem { public class TypeListItem {
public static final int SIZE_OFFSET = 0;
public static final int LIST_OFFSET = 4;
@Nonnull
public static SectionAnnotator getAnnotator() {
return new SectionAnnotator() {
@Nonnull @Override public String getItemName() {
return "type_list";
}
@Override
protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) {
int size = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "size: %d", size);
for (int i=0; i<size; i++) {
int typeIndex = dexFile.readUshort(out.getCursor());
out.annotate(2, TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));
}
}
@Override public int getItemAlignment() {
return 4;
}
};
}
@Nonnull @Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeListOffset) { public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
if (typeListOffset == 0) { if (typeListOffset == 0) {

View File

@ -36,6 +36,7 @@ import org.jf.dexlib2.base.reference.BaseMethodReference;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.raw.MethodIdItem; import org.jf.dexlib2.dexbacked.raw.MethodIdItem;
import org.jf.dexlib2.dexbacked.raw.ProtoIdItem; import org.jf.dexlib2.dexbacked.raw.ProtoIdItem;
import org.jf.dexlib2.dexbacked.raw.TypeListItem;
import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -69,9 +70,8 @@ public class DexBackedMethodReference extends BaseMethodReference {
int protoIdItemOffset = getProtoIdItemOffset(); int protoIdItemOffset = getProtoIdItemOffset();
final int parametersOffset = dexFile.readSmallUint(protoIdItemOffset + ProtoIdItem.PARAMETERS_OFFSET); final int parametersOffset = dexFile.readSmallUint(protoIdItemOffset + ProtoIdItem.PARAMETERS_OFFSET);
if (parametersOffset > 0) { if (parametersOffset > 0) {
final int parameterCount = dexFile.readSmallUint(parametersOffset + final int parameterCount = dexFile.readSmallUint(parametersOffset + TypeListItem.SIZE_OFFSET);
DexBackedDexFile.Impl.TYPE_LIST_SIZE_OFFSET); final int paramListStart = parametersOffset + TypeListItem.LIST_OFFSET;
final int paramListStart = parametersOffset + DexBackedDexFile.Impl.TYPE_LIST_LIST_OFFSET;
return new FixedSizeList<String>() { return new FixedSizeList<String>() {
@Nonnull @Nonnull
@Override @Override

View File

@ -31,6 +31,7 @@
package org.jf.dexlib2.writer; package org.jf.dexlib2.writer;
import org.jf.dexlib2.dexbacked.raw.ItemType;
import org.jf.util.ExceptionWithContext; import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -59,26 +60,26 @@ public class MapItem {
writer.writeInt(numItems); writer.writeInt(numItems);
// index section // index section
writeItem(writer, DexItemType.HEADER_ITEM, 1, 0); writeItem(writer, ItemType.HEADER_ITEM, 1, 0);
writeItem(writer, DexItemType.STRING_ID_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getIndexSectionOffset()); writeItem(writer, ItemType.STRING_ID_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getIndexSectionOffset());
writeItem(writer, DexItemType.TYPE_ID_ITEM, dexFile.typePool.getNumItems(), dexFile.typePool.getSectionOffset()); writeItem(writer, ItemType.TYPE_ID_ITEM, dexFile.typePool.getNumItems(), dexFile.typePool.getSectionOffset());
writeItem(writer, DexItemType.PROTO_ID_ITEM, dexFile.protoPool.getNumItems(), dexFile.protoPool.getSectionOffset()); writeItem(writer, ItemType.PROTO_ID_ITEM, dexFile.protoPool.getNumItems(), dexFile.protoPool.getSectionOffset());
writeItem(writer, DexItemType.FIELD_ID_ITEM, dexFile.fieldPool.getNumItems(), dexFile.fieldPool.getSectionOffset()); writeItem(writer, ItemType.FIELD_ID_ITEM, dexFile.fieldPool.getNumItems(), dexFile.fieldPool.getSectionOffset());
writeItem(writer, DexItemType.METHOD_ID_ITEM, dexFile.methodPool.getNumItems(), dexFile.methodPool.getSectionOffset()); writeItem(writer, ItemType.METHOD_ID_ITEM, dexFile.methodPool.getNumItems(), dexFile.methodPool.getSectionOffset());
writeItem(writer, DexItemType.CLASS_DEF_ITEM, dexFile.classDefPool.getNumClassDefItems(), dexFile.classDefPool.getIndexSectionOffset()); writeItem(writer, ItemType.CLASS_DEF_ITEM, dexFile.classDefPool.getNumClassDefItems(), dexFile.classDefPool.getIndexSectionOffset());
// data section // data section
writeItem(writer, DexItemType.STRING_DATA_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getDataSectionOffset()); writeItem(writer, ItemType.STRING_DATA_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getDataSectionOffset());
writeItem(writer, DexItemType.TYPE_LIST, dexFile.typeListPool.getNumItems(), dexFile.typeListPool.getSectionOffset()); writeItem(writer, ItemType.TYPE_LIST, dexFile.typeListPool.getNumItems(), dexFile.typeListPool.getSectionOffset());
writeItem(writer, DexItemType.ENCODED_ARRAY_ITEM, dexFile.encodedArrayPool.getNumItems(), dexFile.encodedArrayPool.getSectionOffset()); writeItem(writer, ItemType.ENCODED_ARRAY_ITEM, dexFile.encodedArrayPool.getNumItems(), dexFile.encodedArrayPool.getSectionOffset());
writeItem(writer, DexItemType.ANNOTATION_ITEM, dexFile.annotationPool.getNumItems(), dexFile.annotationPool.getSectionOffset()); writeItem(writer, ItemType.ANNOTATION_ITEM, dexFile.annotationPool.getNumItems(), dexFile.annotationPool.getSectionOffset());
writeItem(writer, DexItemType.ANNOTATION_SET_ITEM, dexFile.annotationSetPool.getNumItems(), dexFile.annotationSetPool.getSectionOffset()); writeItem(writer, ItemType.ANNOTATION_SET_ITEM, dexFile.annotationSetPool.getNumItems(), dexFile.annotationSetPool.getSectionOffset());
writeItem(writer, DexItemType.ANNOTATION_SET_REF_LIST, dexFile.annotationSetRefPool.getNumItems(), dexFile.annotationSetRefPool.getSectionOffset()); writeItem(writer, ItemType.ANNOTATION_SET_REF_LIST, dexFile.annotationSetRefPool.getNumItems(), dexFile.annotationSetRefPool.getSectionOffset());
writeItem(writer, DexItemType.ANNOTATION_DIRECTORY_ITEM, dexFile.annotationDirectoryPool.getNumItems(), dexFile.annotationDirectoryPool.getSectionOffset()); writeItem(writer, ItemType.ANNOTATION_DIRECTORY_ITEM, dexFile.annotationDirectoryPool.getNumItems(), dexFile.annotationDirectoryPool.getSectionOffset());
writeItem(writer, DexItemType.DEBUG_INFO_ITEM, dexFile.debugInfoPool.getNumItems(), dexFile.debugInfoPool.getSectionOffset()); writeItem(writer, ItemType.DEBUG_INFO_ITEM, dexFile.debugInfoPool.getNumItems(), dexFile.debugInfoPool.getSectionOffset());
writeItem(writer, DexItemType.CODE_ITEM, dexFile.codeItemPool.getNumItems(), dexFile.codeItemPool.getSectionOffset()); writeItem(writer, ItemType.CODE_ITEM, dexFile.codeItemPool.getNumItems(), dexFile.codeItemPool.getSectionOffset());
writeItem(writer, DexItemType.CLASS_DATA_ITEM, dexFile.classDefPool.getNumClassDataItems(), dexFile.classDefPool.getDataSectionOffset()); writeItem(writer, ItemType.CLASS_DATA_ITEM, dexFile.classDefPool.getNumClassDataItems(), dexFile.classDefPool.getDataSectionOffset());
writeItem(writer, DexItemType.MAP_LIST, 1, sectionOffset); writeItem(writer, ItemType.MAP_LIST, 1, sectionOffset);
} }
private int calcNumItems() { private int calcNumItems() {