CatchStructspublic final class CatchStructs extends Object List of exception handlers (tuples of covered range, catch type,
handler address) for a particular piece of code. Instances of this
class correspond to a {@code try_item[]} and a
{@code catch_handler_item[]}. |
Fields Summary |
---|
private static final int | TRY_ITEM_WRITE_SIZEthe size of a {@code try_item}: a {@code uint}
and two {@code ushort}s | private final com.android.dx.dex.code.DalvCode | code{@code non-null;} code that contains the catches | private com.android.dx.dex.code.CatchTable | table{@code null-ok;} the underlying table; set in
{@link #finishProcessingIfNecessary} | private byte[] | encodedHandlers{@code null-ok;} the encoded handler list, if calculated; set in
{@link #encode} | private int | encodedHandlerHeaderSizelength of the handlers header (encoded size), if known; used for
annotation | private TreeMap | handlerOffsets{@code null-ok;} map from handler lists to byte offsets, if calculated; set in
{@link #encode} |
Constructors Summary |
---|
public CatchStructs(com.android.dx.dex.code.DalvCode code)Constructs an instance.
this.code = code;
this.table = null;
this.encodedHandlers = null;
this.encodedHandlerHeaderSize = 0;
this.handlerOffsets = null;
|
Methods Summary |
---|
private static void | annotateAndConsumeHandlers(com.android.dx.dex.code.CatchHandlerList handlers, int offset, int size, java.lang.String prefix, java.io.PrintWriter printTo, com.android.dx.util.AnnotatedOutput annotateTo)Helper for {@link #annotateEntries} to annotate a catch handler list
while consuming it.
String s = handlers.toHuman(prefix, Hex.u2(offset) + ": ");
if (printTo != null) {
printTo.println(s);
}
annotateTo.annotate(size, s);
| private void | annotateEntries(java.lang.String prefix, java.io.PrintWriter printTo, com.android.dx.util.AnnotatedOutput annotateTo)Helper method to annotate or simply print the exception handlers.
Only one of {@code printTo} or {@code annotateTo} should
be non-null.
finishProcessingIfNecessary();
boolean consume = (annotateTo != null);
int amt1 = consume ? 6 : 0;
int amt2 = consume ? 2 : 0;
int size = table.size();
String subPrefix = prefix + " ";
if (consume) {
annotateTo.annotate(0, prefix + "tries:");
} else {
printTo.println(prefix + "tries:");
}
for (int i = 0; i < size; i++) {
CatchTable.Entry entry = table.get(i);
CatchHandlerList handlers = entry.getHandlers();
String s1 = subPrefix + "try " + Hex.u2or4(entry.getStart())
+ ".." + Hex.u2or4(entry.getEnd());
String s2 = handlers.toHuman(subPrefix, "");
if (consume) {
annotateTo.annotate(amt1, s1);
annotateTo.annotate(amt2, s2);
} else {
printTo.println(s1);
printTo.println(s2);
}
}
if (! consume) {
// Only emit the handler lists if we are consuming bytes.
return;
}
annotateTo.annotate(0, prefix + "handlers:");
annotateTo.annotate(encodedHandlerHeaderSize,
subPrefix + "size: " + Hex.u2(handlerOffsets.size()));
int lastOffset = 0;
CatchHandlerList lastList = null;
for (Map.Entry<CatchHandlerList, Integer> mapping :
handlerOffsets.entrySet()) {
CatchHandlerList list = mapping.getKey();
int offset = mapping.getValue();
if (lastList != null) {
annotateAndConsumeHandlers(lastList, lastOffset,
offset - lastOffset, subPrefix, printTo, annotateTo);
}
lastList = list;
lastOffset = offset;
}
annotateAndConsumeHandlers(lastList, lastOffset,
encodedHandlers.length - lastOffset,
subPrefix, printTo, annotateTo);
| public void | debugPrint(java.io.PrintWriter out, java.lang.String prefix)Does a human-friendly dump of this instance.
annotateEntries(prefix, out, null);
| public void | encode(DexFile file)Encodes the handler lists.
finishProcessingIfNecessary();
TypeIdsSection typeIds = file.getTypeIds();
int size = table.size();
handlerOffsets = new TreeMap<CatchHandlerList, Integer>();
/*
* First add a map entry for each unique list. The tree structure
* will ensure they are sorted when we reiterate later.
*/
for (int i = 0; i < size; i++) {
handlerOffsets.put(table.get(i).getHandlers(), null);
}
if (handlerOffsets.size() > 65535) {
throw new UnsupportedOperationException(
"too many catch handlers");
}
ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
// Write out the handlers "header" consisting of its size in entries.
encodedHandlerHeaderSize =
out.writeUleb128(handlerOffsets.size());
// Now write the lists out in order, noting the offset of each.
for (Map.Entry<CatchHandlerList, Integer> mapping :
handlerOffsets.entrySet()) {
CatchHandlerList list = mapping.getKey();
int listSize = list.size();
boolean catchesAll = list.catchesAll();
// Set the offset before we do any writing.
mapping.setValue(out.getCursor());
if (catchesAll) {
// A size <= 0 means that the list ends with a catch-all.
out.writeSleb128(-(listSize - 1));
listSize--;
} else {
out.writeSleb128(listSize);
}
for (int i = 0; i < listSize; i++) {
CatchHandlerList.Entry entry = list.get(i);
out.writeUleb128(
typeIds.indexOf(entry.getExceptionType()));
out.writeUleb128(entry.getHandler());
}
if (catchesAll) {
out.writeUleb128(list.get(listSize).getHandler());
}
}
encodedHandlers = out.toByteArray();
| private void | finishProcessingIfNecessary()Finish processing the catches, if necessary.
if (table == null) {
table = code.getCatches();
}
| public int | triesSize()Gets the size of the tries list, in entries.
finishProcessingIfNecessary();
return table.size();
| public int | writeSize()Gets the write size of this instance, in bytes.
return (triesSize() * TRY_ITEM_WRITE_SIZE) +
+ encodedHandlers.length;
| public void | writeTo(DexFile file, com.android.dx.util.AnnotatedOutput out)Writes this instance to the given stream.
finishProcessingIfNecessary();
if (out.annotates()) {
annotateEntries(" ", null, out);
}
int tableSize = table.size();
for (int i = 0; i < tableSize; i++) {
CatchTable.Entry one = table.get(i);
int start = one.getStart();
int end = one.getEnd();
int insnCount = end - start;
if (insnCount >= 65536) {
throw new UnsupportedOperationException(
"bogus exception range: " + Hex.u4(start) + ".." +
Hex.u4(end));
}
out.writeInt(start);
out.writeShort(insnCount);
out.writeShort(handlerOffsets.get(one.getHandlers()));
}
out.write(encodedHandlers);
|
|