FileDocCategorySizeDatePackage
StdConstantPool.javaAPI DocAndroid 5.1 API4024Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.rop.cst

StdConstantPool

public final class StdConstantPool extends com.android.dexgen.util.MutabilityControl implements ConstantPool
Standard implementation of {@link ConstantPool}, which directly stores an array of {@link Constant} objects and can be made immutable.

Fields Summary
private final Constant[]
entries
{@code non-null;} array of entries
Constructors Summary
public StdConstantPool(int size)
Constructs an instance. All indices initially contain {@code null}.

param
size the size of the pool; this corresponds to the class file field {@code constant_pool_count}, and is in fact always at least one more than the actual size of the constant pool, as element {@code 0} is always invalid.

        super(size > 1);

        if (size < 1) {
            throw new IllegalArgumentException("size < 1");
        }

        entries = new Constant[size];
    
Methods Summary
public Constantget(int n)
{@inheritDoc}

        try {
            Constant result = entries[n];

            if (result == null) {
                throwInvalid(n);
            }

            return result;
        } catch (IndexOutOfBoundsException ex) {
            // Translate the exception.
            return throwInvalid(n);
        }
    
public Constantget0Ok(int n)
{@inheritDoc}

        if (n == 0) {
            return null;
        }

        return get(n);
    
public ConstantgetOrNull(int n)
{@inheritDoc}

        try {
            return entries[n];
        } catch (IndexOutOfBoundsException ex) {
            // Translate the exception.
            return throwInvalid(n);
        }
    
public voidset(int n, Constant cst)
Sets the entry at the given index.

param
n {@code >= 1, < size();} which entry
param
cst {@code null-ok;} the constant to store

        throwIfImmutable();

        boolean cat2 = (cst != null) && cst.isCategory2();

        if (n < 1) {
            throw new IllegalArgumentException("n < 1");
        }

        if (cat2) {
            // Storing a category-2 entry nulls out the next index.
            if (n == (entries.length - 1)) {
                throw new IllegalArgumentException("(n == size - 1) && " +
                                                   "cst.isCategory2()");
            }
            entries[n + 1] = null;
        }

        if ((cst != null) && (entries[n] == null)) {
            /*
             * Overwriting the second half of a category-2 entry nulls out
             * the first half.
             */
            Constant prev = entries[n - 1];
            if ((prev != null) && prev.isCategory2()) {
                entries[n - 1] = null;
            }
        }

        entries[n] = cst;
    
public intsize()
{@inheritDoc}

        return entries.length;
    
private static ConstantthrowInvalid(int idx)
Throws the right exception for an invalid cpi.

param
idx the bad cpi
return
never
throws
ExceptionWithContext always thrown

        throw new ExceptionWithContext("invalid constant pool index " +
                                       Hex.u2(idx));