- added logic to set the offset and index of items when reading them in, in order to support an immediate write without having to call place (for dumping)

- moved the logic in MapItem that sorts the items for the various sections from writeTo() to place(), so that the order of items aren't changed when doing a read then immediate write (for dumping)
- changed the logic in makeBlankMapItem to get the map values from the MapValue enum, instead of hard coded values
- added logic to the OffsettedSection so that it sorts the item list by offset after reading it in, so that they items are in the same order as in the file it just read in (for dumping)

git-svn-id: https://smali.googlecode.com/svn/trunk@180 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2009-06-20 20:30:12 +00:00
parent 1401347994
commit 8c568e2b37
4 changed files with 41 additions and 33 deletions

View File

@ -50,10 +50,11 @@ public abstract class IndexedSection<T extends IndexedItem<T>> extends Section<T
public void readFrom(int size, Input in) { public void readFrom(int size, Input in) {
super.setSize(size); super.setSize(size);
this.offset = in.getCursor();
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
T item = getByIndex(i); T item = getByIndex(i);
item.readFrom(in); item.readFrom(in, i);
in.alignTo(item.getAlignment()); in.alignTo(item.getAlignment());
} }
} }

View File

@ -60,7 +60,9 @@ public abstract class Item<T extends Item> {
return offset; return offset;
} }
public void readFrom(Input in) { public void readFrom(Input in, int index) {
this.offset = in.getCursor();
this.index = index;
for (Field field: fields) { for (Field field: fields) {
field.readFrom(in); field.readFrom(in);
} }

View File

@ -29,6 +29,7 @@
package org.jf.dexlib; package org.jf.dexlib;
import org.jf.dexlib.util.AnnotatedOutput; import org.jf.dexlib.util.AnnotatedOutput;
import org.jf.dexlib.util.Input;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -51,6 +52,12 @@ public class MapItem extends IndexedItem<MapItem> {
} }
public int place(int index, int offset) { public int place(int index, int offset) {
Collections.sort(mapEntries, new Comparator<MapField>() {
public int compare(MapField o1, MapField o2) {
return ((Integer)o1.getSectionOffset()).compareTo(o2.getSectionOffset());
}
});
for (int i=0; i<mapEntries.size(); i++) { for (int i=0; i<mapEntries.size(); i++) {
MapField mapField = mapEntries.get(i); MapField mapField = mapEntries.get(i);
mapField.place(offset); mapField.place(offset);
@ -62,38 +69,27 @@ public class MapItem extends IndexedItem<MapItem> {
return super.place(index, offset); return super.place(index, offset);
} }
public void writeTo(AnnotatedOutput out) {
Collections.sort(mapEntries, new Comparator<MapField>() {
public int compare(MapField o1, MapField o2) {
return ((Integer)o1.getSectionOffset()).compareTo(o2.getSectionOffset());
}
});
super.writeTo(out);
}
public static MapItem makeBlankMapItem(DexFile dexFile) { public static MapItem makeBlankMapItem(DexFile dexFile) {
MapItem mapItem = new MapItem(dexFile, 0); MapItem mapItem = new MapItem(dexFile, 0);
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0000)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_HEADER_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0001)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_ID_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0002)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_ID_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0003)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_PROTO_ID_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0004)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_FIELD_ID_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0005)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_METHOD_ID_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x0006)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DEF_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x1002)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_DATA_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x1003)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ENCODED_ARRAY_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2001)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2006)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x1001)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_REF_LIST.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2002)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2003)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_LIST.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2004)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_DEBUG_INFO_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2005)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CODE_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x2000)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DATA_ITEM.getMapValue()));
mapItem.mapEntries.add(new MapField(dexFile, (short)0x1000)); mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_MAP_LIST.getMapValue()));
return mapItem; return mapItem;

View File

@ -31,6 +31,8 @@ package org.jf.dexlib;
import org.jf.dexlib.util.Input; import org.jf.dexlib.util.Input;
import java.util.HashMap; import java.util.HashMap;
import java.util.Collections;
import java.util.Comparator;
public abstract class OffsettedSection<T extends OffsettedItem<T>> extends Section<T> { public abstract class OffsettedSection<T extends OffsettedItem<T>> extends Section<T> {
protected HashMap<Integer, T> itemsByOffset; protected HashMap<Integer, T> itemsByOffset;
@ -57,14 +59,21 @@ public abstract class OffsettedSection<T extends OffsettedItem<T>> extends Secti
return item; return item;
} }
public void readFrom(int size, Input in) { public void readFrom(int size, Input in) {
this.offset = in.getCursor();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
T item = getByOffset(in.getCursor()); T item = getByOffset(in.getCursor());
item.readFrom(in); item.readFrom(in, i);
//TODO: why are we aligning afterwards? //TODO: why are we aligning afterwards?
in.alignTo(item.getAlignment()); in.alignTo(item.getAlignment());
} }
//sort the items list by offset
Collections.sort(items, new Comparator<T>() {
public int compare(T t, T t1) {
return ((Integer)t.getOffset()).compareTo(t1.getOffset());
}
});
} }
protected abstract T make(int offset); protected abstract T make(int offset);