Search in sources :

Example 6 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project buck by facebook.

the class AttributeFactory method parse.

/**
     * Parses and makes an attribute based on the bytes at the
     * indicated position in the given array. This method figures out
     * the name, and then does all the setup to call on to {@link #parse0},
     * which does the actual construction.
     *
     * @param cf {@code non-null;} class file to parse from
     * @param context context to parse in; one of the {@code CTX_*}
     * constants
     * @param offset offset into {@code dcf}'s {@code bytes}
     * to start parsing at
     * @param observer {@code null-ok;} parse observer to report to, if any
     * @return {@code non-null;} an appropriately-constructed {@link Attribute}
     */
public final Attribute parse(DirectClassFile cf, int context, int offset, ParseObserver observer) {
    if (cf == null) {
        throw new NullPointerException("cf == null");
    }
    if ((context < 0) || (context >= CTX_COUNT)) {
        throw new IllegalArgumentException("bad context");
    }
    CstString name = null;
    try {
        ByteArray bytes = cf.getBytes();
        ConstantPool pool = cf.getConstantPool();
        int nameIdx = bytes.getUnsignedShort(offset);
        int length = bytes.getInt(offset + 2);
        name = (CstString) pool.get(nameIdx);
        if (observer != null) {
            observer.parsed(bytes, offset, 2, "name: " + name.toHuman());
            observer.parsed(bytes, offset + 2, 4, "length: " + Hex.u4(length));
        }
        return parse0(cf, context, name.getString(), offset + 6, length, observer);
    } catch (ParseException ex) {
        ex.addContext("...while parsing " + ((name != null) ? (name.toHuman() + " ") : "") + "attribute at offset " + Hex.u4(offset));
        throw ex;
    }
}
Also used : ConstantPool(com.android.dx.rop.cst.ConstantPool) CstString(com.android.dx.rop.cst.CstString) ByteArray(com.android.dx.util.ByteArray) ParseException(com.android.dx.cf.iface.ParseException)

Example 7 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project J2ME-Loader by nikita36078.

the class AttributeFactory method parse.

/**
 * Parses and makes an attribute based on the bytes at the
 * indicated position in the given array. This method figures out
 * the name, and then does all the setup to call on to {@link #parse0},
 * which does the actual construction.
 *
 * @param cf {@code non-null;} class file to parse from
 * @param context context to parse in; one of the {@code CTX_*}
 * constants
 * @param offset offset into {@code dcf}'s {@code bytes}
 * to start parsing at
 * @param observer {@code null-ok;} parse observer to report to, if any
 * @return {@code non-null;} an appropriately-constructed {@link Attribute}
 */
public final Attribute parse(DirectClassFile cf, int context, int offset, ParseObserver observer) {
    if (cf == null) {
        throw new NullPointerException("cf == null");
    }
    if ((context < 0) || (context >= CTX_COUNT)) {
        throw new IllegalArgumentException("bad context");
    }
    CstString name = null;
    try {
        ByteArray bytes = cf.getBytes();
        ConstantPool pool = cf.getConstantPool();
        int nameIdx = bytes.getUnsignedShort(offset);
        int length = bytes.getInt(offset + 2);
        name = (CstString) pool.get(nameIdx);
        if (observer != null) {
            observer.parsed(bytes, offset, 2, "name: " + name.toHuman());
            observer.parsed(bytes, offset + 2, 4, "length: " + Hex.u4(length));
        }
        return parse0(cf, context, name.getString(), offset + 6, length, observer);
    } catch (ParseException ex) {
        ex.addContext("...while parsing " + ((name != null) ? (name.toHuman() + " ") : "") + "attribute at offset " + Hex.u4(offset));
        throw ex;
    }
}
Also used : ConstantPool(com.android.dx.rop.cst.ConstantPool) CstString(com.android.dx.rop.cst.CstString) ByteArray(com.android.dx.util.ByteArray) ParseException(com.android.dx.cf.iface.ParseException)

Example 8 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project J2ME-Loader by nikita36078.

the class AttributeFactory method parse0.

/**
 * Parses attribute content. The base class implements this by constructing
 * an instance of {@link RawAttribute}. Subclasses are expected to
 * override this to do something better in most cases.
 *
 * @param cf {@code non-null;} class file to parse from
 * @param context context to parse in; one of the {@code CTX_*}
 * constants
 * @param name {@code non-null;} the attribute name
 * @param offset offset into {@code bytes} to start parsing at; this
 * is the offset to the start of attribute data, not to the header
 * @param length the length of the attribute data
 * @param observer {@code null-ok;} parse observer to report to, if any
 * @return {@code non-null;} an appropriately-constructed {@link Attribute}
 */
