Search in sources :

Example 1 with CatchHandlerList

use of com.taobao.android.dx.dex.code.CatchHandlerList in project atlas by alibaba.

the class CatchStructs method annotateEntries.

/**
     * Helper method to annotate or simply print the exception handlers.
     * Only one of {@code printTo} or {@code annotateTo} should
     * be non-null.
     *
     * @param prefix {@code non-null;} prefix for each line
     * @param printTo {@code null-ok;} where to print to
     * @param annotateTo {@code null-ok;} where to consume bytes and annotate to
     */
private void annotateEntries(String prefix, PrintWriter printTo, AnnotatedOutput annotateTo) {
    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);
}
Also used : CatchHandlerList(com.taobao.android.dx.dex.code.CatchHandlerList) TreeMap(java.util.TreeMap) Map(java.util.Map) CatchTable(com.taobao.android.dx.dex.code.CatchTable)

Example 2 with CatchHandlerList

use of com.taobao.android.dx.dex.code.CatchHandlerList in project atlas by alibaba.

the class CatchStructs method encode.

/**
     * Encodes the handler lists.
     *
     * @param file {@code non-null;} file this instance is part of
     */
public void encode(DexFile file) {
    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();
}
Also used : CatchHandlerList(com.taobao.android.dx.dex.code.CatchHandlerList) ByteArrayAnnotatedOutput(com.taobao.android.dx.util.ByteArrayAnnotatedOutput) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

CatchHandlerList (com.taobao.android.dx.dex.code.CatchHandlerList)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 CatchTable (com.taobao.android.dx.dex.code.CatchTable)1 ByteArrayAnnotatedOutput (com.taobao.android.dx.util.ByteArrayAnnotatedOutput)1