Refactor how parameters/parameter names are handled

This commit is contained in:
Ben Gruver 2012-10-28 13:13:11 -07:00
parent 87179940cb
commit 12e071db60
2 changed files with 19 additions and 18 deletions

View File

@ -112,7 +112,7 @@ public class DexBackedMethodImplementation implements MethodImplementation {
public Iterable<? extends DebugItem> getDebugItems() { public Iterable<? extends DebugItem> getDebugItems() {
final int debugInfoOffset = dexBuf.readSmallUint(codeOffset + DEBUG_OFFSET_OFFSET); final int debugInfoOffset = dexBuf.readSmallUint(codeOffset + DEBUG_OFFSET_OFFSET);
if (debugInfoOffset > 0) { if (debugInfoOffset > 0) {
return new DebugItemList(dexBuf, debugInfoOffset, method); return new DebugItemList(dexBuf, debugInfoOffset, this);
} }
return ImmutableList.of(); return ImmutableList.of();
} }
@ -135,7 +135,9 @@ public class DexBackedMethodImplementation implements MethodImplementation {
return ImmutableList.copyOf(instructions); return ImmutableList.copyOf(instructions);
} }
public List<MethodParameter> getParametersWithNames() { //TODO: reading the method params from the debug_info_item should probably be centralized with other debug stuff
@Nullable
public VariableSizeList<MethodParameter> getParametersWithNamesOrNull() {
final int debugInfoOffset = dexBuf.readSmallUint(codeOffset + DEBUG_OFFSET_OFFSET); final int debugInfoOffset = dexBuf.readSmallUint(codeOffset + DEBUG_OFFSET_OFFSET);
if (debugInfoOffset > 0) { if (debugInfoOffset > 0) {
DexReader reader = dexBuf.readerAt(debugInfoOffset); DexReader reader = dexBuf.readerAt(debugInfoOffset);
@ -168,6 +170,14 @@ public class DexBackedMethodImplementation implements MethodImplementation {
@Override public int size() { return methodParametersWithoutNames.size(); } @Override public int size() { return methodParametersWithoutNames.size(); }
}; };
} }
return ImmutableList.of(); return null;
}
public List<? extends MethodParameter> getParametersWithNames() {
List<MethodParameter> parameters = getParametersWithNamesOrNull();
if (parameters != null) {
return parameters;
}
return method.getParametersWithoutNames();
} }
} }

View File

@ -32,16 +32,14 @@
package org.jf.dexlib2.dexbacked.util; package org.jf.dexlib2.dexbacked.util;
import org.jf.dexlib2.DebugItemType; import org.jf.dexlib2.DebugItemType;
import org.jf.dexlib2.dexbacked.DexBackedMethodImplementation;
import org.jf.dexlib2.dexbacked.DexBuffer; import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader; import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.MethodImplementation;
import org.jf.dexlib2.iface.MethodParameter; import org.jf.dexlib2.iface.MethodParameter;
import org.jf.dexlib2.iface.debug.DebugItem; import org.jf.dexlib2.iface.debug.DebugItem;
import org.jf.dexlib2.iface.debug.EndLocal; import org.jf.dexlib2.iface.debug.EndLocal;
import org.jf.dexlib2.iface.debug.LocalInfo; import org.jf.dexlib2.iface.debug.LocalInfo;
import org.jf.dexlib2.immutable.debug.*; import org.jf.dexlib2.immutable.debug.*;
import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -52,19 +50,13 @@ import java.util.NoSuchElementException;
public class DebugItemList implements Iterable<DebugItem> { public class DebugItemList implements Iterable<DebugItem> {
@Nonnull public final DexBuffer dexBuf; @Nonnull public final DexBuffer dexBuf;
private final int debugInfoOffset; private final int debugInfoOffset;
@Nonnull private final Method method; @Nonnull private final DexBackedMethodImplementation methodImpl;
@Nonnull private final MethodImplementation methodImpl;
public DebugItemList(@Nonnull DexBuffer dexBuf, public DebugItemList(@Nonnull DexBuffer dexBuf,
int debugInfoOffset, int debugInfoOffset,
@Nonnull Method method) { @Nonnull DexBackedMethodImplementation methodImpl) {
this.dexBuf = dexBuf; this.dexBuf = dexBuf;
this.debugInfoOffset = debugInfoOffset; this.debugInfoOffset = debugInfoOffset;
this.method = method;
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl == null) {
throw new ExceptionWithContext("Creating a DebugItemList for a method with no implementation. WTF?");
}
this.methodImpl = methodImpl; this.methodImpl = methodImpl;
} }
@ -86,10 +78,9 @@ public class DebugItemList implements Iterable<DebugItem> {
final LocalInfo[] locals = new LocalInfo[registerCount]; final LocalInfo[] locals = new LocalInfo[registerCount];
Arrays.fill(locals, EMPTY_LOCAL_INFO); Arrays.fill(locals, EMPTY_LOCAL_INFO);
// getParameters returns a VariableSizeList when a method implementation is present. Since we're reading debug // getParametersWithNames returns a VariableSizeList when debug info is present
// information, the method obviously has an implementation. VariableSizeList<? extends MethodParameter> parameters = methodImpl.getParametersWithNamesOrNull();
VariableSizeList<? extends MethodParameter> parameters = assert parameters != null;
(VariableSizeList<? extends MethodParameter>)method.getParameters();
final VariableSizeList<? extends MethodParameter>.Iterator parameterIterator = parameters.listIterator(); final VariableSizeList<? extends MethodParameter>.Iterator parameterIterator = parameters.listIterator();
{ // local scope for i { // local scope for i