Search in sources :

Example 1 with ParseException

use of com.android.dx.cf.iface.ParseException in project buck by facebook.

the class ConstantPoolParser method determineOffsets.

/**
     * Populates {@link #offsets} and also completely parse utf8 constants.
     */
private void determineOffsets() {
    // offset from the start of the file to the first cst
    int at = 10;
    int lastCategory;
    for (int i = 1; i < offsets.length; i += lastCategory) {
        offsets[i] = at;
        int tag = bytes.getUnsignedByte(at);
        try {
            switch(tag) {
                case CONSTANT_Integer:
                case CONSTANT_Float:
                case CONSTANT_Fieldref:
                case CONSTANT_Methodref:
                case CONSTANT_InterfaceMethodref:
                case CONSTANT_NameAndType:
                    {
                        lastCategory = 1;
                        at += 5;
                        break;
                    }
                case CONSTANT_Long:
                case CONSTANT_Double:
                    {
                        lastCategory = 2;
                        at += 9;
                        break;
                    }
                case CONSTANT_Class:
                case CONSTANT_String:
                    {
                        lastCategory = 1;
                        at += 3;
                        break;
                    }
                case CONSTANT_Utf8:
                    {
                        lastCategory = 1;
                        at += bytes.getUnsignedShort(at + 1) + 3;
                        break;
                    }
                case CONSTANT_MethodHandle:
                    {
                        throw new ParseException("MethodHandle not supported");
                    }
                case CONSTANT_MethodType:
                    {
                        throw new ParseException("MethodType not supported");
                    }
                case CONSTANT_InvokeDynamic:
                    {
                        throw new ParseException("InvokeDynamic not supported");
                    }
                default:
                    {
                        throw new ParseException("unknown tag byte: " + Hex.u1(tag));
                    }
            }
        } catch (ParseException ex) {
            ex.addContext("...while preparsing cst " + Hex.u2(i) + " at offset " + Hex.u4(at));
            throw ex;
        }
    }
    endOffset = at;
}
Also used : ParseException(com.android.dx.cf.iface.ParseException)

Example 2 with ParseException

use of com.android.dx.cf.iface.ParseException in project buck by facebook.

the class AnnotationParser method parseValue.

/**
     * Parses an annotation value.
     *
     * @return {@code non-null;} the parsed value
     */
private Constant parseValue() throws IOException {
    int tag = input.readUnsignedByte();
    if (observer != null) {
        CstString humanTag = new CstString(Character.toString((char) tag));
        parsed(1, "tag: " + humanTag.toQuoted());
    }
    switch(tag) {
        case 'B':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstByte.make(value.getValue());
            }
        case 'C':
            {
                CstInteger value = (CstInteger) parseConstant();
                int intValue = value.getValue();
                return CstChar.make(value.getValue());
            }
        case 'D':
            {
                CstDouble value = (CstDouble) parseConstant();
                return value;
            }
        case 'F':
            {
                CstFloat value = (CstFloat) parseConstant();
                return value;
            }
        case 'I':
            {
                CstInteger value = (CstInteger) parseConstant();
                return value;
            }
        case 'J':
            {
                CstLong value = (CstLong) parseConstant();
                return value;
            }
        case 'S':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstShort.make(value.getValue());
            }
        case 'Z':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstBoolean.make(value.getValue());
            }
        case 'c':
            {
                int classInfoIndex = input.readUnsignedShort();
                CstString value = (CstString) pool.get(classInfoIndex);
                Type type = Type.internReturnType(value.getString());
                if (observer != null) {
                    parsed(2, "class_info: " + type.toHuman());
                }
                return new CstType(type);
            }
        case 's':
            {
                return parseConstant();
            }
        case 'e':
            {
                requireLength(4);
                int typeNameIndex = input.readUnsignedShort();
                int constNameIndex = input.readUnsignedShort();
                CstString typeName = (CstString) pool.get(typeNameIndex);
                CstString constName = (CstString) pool.get(constNameIndex);
                if (observer != null) {
                    parsed(2, "type_name: " + typeName.toHuman());
                    parsed(2, "const_name: " + constName.toHuman());
                }
                return new CstEnumRef(new CstNat(constName, typeName));
            }
        case '@':
            {
                Annotation annotation = parseAnnotation(AnnotationVisibility.EMBEDDED);
                return new CstAnnotation(annotation);
            }
        case '[':
            {
                requireLength(2);
                int numValues = input.readUnsignedShort();
                CstArray.List list = new CstArray.List(numValues);
                if (observer != null) {
                    parsed(2, "num_values: " + numValues);
                    changeIndent(1);
                }
                for (int i = 0; i < numValues; i++) {
                    if (observer != null) {
                        changeIndent(-1);
                        parsed(0, "element_value[" + i + "]:");
                        changeIndent(1);
                    }
                    list.set(i, parseValue());
                }
                if (observer != null) {
                    changeIndent(-1);
                }
                list.setImmutable();
                return new CstArray(list);
            }
        default:
            {
                throw new ParseException("unknown annotation tag: " + Hex.u1(tag));
            }
    }
}
Also used : CstFloat(com.android.dx.rop.cst.CstFloat) CstNat(com.android.dx.rop.cst.CstNat) CstArray(com.android.dx.rop.cst.CstArray) CstLong(com.android.dx.rop.cst.CstLong) CstString(com.android.dx.rop.cst.CstString) CstAnnotation(com.android.dx.rop.cst.CstAnnotation) CstDouble(com.android.dx.rop.cst.CstDouble) CstEnumRef(com.android.dx.rop.cst.CstEnumRef) Annotation(com.android.dx.rop.annotation.Annotation) CstAnnotation(com.android.dx.rop.cst.CstAnnotation) Type(com.android.dx.rop.type.Type) CstType(com.android.dx.rop.cst.CstType) CstInteger(com.android.dx.rop.cst.CstInteger) CstType(com.android.dx.rop.cst.CstType) AnnotationsList(com.android.dx.rop.annotation.AnnotationsList) ParseException(com.android.dx.cf.iface.ParseException)