protected Attribute parse0(DirectClassFile cf, int context, String name, int offset, int length, ParseObserver observer) {
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    Attribute result = new RawAttribute(name, bytes, offset, length, pool);
    if (observer != null) {
        observer.parsed(bytes, offset, length, "attribute data");
    }
    return result;
}
Also used : Attribute(com.android.dx.cf.iface.Attribute) RawAttribute(com.android.dx.cf.attrib.RawAttribute) ConstantPool(com.android.dx.rop.cst.ConstantPool) RawAttribute(com.android.dx.cf.attrib.RawAttribute) ByteArray(com.android.dx.util.ByteArray)

Example 9 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project J2ME-Loader by nikita36078.

the class StdAttributeFactory method sourceFile.

/**
 * Parses a {@code SourceFile} attribute.
 */
private Attribute sourceFile(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 2) {
        throwBadLength(2);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    CstString cst = (CstString) pool.get(idx);
    Attribute result = new AttSourceFile(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "source: " + cst);
    }
    return result;
}
Also used : Attribute(com.android.dx.cf.iface.Attribute) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstString(com.android.dx.rop.cst.CstString) AttSourceFile(com.android.dx.cf.attrib.AttSourceFile) ByteArray(com.android.dx.util.ByteArray)

Example 10 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project J2ME-Loader by nikita36078.

the class StdAttributeFactory method innerClasses.

/**
 * Parses an {@code InnerClasses} attribute.
 */
private Attribute innerClasses(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    // number_of_classes
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "number_of_classes: " + Hex.u2(count));
    }
    offset += 2;
    length -= 2;
    if (length != (count * 8)) {
        throwBadLength((count * 8) + 2);
    }
    InnerClassList list = new InnerClassList(count);
    for (int i = 0; i < count; i++) {
        int innerClassIdx = bytes.getUnsignedShort(offset);
        int outerClassIdx = bytes.getUnsignedShort(offset + 2);
        int nameIdx = bytes.getUnsignedShort(offset + 4);
        int accessFlags = bytes.getUnsignedShort(offset + 6);
        CstType innerClass = (CstType) pool.get(innerClassIdx);
        CstType outerClass = (CstType) pool.get0Ok(outerClassIdx);
        CstString name = (CstString) pool.get0Ok(nameIdx);
        list.set(i, innerClass, outerClass, name, accessFlags);
        if (observer != null) {
            observer.parsed(bytes, offset, 2, "inner_class: " + DirectClassFile.stringOrNone(innerClass));
            observer.parsed(bytes, offset + 2, 2, "  outer_class: " + DirectClassFile.stringOrNone(outerClass));
            observer.parsed(bytes, offset + 4, 2, "  name: " + DirectClassFile.stringOrNone(name));
            observer.parsed(bytes, offset + 6, 2, "  access_flags: " + AccessFlags.innerClassString(accessFlags));
        }
        offset += 8;
    }
    list.setImmutable();
    return new AttInnerClasses(list);
}
Also used : ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) CstString(com.android.dx.rop.cst.CstString) ByteArray(com.android.dx.util.ByteArray) InnerClassList(com.android.dx.cf.attrib.InnerClassList) AttInnerClasses(com.android.dx.cf.attrib.AttInnerClasses)

Aggregations

ConstantPool (com.android.dx.rop.cst.ConstantPool)20 ByteArray (com.android.dx.util.ByteArray)18 CstString (com.android.dx.rop.cst.CstString)12 Attribute (com.android.dx.cf.iface.Attribute)10 CstType (com.android.dx.rop.cst.CstType)8 ParseException (com.android.dx.cf.iface.ParseException)4 StdAttributeList (com.android.dx.cf.iface.StdAttributeList)4 CstNat (com.android.dx.rop.cst.CstNat)4 TypedConstant (com.android.dx.rop.cst.TypedConstant)4 AttCode (com.android.dx.cf.attrib.AttCode)2 AttConstantValue (com.android.dx.cf.attrib.AttConstantValue)2 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)2 AttInnerClasses (com.android.dx.cf.attrib.AttInnerClasses)2 AttSignature (com.android.dx.cf.attrib.AttSignature)2 AttSourceFile (com.android.dx.cf.attrib.AttSourceFile)2 InnerClassList (com.android.dx.cf.attrib.InnerClassList)2 RawAttribute (com.android.dx.cf.attrib.RawAttribute)2 ByteCatchList (com.android.dx.cf.code.ByteCatchList)2 BytecodeArray (com.android.dx.cf.code.BytecodeArray)2 Member (com.android.dx.cf.iface.Member)2