FileDocCategorySizeDatePackage
AttributeListParser.javaAPI DocAndroid 5.1 API5000Thu Mar 12 22:18:30 GMT 2015com.android.dx.cf.direct

AttributeListParser

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

Fields Summary
private final DirectClassFile
cf
{@code 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
{@code non-null;} attribute factory to use
private final com.android.dx.cf.iface.StdAttributeList
list
{@code non-null;} list of parsed attributes
private int
endOffset
{@code >= -1;} the end offset of this list in the byte array of the classfile, or {@code -1} if not yet parsed
private com.android.dx.cf.iface.ParseObserver
observer
{@code null-ok;} parse observer, if any
Constructors Summary
public AttributeListParser(DirectClassFile cf, int context, int offset, AttributeFactory attributeFactory)
Constructs an instance.

param
cf {@code non-null;} class file to parse from
param
context attribute parsing context (see {@link AttributeFactory})
param
offset offset in {@code bytes} to the start of the list
param
attributeFactory {@code 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 {@code byte[]} which it came from.

return
{@code >= 0;} the end offset

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

return
{@code 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 {@code null-ok;} the observer

        this.observer = observer;