Search in sources :

Example 1 with Constant

use of com.taobao.android.dx.rop.cst.Constant 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 2 with Constant

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

the class ClassReferenceListBuilder method addDependencies.

private void addDependencies(ConstantPool pool) {
    for (Constant constant : pool.getEntries()) {
        if (constant instanceof CstType) {
            Type type = ((CstType) constant).getClassType();
            String descriptor = type.getDescriptor();
            if (descriptor.endsWith(";")) {
                int lastBrace = descriptor.lastIndexOf('[');
                if (lastBrace < 0) {
                    addClassWithHierachy(descriptor.substring(1, descriptor.length() - 1));
                } else {
                    assert descriptor.length() > lastBrace + 3 && descriptor.charAt(lastBrace + 1) == 'L';
                    addClassWithHierachy(descriptor.substring(lastBrace + 2, descriptor.length() - 1));
                }
            }
        }
    }
}
Also used : CstType(com.taobao.android.dx.rop.cst.CstType) Type(com.taobao.android.dx.rop.type.Type) Constant(com.taobao.android.dx.rop.cst.Constant) CstType(com.taobao.android.dx.rop.cst.CstType)

Example 3 with Constant

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

the class CfTranslator method updateDexStatistics.

/**
 * Helper that updates the dex statistics.
 */
private static void updateDexStatistics(CfOptions cfOptions, DexOptions dexOptions, RopMethod optRmeth, RopMethod nonOptRmeth, LocalVariableInfo locals, int paramSize, int originalByteCount) {
    /*
         * Run rop->dex again on optimized vs. non-optimized method to
         * collect statistics. We have to totally convert both ways,
         * since converting the "real" method getting added to the
         * file would corrupt it (by messing with its constant pool
         * indices).
         */
    DalvCode optCode = RopTranslator.translate(optRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions);
    DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions);
    /*
         * Fake out the indices, so code.getInsns() can work well enough
         * for the current purpose.
         */
    DalvCode.AssignIndicesCallback callback = new DalvCode.AssignIndicesCallback() {

        public int getIndex(Constant cst) {
            // Everything is at index 0!
            return 0;
        }
    };
    optCode.assignIndices(callback);
    nonOptCode.assignIndices(callback);
    CodeStatistics.updateDexStatistics(nonOptCode, optCode);
    CodeStatistics.updateOriginalByteCount(originalByteCount);
}
Also used : DalvCode(com.taobao.android.dx.dex.code.DalvCode) Constant(com.taobao.android.dx.rop.cst.Constant) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant)

Example 4 with Constant

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

the class ConstantPoolParser method parse.

/**
 * Does the actual parsing.
 */
private void parse() {
    determineOffsets();
    if (observer != null) {
        observer.parsed(bytes, 8, 2, "constant_pool_count: " + Hex.u2(offsets.length));
        observer.parsed(bytes, 10, 0, "\nconstant_pool:");
        observer.changeIndent(1);
    }
    /*
         * Track the constant value's original string type. True if constants[i] was
         * a CONSTANT_Utf8, false for any other type including CONSTANT_string.
         */
    BitSet wasUtf8 = new BitSet(offsets.length);
    for (int i = 1; i < offsets.length; i++) {
        int offset = offsets[i];
        if ((offset != 0) && (pool.getOrNull(i) == null)) {
            parse0(i, wasUtf8);
        }
    }
    if (observer != null) {
        for (int i = 1; i < offsets.length; i++) {
            Constant cst = pool.getOrNull(i);
            if (cst == null) {
                continue;
            }
            int offset = offsets[i];
            int nextOffset = endOffset;
            for (int j = i + 1; j < offsets.length; j++) {
                int off = offsets[j];
                if (off != 0) {
                    nextOffset = off;
                    break;
                }
            }
            String human = wasUtf8.get(i) ? Hex.u2(i) + ": utf8{\"" + cst.toHuman() + "\"}" : Hex.u2(i) + ": " + cst.toString();
            observer.parsed(bytes, offset, nextOffset - offset, human);
        }
        observer.changeIndent(-1);
        observer.parsed(bytes, endOffset, 0, "end constant_pool");
    }
}
Also used : Constant(com.taobao.android.dx.rop.cst.Constant) BitSet(java.util.BitSet) CONSTANT_String(com.taobao.android.dx.cf.cst.ConstantTags.CONSTANT_String) CstString(com.taobao.android.dx.rop.cst.CstString)

Example 5 with Constant

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

the class AnnotationParser method parseElement.

/**
 * Parses a {@link NameValuePair}.
 *
 * @return {@code non-null;} the parsed element
 */
private NameValuePair parseElement() throws IOException {
    requireLength(5);
    int elementNameIndex = input.readUnsignedShort();
    CstString elementName = (CstString) pool.get(elementNameIndex);
    if (observer != null) {
        parsed(2, "element_name: " + elementName.toHuman());
        parsed(0, "value: ");
        changeIndent(1);
    }
    Constant value = parseValue();
    if (observer != null) {
        changeIndent(-1);
    }
    return new NameValuePair(elementName, value);
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) Constant(com.taobao.android.dx.rop.cst.Constant) CstString(com.taobao.android.dx.rop.cst.CstString)

Aggregations

Constant (com.taobao.android.dx.rop.cst.Constant)46 RegisterSpecList (com.taobao.android.dx.rop.code.RegisterSpecList)17 CstType (com.taobao.android.dx.rop.cst.CstType)16 CstString (com.taobao.android.dx.rop.cst.CstString)15 CstInsn (com.taobao.android.dx.dex.code.CstInsn)12 TypedConstant (com.taobao.android.dx.rop.cst.TypedConstant)11 RegisterSpec (com.taobao.android.dx.rop.code.RegisterSpec)10 CstFieldRef (com.taobao.android.dx.rop.cst.CstFieldRef)9 CstLiteralBits (com.taobao.android.dx.rop.cst.CstLiteralBits)9 CstInteger (com.taobao.android.dx.rop.cst.CstInteger)6 CstMethodRef (com.taobao.android.dx.rop.cst.CstMethodRef)6 Type (com.taobao.android.dx.rop.type.Type)5 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)4 Insn (com.taobao.android.dx.rop.code.Insn)4 PlainInsn (com.taobao.android.dx.rop.code.PlainInsn)4 TypeBearer (com.taobao.android.dx.rop.type.TypeBearer)4 Rop (com.taobao.android.dx.rop.code.Rop)3 ThrowingCstInsn (com.taobao.android.dx.rop.code.ThrowingCstInsn)3 CstInterfaceMethodRef (com.taobao.android.dx.rop.cst.CstInterfaceMethodRef)3 ArrayList (java.util.ArrayList)3