Search in sources :

Example 1 with CstString

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

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

the class Main method dumpMethod.

/**
 * Dumps any method with the given name in the given file.
 *
 * @param dex {@code non-null;} the dex file
 * @param fqName {@code non-null;} the fully-qualified name of the
 * method(s)
 * @param out {@code non-null;} where to dump to
 */
private void dumpMethod(DexFile dex, String fqName, OutputStreamWriter out) {
    boolean wildcard = fqName.endsWith("*");
    int lastDot = fqName.lastIndexOf('.');
    if ((lastDot <= 0) || (lastDot == (fqName.length() - 1))) {
        DxConsole.err.println("bogus fully-qualified method name: " + fqName);
        return;
    }
    String className = fqName.substring(0, lastDot).replace('.', '/');
    String methodName = fqName.substring(lastDot + 1);
    ClassDefItem clazz = dex.getClassOrNull(className);
    if (clazz == null) {
        DxConsole.err.println("no such class: " + className);
        return;
    }
    if (wildcard) {
        methodName = methodName.substring(0, methodName.length() - 1);
    }
    ArrayList<EncodedMethod> allMeths = clazz.getMethods();
    TreeMap<CstNat, EncodedMethod> meths = new TreeMap<CstNat, EncodedMethod>();
    /*
         * Figure out which methods to include in the output, and get them
         * all sorted, so that the printout code is robust with respect to
         * changes in the underlying order.
         */
    for (EncodedMethod meth : allMeths) {
        String methName = meth.getName().getString();
        if ((wildcard && methName.startsWith(methodName)) || (!wildcard && methName.equals(methodName))) {
            meths.put(meth.getRef().getNat(), meth);
        }
    }
    if (meths.size() == 0) {
        DxConsole.err.println("no such method: " + fqName);
        return;
    }
    PrintWriter pw = new PrintWriter(out);
    for (EncodedMethod meth : meths.values()) {
        // TODO: Better stuff goes here, perhaps.
        meth.debugPrint(pw, args.verboseDump);
        /*
             * The (default) source file is an attribute of the class, but
             * it's useful to see it in method dumps.
             */
        CstString sourceFile = clazz.getSourceFile();
        if (sourceFile != null) {
            pw.println("  source file: " + sourceFile.toQuoted());
        }
        Annotations methodAnnotations = clazz.getMethodAnnotations(meth.getRef());
        AnnotationsList parameterAnnotations = clazz.getParameterAnnotations(meth.getRef());
        if (methodAnnotations != null) {
            pw.println("  method annotations:");
            for (Annotation a : methodAnnotations.getAnnotations()) {
                pw.println("    " + a);
            }
        }
        if (parameterAnnotations != null) {
            pw.println("  parameter annotations:");
            int sz = parameterAnnotations.size();
            for (int i = 0; i < sz; i++) {
                pw.println("    parameter " + i);
                Annotations annotations = parameterAnnotations.get(i);
                for (Annotation a : annotations.getAnnotations()) {
                    pw.println("      " + a);
                }
            }
        }
    }
    pw.flush();
}
Also used : CstNat(com.taobao.android.dx.rop.cst.CstNat) CstString(com.taobao.android.dx.rop.cst.CstString) AnnotationsList(com.taobao.android.dx.rop.annotation.AnnotationsList) CstString(com.taobao.android.dx.rop.cst.CstString) Annotation(com.taobao.android.dx.rop.annotation.Annotation) Annotations(com.taobao.android.dx.rop.annotation.Annotations) ClassDefItem(com.taobao.android.dx.dex.file.ClassDefItem) EncodedMethod(com.taobao.android.dx.dex.file.EncodedMethod)

Example 3 with CstString

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

the class StdAttributeFactory method parseLocalVariables.

/**
 * Parse the table part of either a {@code LocalVariableTable}
 * or a {@code LocalVariableTypeTable}.
 *
 * @param bytes {@code non-null;} bytes to parse, which should <i>only</i>
 * contain the table data (no header)
 * @param pool {@code non-null;} constant pool to use
 * @param count {@code >= 0;} the number of entries
 * @param typeTable {@code true} iff this is for a type table
 * @return {@code non-null;} the constructed list
 */
private LocalVariableList parseLocalVariables(ByteArray bytes, ConstantPool pool, ParseObserver observer, int count, boolean typeTable) {
    if (bytes.size() != (count * 10)) {
        // "+ 2" is for the count.
        throwBadLength((count * 10) + 2);
    }
    ByteArray.MyDataInputStream in = bytes.makeDataInputStream();
    LocalVariableList list = new LocalVariableList(count);
    try {
        for (int i = 0; i < count; i++) {
            int startPc = in.readUnsignedShort();
            int length = in.readUnsignedShort();
            int nameIdx = in.readUnsignedShort();
            int typeIdx = in.readUnsignedShort();
            int index = in.readUnsignedShort();
            CstString name = (CstString) pool.get(nameIdx);
            CstString type = (CstString) pool.get(typeIdx);
            CstString descriptor = null;
            CstString signature = null;
            if (typeTable) {
                signature = type;
            } else {
                descriptor = type;
            }
            list.set(i, startPc, length, name, descriptor, signature, index);
            if (observer != null) {
                observer.parsed(bytes, i * 10, 10, Hex.u2(startPc) + ".." + Hex.u2(startPc + length) + " " + Hex.u2(index) + " " + name.toHuman() + " " + type.toHuman());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("shouldn't happen", ex);
    }
    list.setImmutable();
    return list;
}
Also used : LocalVariableList(com.taobao.android.dx.cf.code.LocalVariableList) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) IOException(java.io.IOException)

Example 4 with CstString

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

the class ConstantPoolParser method parseUtf8.

/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);
    // Skip to the data.
    at += 3;
    ByteArray ubytes = bytes.slice(at, at + length);
    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
Also used : CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Example 5 with CstString

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

CstString (com.taobao.android.dx.rop.cst.CstString)36 CstType (com.taobao.android.dx.rop.cst.CstType)16 Constant (com.taobao.android.dx.rop.cst.Constant)14 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)7 ByteArray (com.taobao.android.dx.util.ByteArray)7 CstFieldRef (com.taobao.android.dx.rop.cst.CstFieldRef)6 ParseException (com.taobao.android.dx.cf.iface.ParseException)5 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)5 RegisterSpec (com.taobao.android.dx.rop.code.RegisterSpec)5 CstNat (com.taobao.android.dx.rop.cst.CstNat)5 ClassDefItem (com.taobao.android.dx.dex.file.ClassDefItem)4 Annotation (com.taobao.android.dx.rop.annotation.Annotation)4 CstMethodRef (com.taobao.android.dx.rop.cst.CstMethodRef)4 Type (com.taobao.android.dx.rop.type.Type)4 Annotations (com.taobao.android.dx.rop.annotation.Annotations)3 CstAnnotation (com.taobao.android.dx.rop.cst.CstAnnotation)3 CstEnumRef (com.taobao.android.dx.rop.cst.CstEnumRef)3 CstInterfaceMethodRef (com.taobao.android.dx.rop.cst.CstInterfaceMethodRef)3 TypedConstant (com.taobao.android.dx.rop.cst.TypedConstant)3 Attribute (com.taobao.android.dx.cf.iface.Attribute)2