com.android.dx.util.AnnotatedOutput Java Examples

The following examples show how to use com.android.dx.util.AnnotatedOutput. 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: UniformListItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    int size = items.size();

    if (out.annotates()) {
        out.annotate(0, offsetString() + " " + typeName());
        out.annotate(4, "  size: " + Hex.u4(size));
    }

    out.writeInt(size);

    for (OffsettedItem i : items) {
        i.writeTo(file, out);
    }
}
 
Example #2
Source File: StringDataItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo0(DexFile file, AnnotatedOutput out) {
    ByteArray bytes = value.getBytes();
    int utf16Size = value.getUtf16Size();

    if (out.annotates()) {
        out.annotate(Leb128.unsignedLeb128Size(utf16Size),
                "utf16_size: " + Hex.u4(utf16Size));
        out.annotate(bytes.size() + 1, value.toQuoted());
    }

    out.writeUleb128(utf16Size);
    out.write(bytes);
    out.writeByte(0);
}
 
Example #3
Source File: ClassDataItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo0(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();

    if (annotates) {
        /*
         * The output is to be annotated, so redo the work previously
         * done by place0(), except this time annotations will actually
         * get emitted.
         */
        encodeOutput(file, out);
    } else {
        out.write(encodedForm);
    }
}
 
Example #4
Source File: Form12x.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    int sz = regs.size();

    /*
     * The (sz - 2) and (sz - 1) below makes this code work for
     * both the two- and three-register ops. (See "case 3" in
     * isCompatible(), above.)
     */

    write(out, opcodeUnit(insn,
                          makeByte(regs.get(sz - 2).getReg(),
                                   regs.get(sz - 1).getReg())));
}
 
Example #5
Source File: MethodHandleItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    int targetIndex = getTargetIndex(file);
    int mhType = methodHandle.getMethodHandleType();
    if (out.annotates()) {
        out.annotate(0, indexString() + ' ' + methodHandle.toString());
        String typeComment = " // " + CstMethodHandle.getMethodHandleTypeName(mhType);
        out.annotate(2, "type:     " + Hex.u2(mhType) + typeComment);
        out.annotate(2, "reserved: " + Hex.u2(0));
        String targetComment = " // " +  methodHandle.getRef().toString();
        if (methodHandle.isAccessor()) {
            out.annotate(2, "fieldId:  " + Hex.u2(targetIndex) + targetComment);
        } else {
            out.annotate(2, "methodId: " + Hex.u2(targetIndex) + targetComment);
        }
        out.annotate(2, "reserved: " + Hex.u2(0));
    }
    out.writeShort(mhType);
    out.writeShort(0);
    out.writeShort(getTargetIndex(file));
    out.writeShort(0);
}
 
Example #6
Source File: TypeListItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    TypeIdsSection typeIds = file.getTypeIds();
    int sz = list.size();

    if (out.annotates()) {
        out.annotate(0, offsetString() + " type_list");
        out.annotate(HEADER_SIZE, "  size: " + Hex.u4(sz));
        for (int i = 0; i < sz; i++) {
            Type one = list.getType(i);
            int idx = typeIds.indexOf(one);
            out.annotate(ELEMENT_SIZE,
                         "  " + Hex.u2(idx) + " // " + one.toHuman());
        }
    }

    out.writeInt(sz);

    for (int i = 0; i < sz; i++) {
        out.writeShort(typeIds.indexOf(list.getType(i)));
    }
}
 
Example #7
Source File: Form35c.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    int cpi = ((CstInsn) insn).getIndex();
    RegisterSpecList regs = explicitize(insn.getRegisters());
    int sz = regs.size();
    int r0 = (sz > 0) ? regs.get(0).getReg() : 0;
    int r1 = (sz > 1) ? regs.get(1).getReg() : 0;
    int r2 = (sz > 2) ? regs.get(2).getReg() : 0;
    int r3 = (sz > 3) ? regs.get(3).getReg() : 0;
    int r4 = (sz > 4) ? regs.get(4).getReg() : 0;

    write(out,
          opcodeUnit(insn,
                     makeByte(r4, sz)), // encode the fifth operand here
          (short) cpi,
          codeUnit(r0, r1, r2, r3));
}
 
