Search in sources :

Example 51 with CstType

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

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 context
     * @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
     * @return {@code non-null;} the translated class
     */
private static ClassDefItem translate0(DxContext context, DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
    context.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(context, cf, cfOptions, dexOptions, 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.android.dx.rop.cst.Constant) TypedConstant(com.android.dx.rop.cst.TypedConstant) CstString(com.android.dx.rop.cst.CstString) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstEnumRef(com.android.dx.rop.cst.CstEnumRef) FieldIdsSection(com.android.dx.dex.file.FieldIdsSection) MethodIdsSection(com.android.dx.dex.file.MethodIdsSection) CstMethodRef(com.android.dx.rop.cst.CstMethodRef) Annotations(com.android.dx.rop.annotation.Annotations) ClassDefItem(com.android.dx.dex.file.ClassDefItem) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) CstInterfaceMethodRef(com.android.dx.rop.cst.CstInterfaceMethodRef)

Example 52 with CstType

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

the class AttributeTranslator method translateEnclosingMethod.

/**
     * Gets the {@code EnclosingMethod} attribute out of a given
     * {@link AttributeList}, if any, translating it to an annotation.
     * If the class really has an enclosing method, this returns an
     * {@code EnclosingMethod} annotation; if not, this returns
     * an {@code EnclosingClass} annotation.
     *
     * @param attribs {@code non-null;} the attributes list to search in
     * @return {@code null-ok;} the converted {@code EnclosingMethod} or
     * {@code EnclosingClass} annotation, if there was an
     * attribute to translate
     */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod) attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);
    if (enclosingMethod == null) {
        return null;
    }
    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();
    if (nat == null) {
        /*
             * Dalvik doesn't use EnclosingMethod annotations unless
             * there really is an enclosing method. Anonymous classes
             * are unambiguously identified by having an InnerClass
             * annotation with an empty name along with an appropriate
             * EnclosingClass.
             */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }
    return AnnotationUtils.makeEnclosingMethod(new CstMethodRef(enclosingClass, nat));
}
Also used : CstNat(com.android.dx.rop.cst.CstNat) AttEnclosingMethod(com.android.dx.cf.attrib.AttEnclosingMethod) CstType(com.android.dx.rop.cst.CstType) CstMethodRef(com.android.dx.rop.cst.CstMethodRef)

Example 53 with CstType

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

the class OutputFinisher method assignIndices.

/**
     * Helper for {@link #assignIndices} which does assignment for one
     * instruction.
     *
     * @param insn {@code non-null;} the instruction
     * @param callback {@code non-null;} the callback
     */
private static void assignIndices(CstInsn insn, DalvCode.AssignIndicesCallback callback) {
    Constant cst = insn.getConstant();
    int index = callback.getIndex(cst);
    if (index >= 0) {
        insn.setIndex(index);
    }
    if (cst instanceof CstMemberRef) {
        CstMemberRef member = (CstMemberRef) cst;
        CstType definer = member.getDefiningClass();
        index = callback.getIndex(definer);
        if (index >= 0) {
            insn.setClassIndex(index);
        }
    }
}
Also used : CstMemberRef(com.android.dx.rop.cst.CstMemberRef) Constant(com.android.dx.rop.cst.Constant) CstType(com.android.dx.rop.cst.CstType)

Example 54 with CstType

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

the class ConstantPoolParser method parse0.

/**
     * Parses the constant for the given index if it hasn't already been
     * parsed, also storing it in the constant pool. This will also
     * have the side effect of parsing any entries the indicated one
     * depends on.
     *
     * @param idx which constant
     * @return {@code non-null;} the parsed constant
     */
