FileDocCategorySizeDatePackage
ClassDefsSection.javaAPI DocAndroid 1.5 API5443Wed May 06 22:41:02 BST 2009com.android.dx.dex.file

ClassDefsSection

public final class ClassDefsSection extends UniformItemSection
Class definitions list section of a .dex file.

Fields Summary
private final TreeMap
classDefs
non-null; map from type constants for classes to {@link ClassDefItem} instances that define those classes
private ArrayList
orderedDefs
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 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 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.dx.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.dx.rop.type.Type type, int idx, int maxDepth)
Helper for {@link #orderItems}, which recursively assigns indices to classes.

param
type null-ok; type ref to assign, if any
param
idx >= 0; the next index to assign
param
maxDepth maximum recursion depth; if negative, this will throw an exception indicating class definition circularity
return
>= 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.dx.util.AnnotatedOutput out)
Writes the portion of the file header that refers to this instance.

param
out 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);