Get rid of the separate DexBackedDexFile.Impl class

This commit is contained in:
Ben Gruver 2013-03-06 20:32:06 -08:00
parent 132eeaedf7
commit 12659ec7db
3 changed files with 139 additions and 166 deletions

View File

@ -93,7 +93,7 @@ public final class DexFileFactory {
dexBytes = Files.toByteArray(dexFile); dexBytes = Files.toByteArray(dexFile);
} }
return new DexBackedDexFile.Impl(dexBytes); return new DexBackedDexFile(dexBytes);
} }
public static void writeDexFile(String path, DexFile dexFile) throws IOException { public static void writeDexFile(String path, DexFile dexFile) throws IOException {

View File

@ -40,25 +40,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Set; import java.util.Set;
public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile { public class DexBackedDexFile extends BaseDexBuffer implements DexFile {
public DexBackedDexFile(@Nonnull byte[] buf) {
super(buf);
}
@Nonnull public abstract String getString(int stringIndex);
@Nullable public abstract String getOptionalString(int stringIndex);
@Nonnull public abstract String getType(int typeIndex);
@Nullable public abstract String getOptionalType(int typeIndex);
// TODO: refactor how dex items are read
public abstract int getMethodIdItemOffset(int methodIndex);
public abstract int getProtoIdItemOffset(int protoIndex);
public abstract int getFieldIdItemOffset(int fieldIndex);
public abstract int getClassDefItemOffset(int classIndex);
@Override @Nonnull public abstract DexReader readerAt(int offset);
public static class Impl extends DexBackedDexFile {
private final int stringCount; private final int stringCount;
private final int stringStartOffset; private final int stringStartOffset;
private final int typeCount; private final int typeCount;
@ -72,11 +54,11 @@ 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 Impl(@Nonnull BaseDexBuffer buf) { public DexBackedDexFile (@Nonnull BaseDexBuffer buf) {
this(buf.buf); this(buf.buf);
} }
public Impl(@Nonnull byte[] buf) { public DexBackedDexFile (@Nonnull byte[] buf) {
super(buf); super(buf);
verifyMagic(); verifyMagic();
@ -102,7 +84,7 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
@Nonnull @Nonnull
@Override @Override
public DexBackedClassDef readItem(int index) { public DexBackedClassDef readItem(int index) {
return new DexBackedClassDef(Impl.this, getClassDefItemOffset(index)); return new DexBackedClassDef(DexBackedDexFile.this, getClassDefItemOffset(index));
} }
@Override @Override
@ -155,7 +137,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return typeStartOffset + typeIndex*TypeIdItem.ITEM_SIZE; return typeStartOffset + typeIndex*TypeIdItem.ITEM_SIZE;
} }
@Override
public int getFieldIdItemOffset(int fieldIndex) { public int getFieldIdItemOffset(int fieldIndex) {
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);
@ -163,7 +144,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return fieldStartOffset + fieldIndex*FieldIdItem.ITEM_SIZE; return fieldStartOffset + fieldIndex*FieldIdItem.ITEM_SIZE;
} }
@Override
public int getMethodIdItemOffset(int methodIndex) { public int getMethodIdItemOffset(int methodIndex) {
if (methodIndex < 0 || methodIndex >= methodCount) { if (methodIndex < 0 || methodIndex >= methodCount) {
throw new ExceptionWithContext("Method index out of bounds: %d", methodIndex); throw new ExceptionWithContext("Method index out of bounds: %d", methodIndex);
@ -171,7 +151,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return methodStartOffset + methodIndex*MethodIdItem.ITEM_SIZE; return methodStartOffset + methodIndex*MethodIdItem.ITEM_SIZE;
} }
@Override
public int getProtoIdItemOffset(int protoIndex) { public int getProtoIdItemOffset(int protoIndex) {
if (protoIndex < 0 || protoIndex >= protoCount) { if (protoIndex < 0 || protoIndex >= protoCount) {
throw new ExceptionWithContext("Proto index out of bounds: %d", protoIndex); throw new ExceptionWithContext("Proto index out of bounds: %d", protoIndex);
@ -179,7 +158,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return protoStartOffset + protoIndex*ProtoIdItem.ITEM_SIZE; return protoStartOffset + protoIndex*ProtoIdItem.ITEM_SIZE;
} }
@Override
public int getClassDefItemOffset(int classIndex) { public int getClassDefItemOffset(int classIndex) {
if (classIndex < 0 || classIndex >= classCount) { if (classIndex < 0 || classIndex >= classCount) {
throw new ExceptionWithContext("Class index out of bounds: %d", classIndex); throw new ExceptionWithContext("Class index out of bounds: %d", classIndex);
@ -191,7 +169,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return classCount; return classCount;
} }
@Override
@Nonnull @Nonnull
public String getString(int stringIndex) { public String getString(int stringIndex) {
int stringOffset = getStringIdItemOffset(stringIndex); int stringOffset = getStringIdItemOffset(stringIndex);
@ -201,7 +178,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return reader.readString(utf16Length); return reader.readString(utf16Length);
} }
@Override
@Nullable @Nullable
public String getOptionalString(int stringIndex) { public String getOptionalString(int stringIndex) {
if (stringIndex == -1) { if (stringIndex == -1) {
@ -210,7 +186,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return getString(stringIndex); return getString(stringIndex);
} }
@Override
@Nonnull @Nonnull
public String getType(int typeIndex) { public String getType(int typeIndex) {
int typeOffset = getTypeIdItemOffset(typeIndex); int typeOffset = getTypeIdItemOffset(typeIndex);
@ -218,7 +193,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return getString(stringIndex); return getString(stringIndex);
} }
@Override
@Nullable @Nullable
public String getOptionalType(int typeIndex) { public String getOptionalType(int typeIndex) {
if (typeIndex == -1) { if (typeIndex == -1) {
@ -233,4 +207,3 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
return new DexReader(this, offset); return new DexReader(this, offset);
} }
} }
}

View File

@ -43,7 +43,7 @@ import java.io.Writer;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class RawDexFile extends DexBackedDexFile.Impl { public class RawDexFile extends DexBackedDexFile {
@Nonnull public final HeaderItem headerItem; @Nonnull public final HeaderItem headerItem;
public RawDexFile(BaseDexBuffer buf) { public RawDexFile(BaseDexBuffer buf) {