private Constant parse0(int idx, BitSet wasUtf8) {
    Constant cst = pool.getOrNull(idx);
    if (cst != null) {
        return cst;
    }
    int at = offsets[idx];
    try {
        int tag = bytes.getUnsignedByte(at);
        switch(tag) {
            case CONSTANT_Utf8:
                {
                    cst = parseUtf8(at);
                    wasUtf8.set(idx);
                    break;
                }
            case CONSTANT_Integer:
                {
                    int value = bytes.getInt(at + 1);
                    cst = CstInteger.make(value);
                    break;
                }
            case CONSTANT_Float:
                {
                    int bits = bytes.getInt(at + 1);
                    cst = CstFloat.make(bits);
                    break;
                }
            case CONSTANT_Long:
                {
                    long value = bytes.getLong(at + 1);
                    cst = CstLong.make(value);
                    break;
                }
            case CONSTANT_Double:
                {
                    long bits = bytes.getLong(at + 1);
                    cst = CstDouble.make(bits);
                    break;
                }
            case CONSTANT_Class:
                {
                    int nameIndex = bytes.getUnsignedShort(at + 1);
                    CstString name = (CstString) parse0(nameIndex, wasUtf8);
                    cst = new CstType(Type.internClassName(name.getString()));
                    break;
                }
            case CONSTANT_String:
                {
                    int stringIndex = bytes.getUnsignedShort(at + 1);
                    cst = parse0(stringIndex, wasUtf8);
                    break;
                }
            case CONSTANT_Fieldref:
                {
                    int classIndex = bytes.getUnsignedShort(at + 1);
                    CstType type = (CstType) parse0(classIndex, wasUtf8);
                    int natIndex = bytes.getUnsignedShort(at + 3);
                    CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
                    cst = new CstFieldRef(type, nat);
                    break;
                }
            case CONSTANT_Methodref:
                {
                    int classIndex = bytes.getUnsignedShort(at + 1);
                    CstType type = (CstType) parse0(classIndex, wasUtf8);
                    int natIndex = bytes.getUnsignedShort(at + 3);
                    CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
                    cst = new CstMethodRef(type, nat);
                    break;
                }
            case CONSTANT_InterfaceMethodref:
                {
                    int classIndex = bytes.getUnsignedShort(at + 1);
                    CstType type = (CstType) parse0(classIndex, wasUtf8);
                    int natIndex = bytes.getUnsignedShort(at + 3);
                    CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
                    cst = new CstInterfaceMethodRef(type, nat);
                    break;
                }
            case CONSTANT_NameAndType:
                {
                    int nameIndex = bytes.getUnsignedShort(at + 1);
                    CstString name = (CstString) parse0(nameIndex, wasUtf8);
                    int descriptorIndex = bytes.getUnsignedShort(at + 3);
                    CstString descriptor = (CstString) parse0(descriptorIndex, wasUtf8);
                    cst = new CstNat(name, descriptor);
                    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 parsing cst " + Hex.u2(idx) + " at offset " + Hex.u4(at));
        throw ex;
    } catch (RuntimeException ex) {
        ParseException pe = new ParseException(ex);
        pe.addContext("...while parsing cst " + Hex.u2(idx) + " at offset " + Hex.u4(at));
        throw pe;
    }
    pool.set(idx, cst);
    return cst;
}
Also used : CstNat(com.android.dx.rop.cst.CstNat) Constant(com.android.dx.rop.cst.Constant) CstType(com.android.dx.rop.cst.CstType) CstString(com.android.dx.rop.cst.CstString) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstInterfaceMethodRef(com.android.dx.rop.cst.CstInterfaceMethodRef) ParseException(com.android.dx.cf.iface.ParseException) CstMethodRef(com.android.dx.rop.cst.CstMethodRef)

Example 55 with CstType

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

the class ClassDefsSection method get.

/** {@inheritDoc} */
@Override
public IndexedItem get(Constant cst) {
    if (cst == null) {
        throw new NullPointerException("cst == null");
    }
    throwIfNotPrepared();
    Type type = ((CstType) cst).getClassType();
    IndexedItem result = classDefs.get(type);
    if (result == null) {
        throw new IllegalArgumentException("not found");
    }
    return result;
}
Also used : Type(com.android.dx.rop.type.Type) CstType(com.android.dx.rop.cst.CstType) CstType(com.android.dx.rop.cst.CstType)

Aggregations

CstType (com.android.dx.rop.cst.CstType)76 CstString (com.android.dx.rop.cst.CstString)29 Constant (com.android.dx.rop.cst.Constant)28 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)19 CstMethodRef (com.android.dx.rop.cst.CstMethodRef)18 Type (com.android.dx.rop.type.Type)16 RegisterSpecList (com.android.dx.rop.code.RegisterSpecList)12 CstInsn (com.android.dx.dex.code.CstInsn)10 Annotations (com.android.dx.rop.annotation.Annotations)10 RegisterSpec (com.android.dx.rop.code.RegisterSpec)10 CstNat (com.android.dx.rop.cst.CstNat)10 TypeList (com.android.dx.rop.type.TypeList)9 Annotation (com.android.dx.rop.annotation.Annotation)8 ConstantPool (com.android.dx.rop.cst.ConstantPool)8 ByteArray (com.android.dx.util.ByteArray)7 IntList (com.android.dx.util.IntList)7 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)6 NameValuePair (com.android.dx.rop.annotation.NameValuePair)6 ThrowingCstInsn (com.android.dx.rop.code.ThrowingCstInsn)6 CstInteger (com.android.dx.rop.cst.CstInteger)6