Annotate FieldIdItems

This commit is contained in:
Ben Gruver 2013-02-24 20:13:22 -08:00
parent 5b71582325
commit 1771f92aaf
3 changed files with 89 additions and 13 deletions

View File

@ -31,10 +31,7 @@
package org.jf.dexlib2.dexbacked; package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.raw.HeaderItem; import org.jf.dexlib2.dexbacked.raw.*;
import org.jf.dexlib2.dexbacked.raw.ProtoIdItem;
import org.jf.dexlib2.dexbacked.raw.StringIdItem;
import org.jf.dexlib2.dexbacked.raw.TypeIdItem;
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.dexlib2.util.AnnotatedBytes;
@ -81,15 +78,10 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
private final int classStartOffset; private final int classStartOffset;
private static final int FIELD_ID_ITEM_SIZE = 8;
private static final int METHOD_ID_ITEM_SIZE = 8; private static final int METHOD_ID_ITEM_SIZE = 8;
private static final int CLASS_DEF_ITEM_SIZE = 32; private static final int CLASS_DEF_ITEM_SIZE = 32;
public static final int MAP_ITEM_SIZE = 12; public static final int MAP_ITEM_SIZE = 12;
public static final int FIELD_CLASS_IDX_OFFSET = 0;
public static final int FIELD_TYPE_IDX_OFFSET = 2;
public static final int FIELD_NAME_IDX_OFFSET = 4;
public static final int METHOD_CLASS_IDX_OFFSET = 0; public static final int METHOD_CLASS_IDX_OFFSET = 0;
public static final int METHOD_PROTO_IDX_OFFSET = 2; public static final int METHOD_PROTO_IDX_OFFSET = 2;
public static final int METHOD_NAME_IDX_OFFSET = 4; public static final int METHOD_NAME_IDX_OFFSET = 4;
@ -181,7 +173,7 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
if (fieldIndex < 0 || fieldIndex >= fieldCount) { if (fieldIndex < 0 || fieldIndex >= fieldCount) {
throw new ExceptionWithContext("Field index out of bounds: %d", fieldIndex); throw new ExceptionWithContext("Field index out of bounds: %d", fieldIndex);
} }
return fieldStartOffset + fieldIndex*FIELD_ID_ITEM_SIZE; return fieldStartOffset + fieldIndex*FieldIdItem.ITEM_SIZE;
} }
@Override @Override
@ -275,6 +267,12 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount); ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount);
} }
if (fieldCount > 0) {
annotatedBytes.skipTo(getFieldIdItemOffset(0));
annotatedBytes.annotate(0, " ");
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount);
}
annotatedBytes.writeAnnotations(out, buf); annotatedBytes.writeAnnotations(out, buf);
} }
} }

View File

@ -0,0 +1,77 @@
/*
* 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 FieldIdItem {
public static final int ITEM_SIZE = 8;
public static final int CLASS_OFFSET = 0;
public static final int TYPE_OFFSET = 2;
public static final int NAME_OFFSET = 4;
public static SectionAnnotator getAnnotator() {
return new SectionAnnotator() {
@Override
public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int length) {
if (length > 0) {
out.annotate(0, "-----------------------------");
out.annotate(0, "field_id_item section");
out.annotate(0, "-----------------------------");
out.annotate(0, "");
for (int i=0; i<length; i++) {
out.annotate(0, "[%d] field_id_item", i);
out.indent();
annotateField(out, dexFile);
out.deindent();
}
}
}
};
}
private static void annotateField(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile) {
int classIndex = dexFile.readUshort(out.getCursor());
out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));
int typeIndex = dexFile.readUshort(out.getCursor());
out.annotate(2, "return_type_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));
int nameIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "name_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, nameIndex));
}
}

View File

@ -33,6 +33,7 @@ package org.jf.dexlib2.dexbacked.reference;
import org.jf.dexlib2.base.reference.BaseFieldReference; import org.jf.dexlib2.base.reference.BaseFieldReference;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.raw.FieldIdItem;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -48,18 +49,18 @@ public class DexBackedFieldReference extends BaseFieldReference {
@Nonnull @Nonnull
@Override @Override
public String getDefiningClass() { public String getDefiningClass() {
return dexFile.getType(dexFile.readUshort(fieldIdItemOffset + DexBackedDexFile.Impl.FIELD_CLASS_IDX_OFFSET)); return dexFile.getType(dexFile.readUshort(fieldIdItemOffset + FieldIdItem.CLASS_OFFSET));
} }
@Nonnull @Nonnull
@Override @Override
public String getName() { public String getName() {
return dexFile.getString(dexFile.readSmallUint(fieldIdItemOffset + DexBackedDexFile.Impl.FIELD_NAME_IDX_OFFSET)); return dexFile.getString(dexFile.readSmallUint(fieldIdItemOffset + FieldIdItem.NAME_OFFSET));
} }
@Nonnull @Nonnull
@Override @Override
public String getType() { public String getType() {
return dexFile.getType(dexFile.readUshort(fieldIdItemOffset + DexBackedDexFile.Impl.FIELD_TYPE_IDX_OFFSET)); return dexFile.getType(dexFile.readUshort(fieldIdItemOffset + FieldIdItem.TYPE_OFFSET));
} }
} }