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

ClassDefsSection

public final class ClassDefsSection extends UniformItemSection
Class definitions list section of a {@code .dex} file.

Fields Summary
private final TreeMap
classDefs
{@code non-null;} map from type constants for classes to {@link ClassDefItem} instances that define those classes
private ArrayList
orderedDefs
{@code null-ok;} ordered list of classes; set in {@link #orderItems}
Constructors Summary
public ClassDefsSection(DexFile file)
Constructs an instance. The file offset is initially unknown.

param
file {@code non-null;} file that this instance is part of

        super("class_defs", file, 4);

        classDefs = new TreeMap<Type, ClassDefItem>();
        orderedDefs = null;
    
Methods Summary
public voidadd(ClassDefItem clazz)
Adds an element to this instance. It is illegal to attempt to add more than one class with the same name.

param
clazz {@code non-null;} the class def to add

        Type type;

        try {
            type = clazz.getThisClass().getClassType();
        } catch (NullPointerException ex) {
            // Elucidate the exception.
            throw new NullPointerException("clazz == null");
        }

        throwIfPrepared();

        if (classDefs.get(type) != null) {
            throw new IllegalArgumentException("already added: " + type);
        }

        classDefs.put(type, clazz);
    
public IndexedItemget(com.android.dexgen.rop.cst.Constant cst)
{@inheritDoc}

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

        throwIfNotPrepared();

        Type type = ((CstType) cst).getClassType();
        IndexedItem result = classDefs.get(type);

        if (result == null) {
            throw new IllegalArgumentException("not found");
        }

        return result;
    
public java.util.Collectionitems()
{@inheritDoc}

        if (orderedDefs != null) {
            return orderedDefs;
        }

        return classDefs.values();
    
protected voidorderItems()
{@inheritDoc}

        int sz = classDefs.size();
        int idx = 0;

        orderedDefs = new ArrayList<ClassDefItem>(sz);

        /*
         * Iterate over all the classes, recursively assigning an
         * index to each, implicitly skipping the ones that have
         * already been assigned by the time this (top-level)
         * iteration reaches them.
         */
        for (Type type : classDefs.keySet()) {
            idx = orderItems0(type, idx, sz - idx);
        }
    
private intorderItems0(com.android.dexgen.rop.type.Type type, int idx, int maxDepth)
Helper for {@link #orderItems}, which recursively assigns indices to classes.

param
type {@code null-ok;} type ref to assign, if any
param
idx {@code >= 0;} the next index to assign
param
maxDepth maximum recursion depth; if negative, this will throw an exception indicating class definition circularity
return
{@code >= 0;} the next index to assign

        ClassDefItem c = classDefs.get(type);

        if ((c == null) || (c.hasIndex())) {
            return idx;
        }

        if (maxDepth < 0) {
            throw new RuntimeException("class circularity with " + type);
        }

        maxDepth--;

        CstType superclassCst = c.getSuperclass();
        if (superclassCst != null) {
            Type superclass = superclassCst.getClassType();
            idx = orderItems0(superclass, idx, maxDepth);
        }

        TypeList interfaces = c.getInterfaces();
        int sz = interfaces.size();
        for (int i = 0; i < sz; i++) {
            idx = orderItems0(interfaces.getType(i), idx, maxDepth);
        }

        c.setIndex(idx);
        orderedDefs.add(c);
        return idx + 1;
    
public voidwriteHeaderPart(com.android.dexgen.util.AnnotatedOutput out)
Writes the portion of the file header that refers to this instance.

param
out {@code non-null;} where to write

        throwIfNotPrepared();

        int sz = classDefs.size();
        int offset = (sz == 0) ? 0 : getFileOffset();

        if (out.annotates()) {
            out.annotate(4, "class_defs_size: " + Hex.u4(sz));
            out.annotate(4, "class_defs_off:  " + Hex.u4(offset));
        }

        out.writeInt(sz);
        out.writeInt(offset);