FileDocCategorySizeDatePackage
UniformListItem.javaAPI DocAndroid 5.1 API6359Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.dex.file

UniformListItem

public final class UniformListItem extends OffsettedItem
Class that represents a contiguous list of uniform items. Each item in the list, in particular, must have the same write size and alignment.

This class inherits its alignment from its items, bumped up to {@code 4} if the items have a looser alignment requirement. If it is more than {@code 4}, then there will be a gap after the output list size (which is four bytes) and before the first item.

param
type of element contained in an instance

Fields Summary
private static final int
HEADER_SIZE
the size of the list header
private final ItemType
itemType
{@code non-null;} the item type
private final List
items
{@code non-null;} the contents
Constructors Summary
public UniformListItem(ItemType itemType, List items)
Constructs an instance. It is illegal to modify the given list once it is used to construct an instance of this class.

param
itemType {@code non-null;} the type of the item
param
items {@code non-null and non-empty;} list of items to represent


                                                   
         
        super(getAlignment(items), writeSize(items));

        if (itemType == null) {
            throw new NullPointerException("itemType == null");
        }

        this.items = items;
        this.itemType = itemType;
    
Methods Summary
public voidaddContents(DexFile file)
{@inheritDoc}

        for (OffsettedItem i : items) {
            i.addContents(file);
        }
    
private static intgetAlignment(java.util.List items)
Helper for {@link #UniformListItem}, which returns the alignment requirement implied by the given list. See the header comment for more details.

param
items {@code non-null;} list of items being represented
return
{@code >= 4;} the alignment requirement

        try {
            // Since they all must have the same alignment, any one will do.
            return Math.max(HEADER_SIZE, items.get(0).getAlignment());
        } catch (IndexOutOfBoundsException ex) {
            // Translate the exception.
            throw new IllegalArgumentException("items.size() == 0");
        } catch (NullPointerException ex) {
            // Translate the exception.
            throw new NullPointerException("items == null");
        }
    
public final java.util.ListgetItems()
Gets the underlying list of items.

return
{@code non-null;} the list

        return items;
    
private intheaderSize()
Get the size of the header of this list.

return
{@code >= 0;} the header size

        /*
         * Because of how this instance was set up, this is the same
         * as the alignment.
         */
        return getAlignment();
    
public ItemTypeitemType()
{@inheritDoc}

        return itemType;
    
protected voidplace0(Section addedTo, int offset)
{@inheritDoc}

        offset += headerSize();

        boolean first = true;
        int theSize = -1;
        int theAlignment = -1;

        for (OffsettedItem i : items) {
            int size = i.writeSize();
            if (first) {
                theSize = size;
                theAlignment = i.getAlignment();
                first = false;
            } else {
                if (size != theSize) {
                    throw new UnsupportedOperationException(
                            "item size mismatch");
                }
                if (i.getAlignment() != theAlignment) {
                    throw new UnsupportedOperationException(
                            "item alignment mismatch");
                }
            }

            offset = i.place(addedTo, offset) + size;
        }
    
public final java.lang.StringtoHuman()
{@inheritDoc}

        StringBuffer sb = new StringBuffer(100);
        boolean first = true;

        sb.append("{");

        for (OffsettedItem i : items) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(i.toHuman());
        }

        sb.append("}");
        return sb.toString();
    
public java.lang.StringtoString()
{@inheritDoc}

        StringBuffer sb = new StringBuffer(100);

        sb.append(getClass().getName());
        sb.append(items);

        return sb.toString();
    
private static intwriteSize(java.util.List items)
Calculates the write size for the given list.

param
items {@code non-null;} the list in question
return
{@code >= 0;} the write size

        /*
         * This class assumes all included items are the same size,
         * an assumption which is verified in place0().
         */
        OffsettedItem first = items.get(0);
        return (items.size() * first.writeSize()) + getAlignment(items);
    
protected voidwriteTo0(DexFile file, com.android.dexgen.util.AnnotatedOutput out)
{@inheritDoc}

        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);
        }