FileDocCategorySizeDatePackage
AttributeListParser.javaAPI DocAndroid 1.5 API4941Wed May 06 22:41:02 BST 2009com.android.dx.cf.direct

AttributeListParser

public final class AttributeListParser extends Object
Parser for lists of attributes.

Fields Summary
private final DirectClassFile
cf
non-null; the class file to parse from
private final int
context
attribute parsing context
private final int
offset
offset in the byte array of the classfile to the start of the list
private final AttributeFactory
attributeFactory
non-null; attribute factory to use
private final com.android.dx.cf.iface.StdAttributeList
list
non-null; list of parsed attributes
private int
endOffset
>= -1; the end offset of this list in the byte array of the classfile, or -1 if not yet parsed
private com.android.dx.cf.iface.ParseObserver
observer
null-ok; parse observer, if any
Constructors Summary
public AttributeListParser(DirectClassFile cf, int context, int offset, AttributeFactory attributeFactory)
Constructs an instance.

param
cf non-null; class file to parse from
param
context attribute parsing context (see {@link AttributeFactory})
param
offset offset in bytes to the start of the list
param
attributeFactory non-null; attribute factory to use

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

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

        int size = cf.getBytes().getUnsignedShort(offset);

        this.cf = cf;
        this.context = context;
        this.offset = offset;
        this.attributeFactory = attributeFactory;
        this.list = new StdAttributeList(size);
        this.endOffset = -1;
    
Methods Summary
public intgetEndOffset()
Gets the end offset of this constant pool in the byte[] which it came from.

return
>= 0; the end offset

        parseIfNecessary();
        return endOffset;
    
public com.android.dx.cf.iface.StdAttributeListgetList()
Gets the parsed list.

return
non-null; the list

        parseIfNecessary();
        return list;
    
private voidparse()
Does the actual parsing.

        int sz = list.size();
        int at = offset + 2; // Skip the count.

        ByteArray bytes = cf.getBytes();

        if (observer != null) {
            observer.parsed(bytes, offset, 2,
                            "attributes_count: " + Hex.u2(sz));
        }

        for (int i = 0; i < sz; i++) {
            try {
                if (observer != null) {
                    observer.parsed(bytes, at, 0,
                                    "\nattributes[" + i + "]:\n");
                    observer.changeIndent(1);
                }

                Attribute attrib =
                    attributeFactory.parse(cf, context, at, observer);

                at += attrib.byteLength();
                list.set(i, attrib);

                if (observer != null) {
                    observer.changeIndent(-1);
                    observer.parsed(bytes, at, 0,
                                    "end attributes[" + i + "]\n");
                }
            } catch (ParseException ex) {
                ex.addContext("...while parsing attributes[" + i + "]");
                throw ex;
            } catch (RuntimeException ex) {
                ParseException pe = new ParseException(ex);
                pe.addContext("...while parsing attributes[" + i + "]");
                throw pe;
            }
        }

        endOffset = at;
    
private voidparseIfNecessary()
Runs {@link #parse} if it has not yet been run successfully.

        if (endOffset < 0) {
            parse();
        }
    
public voidsetObserver(com.android.dx.cf.iface.ParseObserver observer)
Sets the parse observer for this instance.

param
observer null-ok; the observer

        this.observer = observer;