Massage the try blocks before writing them out to a dex file

This commit is contained in:
Ben Gruver 2013-04-14 18:36:05 -07:00
parent 8c3d16b7ee
commit ab73502b60
2 changed files with 16 additions and 1 deletions

View File

@ -46,6 +46,7 @@ import org.jf.dexlib2.iface.reference.*;
import org.jf.dexlib2.util.MethodUtil;
import org.jf.dexlib2.util.ReferenceUtil;
import org.jf.dexlib2.writer.util.InstructionWriteUtil;
import org.jf.dexlib2.writer.util.TryListBuilder;
import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull;
@ -152,7 +153,7 @@ public class CodeItemPool {
InstructionWriteUtil instrWriteUtil = new InstructionWriteUtil(methodImpl, dexFile.stringPool);
writer.writeUshort(instrWriteUtil.getOutParamCount());
List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
List<? extends TryBlock> tryBlocks = TryListBuilder.massageTryBlocks(methodImpl.getTryBlocks());
writer.writeUshort(tryBlocks.size());
writer.writeInt(dexFile.debugInfoPool.getOffset(method));
writer.writeInt(instrWriteUtil.getCodeUnitCount());

View File

@ -62,6 +62,20 @@ public class TryListBuilder
listEnd.prev = listStart;
}
public static List<TryBlock> massageTryBlocks(List<? extends TryBlock> tryBlocks) {
TryListBuilder tlb = new TryListBuilder();
for (TryBlock tryBlock: tryBlocks) {
int startAddress = tryBlock.getStartCodeAddress();
int endAddress = startAddress + tryBlock.getCodeUnitCount();
for (ExceptionHandler exceptionHandler: tryBlock.getExceptionHandlers()) {
tlb.addHandler(exceptionHandler.getExceptionType(), startAddress, endAddress,
exceptionHandler.getHandlerCodeAddress());
}
}
return tlb.getTryBlocks();
}
private static class TryBounds {
@Nonnull public final MutableTryBlock start;
@Nonnull public final MutableTryBlock end;