Java Code Examples for com.android.dex.util.ExceptionWithContext#withContext()

The following examples show how to use com.android.dex.util.ExceptionWithContext#withContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DebugInfoDecoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example 2
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example 3
Source File: OffsettedItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example 4
Source File: OffsettedItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example 5
Source File: DebugInfoDecoder.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example 6
Source File: DebugInfoEncoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this (PositionList, LocalList) pair into a state machine
 * sequence.
 *
 * @return {@code non-null;} encoded byte sequence without padding and
 * terminated with a {@code 0x00} byte
 */
public byte[] convert() {
    try {
        byte[] ret;
        ret = convert0();

        if (DEBUG) {
            for (int i = 0 ; i < ret.length; i++) {
                System.err.printf("byte %02x\n", (0xff & ret[i]));
            }
        }

        return ret;
    } catch (IOException ex) {
        throw ExceptionWithContext
                .withContext(ex, "...while encoding debug info");
    }
}
 
Example 7
Source File: MixedItemSection.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Places all the items in this instance at particular offsets. This
 * will call {@link OffsettedItem#place} on each item. If an item
 * does not know its write size before the call to {@code place},
 * it is that call which is responsible for setting the write size.
 * This method may only be called once per instance; subsequent calls
 * will throw an exception.
 */
public void placeItems() {
    throwIfNotPrepared();

    switch (sort) {
        case INSTANCE: {
            Collections.sort(items);
            break;
        }
        case TYPE: {
            Collections.sort(items, TYPE_SORTER);
            break;
        }
    }

    int sz = items.size();
    int outAt = 0;
    for (int i = 0; i < sz; i++) {
        OffsettedItem one = items.get(i);
        try {
            int placedAt = one.place(this, outAt);

            if (placedAt < outAt) {
                throw new RuntimeException("bogus place() result for " +
                        one);
            }

            outAt = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex,
                    "...while placing " + one);
        }
    }

    writeSize = outAt;
}
 
Example 8
Source File: DebugInfoItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.

    try {
        encoded = encode(addedTo.getFile(), null, null, null, false);
        setWriteSize(encoded.length);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while placing debug info for " + ref.toHuman());
    }
}
 
Example 9
Source File: DebugInfoItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.

    try {
        encoded = encode(addedTo.getFile(), null, null, null, false);
        setWriteSize(encoded.length);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while placing debug info for " + ref.toHuman());
    }
}
 
Example 10
Source File: CodeItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #writeTo0} which writes out the actual bytecode.
 *
 * @param file {@code non-null;} file we are part of
 * @param out {@code non-null;} where to write to
 */
private void writeCodes(DexFile file, AnnotatedOutput out) {
    DalvInsnList insns = code.getInsns();

    try {
        insns.writeTo(out);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " +
                "instructions for " + ref.toHuman());
    }
}
 
Example 11
Source File: MixedItemSection.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Places all the items in this instance at particular offsets. This
 * will call {@link OffsettedItem#place} on each item. If an item
 * does not know its write size before the call to {@code place},
 * it is that call which is responsible for setting the write size.
 * This method may only be called once per instance; subsequent calls
 * will throw an exception.
 */
public void placeItems() {
    throwIfNotPrepared();

    switch (sort) {
        case INSTANCE: {
            Collections.sort(items);
            break;
        }
        case TYPE: {
            Collections.sort(items, TYPE_SORTER);
            break;
        }
    }

    int sz = items.size();
    int outAt = 0;
    for (int i = 0; i < sz; i++) {
        OffsettedItem one = items.get(i);
        try {
            int placedAt = one.place(this, outAt);

            if (placedAt < outAt) {
                throw new RuntimeException("bogus place() result for " +
                        one);
            }

            outAt = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex,
                    "...while placing " + one);
        }
    }

    writeSize = outAt;
}
 
Example 12
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the debug info sequence.
 */
public void decode() {
    try {
        decode0();
    } catch (Exception ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while decoding debug info");
    }
}
 
Example 13
Source File: DebugInfoItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.

    try {
        encoded = encode(addedTo.getFile(), null, null, null, false);
        setWriteSize(encoded.length);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while placing debug info for " + ref.toHuman());
    }
}
 
Example 14
Source File: CodeItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #writeTo0} which writes out the actual bytecode.
 *
 * @param file {@code non-null;} file we are part of
 * @param out {@code non-null;} where to write to
 */
private void writeCodes(DexFile file, AnnotatedOutput out) {
    DalvInsnList insns = code.getInsns();

    try {
        insns.writeTo(out);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " +
                "instructions for " + ref.toHuman());
    }
}
 
