Search in sources :

Example 1 with ConstantPoolParser

use of com.android.dx.cf.cst.ConstantPoolParser in project buck by facebook.

the class DirectClassFile method parse0.

/**
     * Does the actual parsing.
     */
private void parse0() {
    if (bytes.size() < 10) {
        throw new ParseException("severely truncated class file");
    }
    if (observer != null) {
        observer.parsed(bytes, 0, 0, "begin classfile");
        observer.parsed(bytes, 0, 4, "magic: " + Hex.u4(getMagic0()));
        observer.parsed(bytes, 4, 2, "minor_version: " + Hex.u2(getMinorVersion0()));
        observer.parsed(bytes, 6, 2, "major_version: " + Hex.u2(getMajorVersion0()));
    }
    if (strictParse) {
        /* Make sure that this looks like a valid class file with a
             * version that we can handle.
             */
        if (!isGoodVersion(getMagic0(), getMinorVersion0(), getMajorVersion0())) {
            throw new ParseException("bad class file magic (" + Hex.u4(getMagic0()) + ") or version (" + Hex.u2(getMajorVersion0()) + "." + Hex.u2(getMinorVersion0()) + ")");
        }
    }
    ConstantPoolParser cpParser = new ConstantPoolParser(bytes);
    cpParser.setObserver(observer);
    pool = cpParser.getPool();
    pool.setImmutable();
    int at = cpParser.getEndOffset();
    // u2 access_flags;
    int accessFlags = bytes.getUnsignedShort(at);
    // u2 this_class;
    int cpi = bytes.getUnsignedShort(at + 2);
    thisClass = (CstType) pool.get(cpi);
    // u2 super_class;
    cpi = bytes.getUnsignedShort(at + 4);
    superClass = (CstType) pool.get0Ok(cpi);
    // u2 interfaces_count
    int count = bytes.getUnsignedShort(at + 6);
    if (observer != null) {
        observer.parsed(bytes, at, 2, "access_flags: " + AccessFlags.classString(accessFlags));
        observer.parsed(bytes, at + 2, 2, "this_class: " + thisClass);
        observer.parsed(bytes, at + 4, 2, "super_class: " + stringOrNone(superClass));
        observer.parsed(bytes, at + 6, 2, "interfaces_count: " + Hex.u2(count));
        if (count != 0) {
            observer.parsed(bytes, at + 8, 0, "interfaces:");
        }
    }
    at += 8;
    interfaces = makeTypeList(at, count);
    at += count * 2;
    if (strictParse) {
        /*
             * Make sure that the file/jar path matches the declared
             * package/class name.
             */
        String thisClassName = thisClass.getClassType().getClassName();
        if (!(filePath.endsWith(".class") && filePath.startsWith(thisClassName) && (filePath.length() == (thisClassName.length() + 6)))) {
            throw new ParseException("class name (" + thisClassName + ") does not match path (" + filePath + ")");
        }
    }
    /*
         * Only set the instance variable accessFlags here, since
         * that's what signals a successful parse of the first part of
         * the file (through the interfaces list).
         */
    this.accessFlags = accessFlags;
    FieldListParser flParser = new FieldListParser(this, thisClass, at, attributeFactory);
    flParser.setObserver(observer);
    fields = flParser.getList();
    at = flParser.getEndOffset();
    MethodListParser mlParser = new MethodListParser(this, thisClass, at, attributeFactory);
    mlParser.setObserver(observer);
    methods = mlParser.getList();
    at = mlParser.getEndOffset();
    AttributeListParser alParser = new AttributeListParser(this, AttributeFactory.CTX_CLASS, at, attributeFactory);
    alParser.setObserver(observer);
    attributes = alParser.getList();
    attributes.setImmutable();
    at = alParser.getEndOffset();
    if (at != bytes.size()) {
        throw new ParseException("extra bytes at end of class file, " + "at offset " + Hex.u4(at));
    }
    if (observer != null) {
        observer.parsed(bytes, at, 0, "end classfile");
    }
}
Also used : ParseException(com.android.dx.cf.iface.ParseException) CstString(com.android.dx.rop.cst.CstString) ConstantPoolParser(com.android.dx.cf.cst.ConstantPoolParser)

Aggregations

ConstantPoolParser (com.android.dx.cf.cst.ConstantPoolParser)1 ParseException (com.android.dx.cf.iface.ParseException)1 CstString (com.android.dx.rop.cst.CstString)1