Example 3 with ParseException

use of com.android.dx.cf.iface.ParseException 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 4 with ParseException

use of com.android.dx.cf.iface.ParseException in project buck by facebook.

the class AttributeListParser method parse.

/**
     * Does the actual parsing.
     */
private void parse() {
    int sz = list.size();
    // Skip the count.
    int at = offset + 2;
    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;
}
Also used : Attribute(com.android.dx.cf.iface.Attribute) ByteArray(com.android.dx.util.ByteArray) ParseException(com.android.dx.cf.iface.ParseException)

Example 5 with ParseException

use of com.android.dx.cf.iface.ParseException in project buck by facebook.

the class MemberListParser method parse.

/**
     * Does the actual parsing.
     */
private void parse() {
    int attributeContext = getAttributeContext();
    int count = getCount();
    // Skip the count.
    int at = offset + 2;
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    if (observer != null) {
        observer.parsed(bytes, offset, 2, humanName() + "s_count: " + Hex.u2(count));
    }
    for (int i = 0; i < count; i++) {
        try {
            int accessFlags = bytes.getUnsignedShort(at);
            int nameIdx = bytes.getUnsignedShort(at + 2);
            int descIdx = bytes.getUnsignedShort(at + 4);
            CstString name = (CstString) pool.get(nameIdx);
            CstString desc = (CstString) pool.get(descIdx);
            if (observer != null) {
                observer.startParsingMember(bytes, at, name.getString(), desc.getString());
                observer.parsed(bytes, at, 0, "\n" + humanName() + "s[" + i + "]:\n");
                observer.changeIndent(1);
                observer.parsed(bytes, at, 2, "access_flags: " + humanAccessFlags(accessFlags));
                observer.parsed(bytes, at + 2, 2, "name: " + name.toHuman());
                observer.parsed(bytes, at + 4, 2, "descriptor: " + desc.toHuman());
            }
            at += 6;
            AttributeListParser parser = new AttributeListParser(cf, attributeContext, at, attributeFactory);
            parser.setObserver(observer);
            at = parser.getEndOffset();
            StdAttributeList attributes = parser.getList();
            attributes.setImmutable();
            CstNat nat = new CstNat(name, desc);
            Member member = set(i, accessFlags, nat, attributes);
            if (observer != null) {
                observer.changeIndent(-1);
                observer.parsed(bytes, at, 0, "end " + humanName() + "s[" + i + "]\n");
                observer.endParsingMember(bytes, at, name.getString(), desc.getString(), member);
            }
        } catch (ParseException ex) {
            ex.addContext("...while parsing " + humanName() + "s[" + i + "]");
            throw ex;
        } catch (RuntimeException ex) {
            ParseException pe = new ParseException(ex);
            pe.addContext("...while parsing " + humanName() + "s[" + i + "]");
            throw pe;
        }
    }
    endOffset = at;
}
Also used : StdAttributeList(com.android.dx.cf.iface.StdAttributeList) CstNat(com.android.dx.rop.cst.CstNat) 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) Member(com.android.dx.cf.iface.Member)

Aggregations

ParseException (com.android.dx.cf.iface.ParseException)9 CstString (com.android.dx.rop.cst.CstString)6 ByteArray (com.android.dx.util.ByteArray)4 CstNat (com.android.dx.rop.cst.CstNat)3 Constant (com.android.dx.rop.cst.Constant)2 ConstantPool (com.android.dx.rop.cst.ConstantPool)2 CstInteger (com.android.dx.rop.cst.CstInteger)2 CstType (com.android.dx.rop.cst.CstType)2 ConstantPoolParser (com.android.dx.cf.cst.ConstantPoolParser)1 Attribute (com.android.dx.cf.iface.Attribute)1 Member (com.android.dx.cf.iface.Member)1 StdAttributeList (com.android.dx.cf.iface.StdAttributeList)1 Annotation (com.android.dx.rop.annotation.Annotation)1 AnnotationsList (com.android.dx.rop.annotation.AnnotationsList)1 CstAnnotation (com.android.dx.rop.cst.CstAnnotation)1 CstArray (com.android.dx.rop.cst.CstArray)1 CstDouble (com.android.dx.rop.cst.CstDouble)1 CstEnumRef (com.android.dx.rop.cst.CstEnumRef)1 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)1 CstFloat (com.android.dx.rop.cst.CstFloat)1