Example #8
Source File: TypeListItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    TypeIdsSection typeIds = file.getTypeIds();
    int sz = list.size();

    if (out.annotates()) {
        out.annotate(0, offsetString() + " type_list");
        out.annotate(HEADER_SIZE, "  size: " + Hex.u4(sz));
        for (int i = 0; i < sz; i++) {
            Type one = list.getType(i);
            int idx = typeIds.indexOf(one);
            out.annotate(ELEMENT_SIZE,
                         "  " + Hex.u2(idx) + " // " + one.toHuman());
        }
    }

    out.writeInt(sz);

    for (int i = 0; i < sz; i++) {
        out.writeShort(typeIds.indexOf(list.getType(i)));
    }
}
 
Example #9
Source File: Form35c.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    int cpi = ((CstInsn) insn).getIndex();
    RegisterSpecList regs = explicitize(insn.getRegisters());
    int sz = regs.size();
    int r0 = (sz > 0) ? regs.get(0).getReg() : 0;
    int r1 = (sz > 1) ? regs.get(1).getReg() : 0;
    int r2 = (sz > 2) ? regs.get(2).getReg() : 0;
    int r3 = (sz > 3) ? regs.get(3).getReg() : 0;
    int r4 = (sz > 4) ? regs.get(4).getReg() : 0;

    write(out,
          opcodeUnit(insn,
                     makeByte(r4, sz)), // encode the fifth operand here
          (short) cpi,
          codeUnit(r0, r1, r2, r3));
}
 
Example #10
Source File: Form21h.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    CstLiteralBits cb = (CstLiteralBits) ((CstInsn) insn).getConstant();
    short bits;

    // Where the high bits are depends on the category of the target.
    if (regs.get(0).getCategory() == 1) {
        bits = (short) (cb.getIntBits() >>> 16);
    } else {
        bits = (short) (cb.getLongBits() >>> 48);
    }

    write(out, opcodeUnit(insn, regs.get(0).getReg()), bits);
}
 
Example #11
Source File: MemberIdItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    TypeIdsSection typeIds = file.getTypeIds();
    StringIdsSection stringIds = file.getStringIds();
    CstNat nat = cst.getNat();
    int classIdx = typeIds.indexOf(getDefiningClass());
    int nameIdx = stringIds.indexOf(nat.getName());
    int typoidIdx = getTypoidIdx(file);

    if (out.annotates()) {
        out.annotate(0, indexString() + ' ' + cst.toHuman());
        out.annotate(2, "  class_idx: " + Hex.u2(classIdx));
        out.annotate(2, String.format("  %-10s %s", getTypoidName() + ':',
                        Hex.u2(typoidIdx)));
        out.annotate(4, "  name_idx:  " + Hex.u4(nameIdx));
    }

    out.writeShort(classIdx);
    out.writeShort(typoidIdx);
    out.writeInt(nameIdx);
}
 
Example #12
Source File: Section.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Writes this instance to the given raw data object.
 *
 * @param out {@code non-null;} where to write to
 */
public final void writeTo(AnnotatedOutput out) {
    throwIfNotPrepared();
    align(out);

    int cursor = out.getCursor();

    if (fileOffset < 0) {
        fileOffset = cursor;
    } else if (fileOffset != cursor) {
        throw new RuntimeException("alignment mismatch: for " + this +
                                   ", at " + cursor +
                                   ", but expected " + fileOffset);
    }

    if (out.annotates()) {
        if (name != null) {
            out.annotate(0, "\n" + name + ":");
        } else if (cursor != 0) {
            out.annotate(0, "\n");
        }
    }

    writeTo0(out);
}
 
Example #13
Source File: Form45cc.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    MultiCstInsn mci = (MultiCstInsn) insn;
    short regB = (short) mci.getIndex(0);  // B is the method index
    short regH = (short) mci.getIndex(1);  // H is the call site proto index

    RegisterSpecList regs = explicitize(insn.getRegisters());
    int regA = regs.size();
    int regC = (regA > 0) ? regs.get(0).getReg() : 0;
    int regD = (regA > 1) ? regs.get(1).getReg() : 0;
    int regE = (regA > 2) ? regs.get(2).getReg() : 0;
    int regF = (regA > 3) ? regs.get(3).getReg() : 0;
    int regG = (regA > 4) ? regs.get(4).getReg() : 0;

    // The output format is: A|G|op BBBB F|E|D|C HHHHH
    write(out,
          opcodeUnit(insn, makeByte(regG, regA)),
          regB,
          codeUnit(regC, regD, regE, regF),
          regH);
}
 
