Class Section

java.lang.Object
com.android.dx.dex.file.Section
Direct Known Subclasses:
MixedItemSection, UniformItemSection

public abstract class Section extends Object
A section of a .dex file. Each section consists of a list of items of some sort or other.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Section(String name, DexFile file, int alignment)
    Constructs an instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final void
    Aligns the output of the given data to the alignment of this instance.
    abstract int
    Returns the absolute file offset of the given item which must be contained in this section.
    final int
    getAbsoluteOffset(int relative)
    Returns the absolute file offset, given an offset from the start of this instance's output.
    final int
    Gets the alignment for this instance's final output.
    final DexFile
    Gets the file that this instance is part of.
    final int
    Gets the offset from the start of the file to this part.
    protected final String
    Returns the name of this section, for annotation purposes.
    abstract Collection<? extends Item>
    Gets the collection of all the items in this section.
    final void
    Prepares this instance for writing.
    protected abstract void
    Does the main work of prepare().
    final int
    setFileOffset(int fileOffset)
    Sets the file offset.
    protected final void
    Throws an exception if prepare() has not been called on this instance.
    protected final void
    Throws an exception if prepare() has already been called on this instance.
    static void
    validateAlignment(int alignment)
    Validates an alignment.
    abstract int
    Gets the size of this instance when output, in bytes.
    final void
    Writes this instance to the given raw data object.
    protected abstract void
    Writes this instance to the given raw data object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Section

      public Section(String name, DexFile file, int alignment)
      Constructs an instance. The file offset is initially unknown.
      Parameters:
      name - null-ok; the name of this instance, for annotation purposes
      file - non-null; file that this instance is part of
      alignment - > 0; alignment requirement for the final output; must be a power of 2
  • Method Details

    • validateAlignment

      public static void validateAlignment(int alignment)
      Validates an alignment.
      Parameters:
      alignment - the alignment
      Throws:
      IllegalArgumentException - thrown if alignment isn't a positive power of 2
    • getFile

      public final DexFile getFile()
      Gets the file that this instance is part of.
      Returns:
      non-null; the file
    • getAlignment

      public final int getAlignment()
      Gets the alignment for this instance's final output.
      Returns:
      > 0; the alignment
    • getFileOffset

      public final int getFileOffset()
      Gets the offset from the start of the file to this part. This throws an exception if the offset has not yet been set.
      Returns:
      >= 0; the file offset
    • setFileOffset

      public final int setFileOffset(int fileOffset)
      Sets the file offset. It is only valid to call this method once once per instance.
      Parameters:
      fileOffset - >= 0; the desired offset from the start of the file where this for this instance
      Returns:
      >= 0; the offset that this instance should be placed at in order to meet its alignment constraint
    • writeTo

      public final void writeTo(AnnotatedOutput out)
      Writes this instance to the given raw data object.
      Parameters:
      out - non-null; where to write to
    • getAbsoluteOffset

      public final int getAbsoluteOffset(int relative)
      Returns the absolute file offset, given an offset from the start of this instance's output. This is only valid to call once this instance has been assigned a file offset (via setFileOffset(int)).
      Parameters:
      relative - >= 0; the relative offset
      Returns:
      >= 0; the corresponding absolute file offset
    • getAbsoluteItemOffset

      public abstract int getAbsoluteItemOffset(Item item)
      Returns the absolute file offset of the given item which must be contained in this section. This is only valid to call once this instance has been assigned a file offset (via setFileOffset(int)).

      Note: Subclasses must implement this as appropriate for their contents.

      Parameters:
      item - non-null; the item in question
      Returns:
      >= 0; the item's absolute file offset
    • prepare

      public final void prepare()
      Prepares this instance for writing. This performs any necessary prerequisites, including particularly adding stuff to other sections. This method may only be called once per instance; subsequent calls will throw an exception.
    • items

      public abstract Collection<? extends Item> items()
      Gets the collection of all the items in this section. It is not valid to attempt to change the returned list.
      Returns:
      non-null; the items
    • prepare0

      protected abstract void prepare0()
      Does the main work of prepare().
    • writeSize

      public abstract int writeSize()
      Gets the size of this instance when output, in bytes.
      Returns:
      >= 0; the size of this instance, in bytes
    • throwIfNotPrepared

      protected final void throwIfNotPrepared()
      Throws an exception if prepare() has not been called on this instance.
    • throwIfPrepared

      protected final void throwIfPrepared()
      Throws an exception if prepare() has already been called on this instance.
    • align

      protected final void align(AnnotatedOutput out)
      Aligns the output of the given data to the alignment of this instance.
      Parameters:
      out - non-null; the output to align
    • writeTo0

      protected abstract void writeTo0(AnnotatedOutput out)
      Writes this instance to the given raw data object. This gets called by writeTo(com.android.dx.util.AnnotatedOutput) after aligning the cursor of out and verifying that either the assigned file offset matches the actual cursor out or that the file offset was not previously assigned, in which case it gets assigned to out's cursor.
      Parameters:
      out - non-null; where to write to
    • getName

      protected final String getName()
      Returns the name of this section, for annotation purposes.
      Returns:
      null-ok; name of this part, for annotation purposes