Example 15
Source File: CodeItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #writeTo0} which writes out the actual bytecode.
 *
 * @param file {@code non-null;} file we are part of
 * @param out {@code non-null;} where to write to
 */
private void writeCodes(DexFile file, AnnotatedOutput out) {
    DalvInsnList insns = code.getInsns();

    try {
        insns.writeTo(out);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " +
                "instructions for " + ref.toHuman());
    }
}
 
Example 16
Source File: MixedItemSection.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Places all the items in this instance at particular offsets. This
 * will call {@link OffsettedItem#place} on each item. If an item
 * does not know its write size before the call to {@code place},
 * it is that call which is responsible for setting the write size.
 * This method may only be called once per instance; subsequent calls
 * will throw an exception.
 */
public void placeItems() {
    throwIfNotPrepared();

    switch (sort) {
        case INSTANCE: {
            Collections.sort(items);
            break;
        }
        case TYPE: {
            Collections.sort(items, TYPE_SORTER);
            break;
        }
    }

    int sz = items.size();
    int outAt = 0;
    for (int i = 0; i < sz; i++) {
        OffsettedItem one = items.get(i);
        try {
            int placedAt = one.place(this, outAt);

            if (placedAt < outAt) {
                throw new RuntimeException("bogus place() result for " +
                        one);
            }

            outAt = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex,
                    "...while placing " + one);
        }
    }

    writeSize = outAt;
}
 
Example 17
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the debug info sequence.
 */
public void decode() {
    try {
        decode0();
    } catch (Exception ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while decoding debug info");
    }
}
 
Example 18
Source File: CfTranslator.java    From Box with Apache License 2.0 3 votes vote down vote up
/**
 * Takes a {@code byte[]}, interprets it as a Java classfile, and
 * translates it into a {@link ClassDefItem}.
 *
 * @param context {@code non-null;} the state global to this invocation.
 * @param cf {@code non-null;} the class file
 * @param bytes {@code non-null;} contents of the file
 * @param cfOptions options for class translation
 * @param dexOptions options for dex output
 * @param dexFile {@code non-null;} dex output
 * @return {@code non-null;} the translated class
 */
public static ClassDefItem translate(DxContext context, DirectClassFile cf, byte[] bytes,
        CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
    try {
        return translate0(context, cf, bytes, cfOptions, dexOptions, dexFile);
    } catch (RuntimeException ex) {
        String msg = "...while processing " + cf.getFilePath();
        throw ExceptionWithContext.withContext(ex, msg);
    }
}
 
Example 19
Source File: CfTranslator.java    From J2ME-Loader with Apache License 2.0 3 votes vote down vote up
/**
 * Takes a {@code byte[]}, interprets it as a Java classfile, and
 * translates it into a {@link ClassDefItem}.
 *
 * @param context {@code non-null;} the state global to this invocation.
 * @param cf {@code non-null;} the class file
 * @param bytes {@code non-null;} contents of the file
 * @param cfOptions options for class translation
 * @param dexOptions options for dex output
 * @param dexFile {@code non-null;} dex output
 * @return {@code non-null;} the translated class
 */
public static ClassDefItem translate(DxContext context, DirectClassFile cf, byte[] bytes,
        CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
    try {
        return translate0(context, cf, bytes, cfOptions, dexOptions, dexFile);
    } catch (RuntimeException ex) {
        String msg = "...while processing " + cf.getFilePath();
        throw ExceptionWithContext.withContext(ex, msg);
    }
}
 
Example 20
Source File: CfTranslator.java    From Box with Apache License 2.0 3 votes vote down vote up
/**
 * Takes a {@code byte[]}, interprets it as a Java classfile, and
 * translates it into a {@link ClassDefItem}.
 *
 * @param context {@code non-null;} the state global to this invocation.
 * @param cf {@code non-null;} the class file
 * @param bytes {@code non-null;} contents of the file
 * @param cfOptions options for class translation
 * @param dexOptions options for dex output
 * @param dexFile {@code non-null;} dex output
 * @return {@code non-null;} the translated class
 */
public static ClassDefItem translate(DxContext context, DirectClassFile cf, byte[] bytes,
        CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
    try {
        return translate0(context, cf, bytes, cfOptions, dexOptions, dexFile);
    } catch (RuntimeException ex) {
        String msg = "...while processing " + cf.getFilePath();
        throw ExceptionWithContext.withContext(ex, msg);
    }
}