Example #14
Source File: TypeIdsSection.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = typeIds.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (sz > DexFormat.MAX_TYPE_IDX + 1) {
        throw new DexIndexOverflowException(
                String.format("Too many type identifiers to fit in one dex file: %1$d; max is %2$d.%n"
                                + "You may try using multi-dex. If multi-dex is enabled then the list of "
                                + "classes for the main dex list is too large.",
                        items().size(), DexFormat.MAX_MEMBER_IDX + 1));
    }

    if (out.annotates()) {
        out.annotate(4, "type_ids_size:   " + Hex.u4(sz));
        out.annotate(4, "type_ids_off:    " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #15
Source File: DebugInfoItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #encode} to do most of the work.
 *
 * @param file {@code null-ok;} file to refer to during encoding
 * @param prefix {@code null-ok;} prefix to attach to each line of output
 * @param debugPrint {@code null-ok;} if specified, an alternate output for
 * annotations
 * @param out {@code null-ok;} if specified, where annotations should go
 * @param consume whether to claim to have consumed output for
 * {@code out}
 * @return {@code non-null;} the encoded array
 */
private byte[] encode0(DexFile file, String prefix, PrintWriter debugPrint,
        AnnotatedOutput out, boolean consume) {
    PositionList positions = code.getPositions();
    LocalList locals = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int regSize = insns.getRegistersSize();

    DebugInfoEncoder encoder =
        new DebugInfoEncoder(positions, locals,
                file, codeSize, regSize, isStatic, ref);

    byte[] result;

    if ((debugPrint == null) && (out == null)) {
        result = encoder.convert();
    } else {
        result = encoder.convertAndAnnotate(prefix, debugPrint, out,
                consume);
    }

    return result;
}
 
Example #16
Source File: MemberIdItem.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) {
    TypeIdsSection typeIds = file.getTypeIds();
    StringIdsSection stringIds = file.getStringIds();
    CstNat nat = cst.getNat();
    int classIdx = typeIds.indexOf(getDefiningClass());
    int nameIdx = stringIds.indexOf(nat.getName());
    int typoidIdx = getTypoidIdx(file);

    if (out.annotates()) {
        out.annotate(0, indexString() + ' ' + cst.toHuman());
        out.annotate(2, "  class_idx: " + Hex.u2(classIdx));
        out.annotate(2, String.format("  %-10s %s", getTypoidName() + ':',
                        Hex.u2(typoidIdx)));
        out.annotate(4, "  name_idx:  " + Hex.u4(nameIdx));
    }

    out.writeShort(classIdx);
    out.writeShort(typoidIdx);
    out.writeInt(nameIdx);
}
 
Example #17
Source File: TypeIdsSection.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = typeIds.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (sz > DexFormat.MAX_TYPE_IDX + 1) {
        throw new DexIndexOverflowException("Too many type references: " + sz +
                "; max is " + (DexFormat.MAX_TYPE_IDX + 1) + ".\n" +
                Main.getTooManyIdsErrorMessage());
    }

    if (out.annotates()) {
        out.annotate(4, "type_ids_size:   " + Hex.u4(sz));
        out.annotate(4, "type_ids_off:    " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #18
Source File: Form22t.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    int offset = ((TargetInsn) insn).getTargetOffset();

    write(out,
          opcodeUnit(insn,
                     makeByte(regs.get(0).getReg(), regs.get(1).getReg())),
          (short) offset);
}
 
Example #19
Source File: AnnotationItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    AnnotationVisibility visibility = annotation.getVisibility();

    if (annotates) {
        out.annotate(0, offsetString() + " annotation");
        out.annotate(1, "  visibility: VISBILITY_" + visibility);
    }

    switch (visibility) {
        case BUILD:   out.writeByte(VISIBILITY_BUILD); break;
        case RUNTIME: out.writeByte(VISIBILITY_RUNTIME); break;
        case SYSTEM:  out.writeByte(VISIBILITY_SYSTEM); break;
        default: {
            // EMBEDDED shouldn't appear at the top level.
            throw new RuntimeException("shouldn't happen");
        }
    }

    if (annotates) {
        /*
         * The output is to be annotated, so redo the work previously
         * done by place0(), except this time annotations will actually
         * get emitted.
         */
        ValueEncoder encoder = new ValueEncoder(file, out);
        encoder.writeAnnotation(annotation, true);
    } else {
        out.write(encodedForm);
    }
}
 
Example #20
Source File: Form31c.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    int cpi = ((CstInsn) insn).getIndex();

    write(out, opcodeUnit(insn, regs.get(0).getReg()), cpi);
}
 
