Search in sources :

Example 1 with ConstantPool

use of com.taobao.android.dx.rop.cst.ConstantPool in project atlas by alibaba.

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.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Example 2 with ConstantPool

use of com.taobao.android.dx.rop.cst.ConstantPool in project atlas by alibaba.

the class CfTranslator method translate0.

/**
     * Performs the main act of translation. This method is separated
     * from {@link #translate} just to keep things a bit simpler in
     * terms of exception handling.
     *
     * @param cf {@code non-null;} the class file
     * @param bytes {@code non-null;} contents of the file
     * @param cfOptions options for class translation
     * @param dexOptions options for dex output
     * @param dexFile {@code non-null;} dex output
     * @param optimizerOptions options for the optimizer
     * @return {@code non-null;} the translated class
     */
private static ClassDefItem translate0(DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, OptimizerOptions optimizerOptions, DexFile dexFile) {
    optimizerOptions.loadOptimizeLists(cfOptions.optimizeListFile, cfOptions.dontOptimizeListFile);
    // Build up a class to output.
    CstType thisClass = cf.getThisClass();
    int classAccessFlags = cf.getAccessFlags() & ~AccessFlags.ACC_SUPER;
    CstString sourceFile = (cfOptions.positionInfo == PositionList.NONE) ? null : cf.getSourceFile();
    ClassDefItem out = new ClassDefItem(thisClass, classAccessFlags, cf.getSuperclass(), cf.getInterfaces(), sourceFile);
    Annotations classAnnotations = AttributeTranslator.getClassAnnotations(cf, cfOptions);
    if (classAnnotations.size() != 0) {
        out.setClassAnnotations(classAnnotations, dexFile);
    }
    FieldIdsSection fieldIdsSection = dexFile.getFieldIds();
    MethodIdsSection methodIdsSection = dexFile.getMethodIds();
    processFields(cf, out, dexFile);
    processMethods(cf, cfOptions, dexOptions, optimizerOptions, out, dexFile);
    // intern constant pool method, field and type references
    ConstantPool constantPool = cf.getConstantPool();
    int constantPoolSize = constantPool.size();
    for (int i = 0; i < constantPoolSize; i++) {
        Constant constant = constantPool.getOrNull(i);
        if (constant instanceof CstMethodRef) {
            methodIdsSection.intern((CstBaseMethodRef) constant);
        } else if (constant instanceof CstInterfaceMethodRef) {
            methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
        } else if (constant instanceof CstFieldRef) {
            fieldIdsSection.intern((CstFieldRef) constant);
        } else if (constant instanceof CstEnumRef) {
            fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
        }
    }
    return out;
}
Also used : Constant(com.taobao.android.dx.rop.cst.Constant) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) CstString(com.taobao.android.dx.rop.cst.CstString) CstFieldRef(com.taobao.android.dx.rop.cst.CstFieldRef) CstEnumRef(com.taobao.android.dx.rop.cst.CstEnumRef) FieldIdsSection(com.taobao.android.dx.dex.file.FieldIdsSection) MethodIdsSection(com.taobao.android.dx.dex.file.MethodIdsSection) CstMethodRef(com.taobao.android.dx.rop.cst.CstMethodRef) Annotations(com.taobao.android.dx.rop.annotation.Annotations) ClassDefItem(com.taobao.android.dx.dex.file.ClassDefItem) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstType(com.taobao.android.dx.rop.cst.CstType) CstInterfaceMethodRef(com.taobao.android.dx.rop.cst.CstInterfaceMethodRef)

Example 3 with ConstantPool

use of com.taobao.android.dx.rop.cst.ConstantPool in project atlas by alibaba.

the class StdAttributeFactory method constantValue.

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

Example 4 with ConstantPool

use of com.taobao.android.dx.rop.cst.ConstantPool in project atlas by alibaba.

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.taobao.android.dx.rop.cst.ConstantPool) CstType(com.taobao.android.dx.rop.cst.CstType) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) InnerClassList(com.taobao.android.dx.cf.attrib.InnerClassList) AttInnerClasses(com.taobao.android.dx.cf.attrib.AttInnerClasses)

Example 5 with ConstantPool

use of com.taobao.android.dx.rop.cst.ConstantPool in project atlas by alibaba.

the class StdAttributeFactory method enclosingMethod.

/**
     * Parses an {@code EnclosingMethod} attribute.
     */
private Attribute enclosingMethod(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 4) {
        throwBadLength(4);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    CstType type = (CstType) pool.get(idx);
    idx = bytes.getUnsignedShort(offset + 2);
    CstNat method = (CstNat) pool.get0Ok(idx);
    Attribute result = new AttEnclosingMethod(type, method);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "class: " + type);
        observer.parsed(bytes, offset + 2, 2, "method: " + DirectClassFile.stringOrNone(method));
    }
    return result;
}
Also used : CstNat(com.taobao.android.dx.rop.cst.CstNat) AttEnclosingMethod(com.taobao.android.dx.cf.attrib.AttEnclosingMethod) Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstType(com.taobao.android.dx.rop.cst.CstType) ByteArray(com.taobao.android.dx.util.ByteArray)

Aggregations

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