/** {@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);
  }
Exemple #2
0
  /** {@inheritDoc} */
  @Override
  public void writeTo(AnnotatedOutput out) {
    int sz = values.size();

    out.writeShort(Opcodes.FILL_ARRAY_DATA_PAYLOAD);
    out.writeShort(elemWidth);
    out.writeInt(initLength);

    // For speed reasons, replicate the for loop in each case
    switch (elemWidth) {
      case 1:
        {
          for (int i = 0; i < sz; i++) {
            Constant cst = values.get(i);
            out.writeByte((byte) ((CstLiteral32) cst).getIntBits());
          }
          break;
        }
      case 2:
        {
          for (int i = 0; i < sz; i++) {
            Constant cst = values.get(i);
            out.writeShort((short) ((CstLiteral32) cst).getIntBits());
          }
          break;
        }
      case 4:
        {
          for (int i = 0; i < sz; i++) {
            Constant cst = values.get(i);
            out.writeInt(((CstLiteral32) cst).getIntBits());
          }
          break;
        }
      case 8:
        {
          for (int i = 0; i < sz; i++) {
            Constant cst = values.get(i);
            out.writeLong(((CstLiteral64) cst).getLongBits());
          }
          break;
        }
      default:
        break;
    }

    // Pad one byte to make the size of data table multiples of 16-bits
    if (elemWidth == 1 && (sz % 2 != 0)) {
      out.writeByte(0x00);
    }
  }
  /**
   * 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);
  }
 /**
  * Aligns the output of the given data to the alignment of this instance.
  *
  * @param out {@code non-null;} the output to align
  */
 protected final void align(AnnotatedOutput out) {
   out.alignTo(alignment);
 }
  /** {@inheritDoc} */
  @Override
  protected void writeTo0(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    int classOff = OffsettedItem.getAbsoluteOffsetOr0(classAnnotations);
    int fieldsSize = listSize(fieldAnnotations);
    int methodsSize = listSize(methodAnnotations);
    int parametersSize = listSize(parameterAnnotations);

    if (annotates) {
      out.annotate(0, offsetString() + " annotations directory");
      out.annotate(4, "  class_annotations_off: " + Hex.u4(classOff));
      out.annotate(4, "  fields_size:           " + Hex.u4(fieldsSize));
      out.annotate(4, "  methods_size:          " + Hex.u4(methodsSize));
      out.annotate(4, "  parameters_size:       " + Hex.u4(parametersSize));
    }

    out.writeInt(classOff);
    out.writeInt(fieldsSize);
    out.writeInt(methodsSize);
    out.writeInt(parametersSize);

    if (fieldsSize != 0) {
      Collections.sort(fieldAnnotations);
      if (annotates) {
        out.annotate(0, "  fields:");
      }
      for (FieldAnnotationStruct item : fieldAnnotations) {
        item.writeTo(file, out);
      }
    }

    if (methodsSize != 0) {
      Collections.sort(methodAnnotations);
      if (annotates) {
        out.annotate(0, "  methods:");
      }
      for (MethodAnnotationStruct item : methodAnnotations) {
        item.writeTo(file, out);
      }
    }

    if (parametersSize != 0) {
      Collections.sort(parameterAnnotations);
      if (annotates) {
        out.annotate(0, "  parameters:");
      }
      for (ParameterAnnotationStruct item : parameterAnnotations) {
        item.writeTo(file, out);
      }
    }
  }