Example #21
Source File: FieldAnnotationStruct.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void writeTo(DexFile file, AnnotatedOutput out) {
    int fieldIdx = file.getFieldIds().indexOf(field);
    int annotationsOff = annotations.getAbsoluteOffset();

    if (out.annotates()) {
        out.annotate(0, "    " + field.toHuman());
        out.annotate(4, "      field_idx:       " + Hex.u4(fieldIdx));
        out.annotate(4, "      annotations_off: " +
                Hex.u4(annotationsOff));
    }

    out.writeInt(fieldIdx);
    out.writeInt(annotationsOff);
}
 
Example #22
Source File: CallSiteItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    if (out.annotates()) {
        out.annotate(0, offsetString() + " call site");
        ValueEncoder encoder = new ValueEncoder(file, out);
        encoder.writeArray(value, true);
    } else {
        out.write(encodedForm);
    }
}
 
Example #23
Source File: CatchStructs.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Writes this instance to the given stream.
 *
 * @param file {@code non-null;} file this instance is part of
 * @param out {@code non-null;} where to write to
 */
public void writeTo(DexFile file, AnnotatedOutput out) {
    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);
}
 
Example #24
Source File: DebugInfoItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    if (out.annotates()) {
        /*
         * Re-run the encoder to generate the annotations,
         * but write the bits from the original encode
         */

        out.annotate(offsetString() + " debug info");
        encode(file, null, null, out, true);
    }

    out.write(encoded);
}
 
Example #25
Source File: CatchStructs.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #annotateEntries} to annotate a catch handler list
 * while consuming it.
 *
 * @param handlers {@code non-null;} handlers to annotate
 * @param offset {@code >= 0;} the offset of this handler
 * @param size {@code >= 1;} the number of bytes the handlers consume
 * @param prefix {@code non-null;} prefix for each line
 * @param printTo {@code null-ok;} where to print to
 * @param annotateTo {@code non-null;} where to annotate to
 */
private static void annotateAndConsumeHandlers(CatchHandlerList handlers,
        int offset, int size, String prefix, PrintWriter printTo,
        AnnotatedOutput annotateTo) {
    String s = handlers.toHuman(prefix, Hex.u2(offset) + ": ");

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

    annotateTo.annotate(size, s);
}
 
Example #26
Source File: Form22x.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    write(out,
          opcodeUnit(insn, regs.get(0).getReg()),
          (short) regs.get(1).getReg());
}
 
Example #27
Source File: ClassDefsSection.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = classDefs.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (out.annotates()) {
        out.annotate(4, "class_defs_size: " + Hex.u4(sz));
        out.annotate(4, "class_defs_off:  " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #28
Source File: Form21s.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    int value =
        ((CstLiteralBits) ((CstInsn) insn).getConstant()).getIntBits();

    write(out,
          opcodeUnit(insn, regs.get(0).getReg()),
          (short) value);
}
 
Example #29
Source File: OddSpacer.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(AnnotatedOutput out) {
    if (codeSize() != 0) {
        out.writeShort(InsnFormat.codeUnit(Opcodes.NOP, 0));
    }
}
 
Example #30
Source File: MethodAnnotationStruct.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void writeTo(DexFile file, AnnotatedOutput out) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int annotationsOff = annotations.getAbsoluteOffset();

    if (out.annotates()) {
        out.annotate(0, "    " + method.toHuman());
        out.annotate(4, "      method_idx:      " + Hex.u4(methodIdx));
        out.annotate(4, "      annotations_off: " +
                Hex.u4(annotationsOff));
    }

    out.writeInt(methodIdx);
    out.writeInt(annotationsOff);
}