Class OffsettedItem

java.lang.Object
com.android.dx.dex.file.Item
com.android.dx.dex.file.OffsettedItem
All Implemented Interfaces:
Comparable<OffsettedItem>
Direct Known Subclasses:
AnnotationItem, AnnotationsDirectoryItem, AnnotationSetItem, AnnotationSetRefItem, CallSiteItem, ClassDataItem, CodeItem, DebugInfoItem, EncodedArrayItem, MapItem, StringDataItem, TypeListItem, UniformListItem

public abstract class OffsettedItem extends Item implements Comparable<OffsettedItem>
An item in a Dalvik file which is referenced by absolute offset.
  • Constructor Details

    • OffsettedItem

      public OffsettedItem(int alignment, int writeSize)
      Constructs an instance. The offset is initially unassigned.
      Parameters:
      alignment - > 0; output alignment requirement; must be a power of 2
      writeSize - >= -1; the size of this instance when written, in bytes, or -1 if not immediately known
  • Method Details

    • getAbsoluteOffsetOr0

      public static int getAbsoluteOffsetOr0(OffsettedItem item)
      Gets the absolute offset of the given item, returning 0 if handed null.
      Parameters:
      item - null-ok; the item in question
      Returns:
      >= 0; the item's absolute offset, or 0 if item == null
    • equals

      public final boolean equals(Object other)
      Comparisons for this class are defined to be type-major (if the types don't match then the objects are not equal), with compareTo0(com.android.dx.dex.file.OffsettedItem) deciding same-type comparisons.
      Overrides:
      equals in class Object
    • compareTo

      public final int compareTo(OffsettedItem other)
      Comparisons for this class are defined to be class-major (if the classes don't match then the objects are not equal), with compareTo0(com.android.dx.dex.file.OffsettedItem) deciding same-class comparisons.
      Specified by:
      compareTo in interface Comparable<OffsettedItem>
    • setWriteSize

      public final void setWriteSize(int writeSize)
      Sets the write size of this item. This may only be called once per instance, and only if the size was unknown upon instance creation.
      Parameters:
      writeSize - > 0; the write size, in bytes
    • writeSize

      public final int writeSize()
      Gets the size of this instance when written, in bytes.
      Specified by:
      writeSize in class Item
      Returns:
      >= 0; the write size
      Throws:
      UnsupportedOperationException - thrown if the write size is not yet known
    • writeTo

      public final void writeTo(DexFile file, AnnotatedOutput out)
      Writes the representation of this instance to the given data section, using the given DexFile to look things up as needed. If this instance keeps track of its offset, then this method will note the written offset and will also throw an exception if this instance has already been written.
      Specified by:
      writeTo in class Item
      Parameters:
      file - non-null; the file to use for reference
      out - non-null; where to write to
    • getRelativeOffset

      public final int getRelativeOffset()
      Gets the relative item offset. The offset is from the start of the section which the instance was written to.
      Returns:
      >= 0; the offset
      Throws:
      RuntimeException - thrown if the offset is not yet known
    • getAbsoluteOffset

      public final int getAbsoluteOffset()
      Gets the absolute item offset. The offset is from the start of the file which the instance was written to.
      Returns:
      >= 0; the offset
      Throws:
      RuntimeException - thrown if the offset is not yet known
    • place

      public final int place(Section addedTo, int offset)
      Indicates that this item has been added to the given section at the given offset. It is only valid to call this method once per instance.
      Parameters:
      addedTo - non-null; the section this instance has been added to
      offset - >= 0; the desired offset from the start of the section where this instance was placed
      Returns:
      >= 0; the offset that this instance should be placed at in order to meet its alignment constraint
    • getAlignment

      public final int getAlignment()
      Gets the alignment requirement of this instance. An instance should only be written when so aligned.
      Returns:
      > 0; the alignment requirement; must be a power of 2
    • offsetString

      public final String offsetString()
      Gets the absolute offset of this item as a string, suitable for including in annotations.
      Returns:
      non-null; the offset string
    • toHuman

      public abstract String toHuman()
      Gets a short human-readable string representing this instance.
      Returns:
      non-null; the human form
    • compareTo0

      protected int compareTo0(OffsettedItem other)
      Compares this instance to another which is guaranteed to be of the same class. The default implementation of this method is to throw an exception (unsupported operation). If a particular class needs to actually sort, then it should override this method.
      Parameters:
      other - non-null; instance to compare to
      Returns:
      -1, 0, or 1, depending on the sort order of this instance and the other
    • place0

      protected void place0(Section addedTo, int offset)
      Does additional work required when placing an instance. The default implementation of this method is a no-op. If a particular class needs to do something special, then it should override this method. In particular, if this instance did not know its write size up-front, then this method is responsible for setting it.
      Parameters:
      addedTo - non-null; the section this instance has been added to
      offset - >= 0; the offset from the start of the section where this instance was placed
    • writeTo0

      protected abstract void writeTo0(DexFile file, AnnotatedOutput out)
      Performs the actual write of the contents of this instance to the given data section. This is called by writeTo(com.android.dx.dex.file.DexFile, com.android.dx.util.AnnotatedOutput), which will have taken care of ensuring alignment.
      Parameters:
      file - non-null; the file to use for reference
      out - non-null; where to write to