FileDocCategorySizeDatePackage
CatchStructs.javaAPI DocAndroid 1.5 API10222Wed May 06 22:41:02 BST 2009com.android.dx.dex.file

CatchStructs

public 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 try_item[] and a catch_handler_item[].

Fields Summary
private static final int
TRY_ITEM_WRITE_SIZE
the size of a try_item: a uint and two ushorts
private final com.android.dx.dex.code.DalvCode
code
non-null; code that contains the catches
private com.android.dx.dex.code.CatchTable
table
null-ok; the underlying table; set in {@link #finishProcessingIfNecessary}
private byte[]
encodedHandlers
null-ok; the encoded handler list, if calculated; set in {@link #encode}
private int
encodedHandlerHeaderSize
length of the handlers header (encoded size), if known; used for annotation
private TreeMap
handlerOffsets
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.

param
code non-null; code that contains the catches


                     
       
        this.code = code;
        this.table = null;
        this.encodedHandlers = null;
        this.encodedHandlerHeaderSize = 0;
        this.handlerOffsets = null;
    
Methods Summary
private static voidannotateAndConsumeHandlers(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.

param
handlers non-null; handlers to annotate
param
offset >= 0; the offset of this handler
param
size >= 1; the number of bytes the handlers consume
param
prefix non-null; prefix for each line
param
printTo null-ok; where to print to
param
annotateTo non-null; where to annotate to

        String s = handlers.toHuman(prefix, Hex.u2(offset) + ": ");

        if (printTo != null) {
            printTo.println(s);
        }

        annotateTo.annotate(size, s);
    
private voidannotateEntries(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 printTo or annotateTo should be non-null.

param
prefix non-null; prefix for each line
param
printTo null-ok; where to print to
param
annotateTo null-ok; where to consume bytes and annotate to

        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 voiddebugPrint(java.io.PrintWriter out, java.lang.String prefix)
Does a human-friendly dump of this instance.

param
out non-null; where to dump
param
prefix non-null; prefix to attach to each line of output

        annotateEntries(prefix, out, null);
    
public voidencode(DexFile file)
Encodes the handler lists.

param
file non-null; file this instance is part of

        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.writeUnsignedLeb128(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.writeSignedLeb128(-(listSize - 1));
                listSize--;
            } else {
                out.writeSignedLeb128(listSize);
            }

            for (int i = 0; i < listSize; i++) {
                CatchHandlerList.Entry entry = list.get(i);
                out.writeUnsignedLeb128(
                        typeIds.indexOf(entry.getExceptionType()));
                out.writeUnsignedLeb128(entry.getHandler());
            }

            if (catchesAll) {
                out.writeUnsignedLeb128(list.get(listSize).getHandler());
            }
        }

        encodedHandlers = out.toByteArray();
    
private voidfinishProcessingIfNecessary()
Finish processing the catches, if necessary.

        if (table == null) {
            table = code.getCatches();
        }
    
public inttriesSize()
Gets the size of the tries list, in entries.

return
>= 0; the tries list size

        finishProcessingIfNecessary();
        return table.size();
    
public intwriteSize()
Gets the write size of this instance, in bytes.

return
>= 0; the write size

        return (triesSize() * TRY_ITEM_WRITE_SIZE) +
                + encodedHandlers.length;
    
public voidwriteTo(DexFile file, com.android.dx.util.AnnotatedOutput out)
Writes this instance to the given stream.

param
file non-null; file this instance is part of
param
out non-null; where to write to

        finishProcessingIfNecessary();

        TypeIdsSection typeIds = file.getTypeIds();
        int tableSize = table.size();
        int handlersSize = handlerOffsets.size();
        
        if (out.annotates()) {
            annotateEntries("  ", null, out);
        }

        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);