Search in sources :

Example 1 with TypeList

use of com.android.dx.rop.type.TypeList in project RocooFix by dodola.

the class ClassReferenceListBuilder method addClassWithHierachy.

private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }
    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }
        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
    // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
Also used : DirectClassFile(com.android.dx.cf.direct.DirectClassFile) CstType(com.android.dx.rop.cst.CstType) FileNotFoundException(java.io.FileNotFoundException) StdTypeList(com.android.dx.rop.type.StdTypeList) TypeList(com.android.dx.rop.type.TypeList)

Example 2 with TypeList

use of com.android.dx.rop.type.TypeList in project buck by facebook.

the class ClassDefItem method writeTo.

/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    TypeIdsSection typeIds = file.getTypeIds();
    int classIdx = typeIds.indexOf(thisClass);
    int superIdx = (superclass == null) ? -1 : typeIds.indexOf(superclass);
    int interOff = OffsettedItem.getAbsoluteOffsetOr0(interfaces);
    int annoOff = annotationsDirectory.isEmpty() ? 0 : annotationsDirectory.getAbsoluteOffset();
    int sourceFileIdx = (sourceFile == null) ? -1 : file.getStringIds().indexOf(sourceFile);
    int dataOff = classData.isEmpty() ? 0 : classData.getAbsoluteOffset();
    int staticValuesOff = OffsettedItem.getAbsoluteOffsetOr0(staticValuesItem);
    if (annotates) {
        out.annotate(0, indexString() + ' ' + thisClass.toHuman());
        out.annotate(4, "  class_idx:           " + Hex.u4(classIdx));
        out.annotate(4, "  access_flags:        " + AccessFlags.classString(accessFlags));
        out.annotate(4, "  superclass_idx:      " + Hex.u4(superIdx) + " // " + ((superclass == null) ? "<none>" : superclass.toHuman()));
        out.annotate(4, "  interfaces_off:      " + Hex.u4(interOff));
        if (interOff != 0) {
            TypeList list = interfaces.getList();
            int sz = list.size();
            for (int i = 0; i < sz; i++) {
                out.annotate(0, "    " + list.getType(i).toHuman());
            }
        }
        out.annotate(4, "  source_file_idx:     " + Hex.u4(sourceFileIdx) + " // " + ((sourceFile == null) ? "<none>" : sourceFile.toHuman()));
        out.annotate(4, "  annotations_off:     " + Hex.u4(annoOff));
        out.annotate(4, "  class_data_off:      " + Hex.u4(dataOff));
        out.annotate(4, "  static_values_off:   " + Hex.u4(staticValuesOff));
    }
    out.writeInt(classIdx);
    out.writeInt(accessFlags);
    out.writeInt(superIdx);
    out.writeInt(interOff);
    out.writeInt(sourceFileIdx);
    out.writeInt(annoOff);
    out.writeInt(dataOff);
    out.writeInt(staticValuesOff);
}
Also used : StdTypeList(com.android.dx.rop.type.StdTypeList) TypeList(com.android.dx.rop.type.TypeList)

Example 3 with TypeList

use of com.android.dx.rop.type.TypeList in project buck by facebook.

the class AttributeTranslator method getMethodAnnotations.

/**
     * Gets the annotations out of a given method, similar to {@link
     * #getAnnotations}, also including an annotation for the translation
     * of the method-specific attribute {@code Exceptions}.
     *
     * @param method {@code non-null;} the method in question
     * @return {@code non-null;} the set of annotations, which may be empty
     */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);
    if (exceptions.size() != 0) {
        Annotation throwsAnnotation = AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }
    return result;
}
Also used : AttRuntimeVisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttRuntimeVisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) Annotations(com.android.dx.rop.annotation.Annotations) AttRuntimeInvisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) AttRuntimeInvisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) TypeList(com.android.dx.rop.type.TypeList) StdTypeList(com.android.dx.rop.type.StdTypeList) Annotation(com.android.dx.rop.annotation.Annotation)

Example 4 with TypeList

use of com.android.dx.rop.type.TypeList in project buck by facebook.

the class CfTranslator method processMethods.

/**
     * Processes the methods of the given class.
     *
     * @param context
     * @param cf {@code non-null;} class being translated
     * @param cfOptions {@code non-null;} options for class translation
     * @param dexOptions {@code non-null;} options for dex output
     * @param out {@code non-null;} output class
     * @param dexFile {@code non-null;} dex output
     */
private static void processMethods(DxContext context, DirectClassFile cf, CfOptions cfOptions, DexOptions dexOptions, ClassDefItem out, DexFile dexFile) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        try {
            CstMethodRef meth = new CstMethodRef(thisClass, one.getNat());
            int accessFlags = one.getAccessFlags();
            boolean isStatic = AccessFlags.isStatic(accessFlags);
            boolean isPrivate = AccessFlags.isPrivate(accessFlags);
            boolean isNative = AccessFlags.isNative(accessFlags);
            boolean isAbstract = AccessFlags.isAbstract(accessFlags);
            boolean isConstructor = meth.isInstanceInit() || meth.isClassInit();
            DalvCode code;
            if (isNative || isAbstract) {
                // There's no code for native or abstract methods.
                code = null;
            } else {
                ConcreteMethod concrete = new ConcreteMethod(one, cf, (cfOptions.positionInfo != PositionList.NONE), cfOptions.localInfo);
                TranslationAdvice advice;
                advice = DexTranslationAdvice.THE_ONE;
                RopMethod rmeth = Ropper.convert(concrete, advice, methods);
                RopMethod nonOptRmeth = null;
                int paramSize;
                paramSize = meth.getParameterWordCount(isStatic);
                String canonicalName = thisClass.getClassType().getDescriptor() + "." + one.getName().getString();
                if (cfOptions.optimize && context.optimizerOptions.shouldOptimize(canonicalName)) {
                    if (DEBUG) {
                        System.err.println("Optimizing " + canonicalName);
                    }
                    nonOptRmeth = rmeth;
                    rmeth = Optimizer.optimize(rmeth, paramSize, isStatic, cfOptions.localInfo, advice);
                    if (DEBUG) {
                        context.optimizerOptions.compareOptimizerStep(nonOptRmeth, paramSize, isStatic, cfOptions, advice, rmeth);
                    }
                    if (cfOptions.statistics) {
                        context.codeStatistics.updateRopStatistics(nonOptRmeth, rmeth);
                    }
                }
                LocalVariableInfo locals = null;
                if (cfOptions.localInfo) {
                    locals = LocalVariableExtractor.extract(rmeth);
                }
                code = RopTranslator.translate(rmeth, cfOptions.positionInfo, locals, paramSize, dexOptions);
                if (cfOptions.statistics && nonOptRmeth != null) {
                    updateDexStatistics(context, cfOptions, dexOptions, rmeth, nonOptRmeth, locals, paramSize, concrete.getCode().size());
                }
            }
            // Preserve the synchronized flag as its "declared" variant...
            if (AccessFlags.isSynchronized(accessFlags)) {
                accessFlags |= AccessFlags.ACC_DECLARED_SYNCHRONIZED;
                /*
                     * ...but only native methods are actually allowed to be
                     * synchronized.
                     */
                if (!isNative) {
                    accessFlags &= ~AccessFlags.ACC_SYNCHRONIZED;
                }
            }
            if (isConstructor) {
                accessFlags |= AccessFlags.ACC_CONSTRUCTOR;
            }
            TypeList exceptions = AttributeTranslator.getExceptions(one);
            EncodedMethod mi = new EncodedMethod(meth, accessFlags, code, exceptions);
            if (meth.isInstanceInit() || meth.isClassInit() || isStatic || isPrivate) {
                out.addDirectMethod(mi);
            } else {
                out.addVirtualMethod(mi);
            }
            Annotations annotations = AttributeTranslator.getMethodAnnotations(one);
            if (annotations.size() != 0) {
                out.addMethodAnnotations(meth, annotations, dexFile);
            }
            AnnotationsList list = AttributeTranslator.getParameterAnnotations(one);
            if (list.size() != 0) {
                out.addParameterAnnotations(meth, list, dexFile);
            }
            dexFile.getMethodIds().intern(meth);
        } catch (RuntimeException ex) {
            String msg = "...while processing " + one.getName().toHuman() + " " + one.getDescriptor().toHuman();
            throw ExceptionWithContext.withContext(ex, msg);
        }
    }
}
Also used : DalvCode(com.android.dx.dex.code.DalvCode) RopMethod(com.android.dx.rop.code.RopMethod) AnnotationsList(com.android.dx.rop.annotation.AnnotationsList) MethodList(com.android.dx.cf.iface.MethodList) DexTranslationAdvice(com.android.dx.rop.code.DexTranslationAdvice) TranslationAdvice(com.android.dx.rop.code.TranslationAdvice) EncodedMethod(com.android.dx.dex.file.EncodedMethod) ConcreteMethod(com.android.dx.cf.code.ConcreteMethod) RopMethod(com.android.dx.rop.code.RopMethod) Method(com.android.dx.cf.iface.Method) CstString(com.android.dx.rop.cst.CstString) CstMethodRef(com.android.dx.rop.cst.CstMethodRef) LocalVariableInfo(com.android.dx.rop.code.LocalVariableInfo) Annotations(com.android.dx.rop.annotation.Annotations) EncodedMethod(com.android.dx.dex.file.EncodedMethod) CstType(com.android.dx.rop.cst.CstType) ConcreteMethod(com.android.dx.cf.code.ConcreteMethod) TypeList(com.android.dx.rop.type.TypeList)

Example 5 with TypeList

use of com.android.dx.rop.type.TypeList in project buck by facebook.

the class ClassDefsSection method orderItems0.

/**
     * Helper for {@link #orderItems}, which recursively assigns indices
     * to classes.
     *
     * @param type {@code null-ok;} type ref to assign, if any
     * @param idx {@code >= 0;} the next index to assign
     * @param maxDepth maximum recursion depth; if negative, this will
     * throw an exception indicating class definition circularity
     * @return {@code >= 0;} the next index to assign
     */
private int orderItems0(Type type, int idx, int maxDepth) {
    ClassDefItem c = classDefs.get(type);
    if ((c == null) || (c.hasIndex())) {
        return idx;
    }
    if (maxDepth < 0) {
        throw new RuntimeException("class circularity with " + type);
    }
    maxDepth--;
    CstType superclassCst = c.getSuperclass();
    if (superclassCst != null) {
        Type superclass = superclassCst.getClassType();
        idx = orderItems0(superclass, idx, maxDepth);
    }
    TypeList interfaces = c.getInterfaces();
    int sz = interfaces.size();
    for (int i = 0; i < sz; i++) {
        idx = orderItems0(interfaces.getType(i), idx, maxDepth);
    }
    c.setIndex(idx);
    orderedDefs.add(c);
    return idx + 1;
}
Also used : Type(com.android.dx.rop.type.Type) CstType(com.android.dx.rop.cst.CstType) CstType(com.android.dx.rop.cst.CstType) TypeList(com.android.dx.rop.type.TypeList)

Aggregations

TypeList (com.android.dx.rop.type.TypeList)14 StdTypeList (com.android.dx.rop.type.StdTypeList)8 CstType (com.android.dx.rop.cst.CstType)7 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)3 BasicBlock (com.android.dx.rop.code.BasicBlock)3 Type (com.android.dx.rop.type.Type)3 FileNotFoundException (java.io.FileNotFoundException)3 Annotations (com.android.dx.rop.annotation.Annotations)2 BasicBlockList (com.android.dx.rop.code.BasicBlockList)2 IntList (com.android.dx.util.IntList)2 AttExceptions (com.android.dx.cf.attrib.AttExceptions)1 AttRuntimeInvisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)1 AttRuntimeInvisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)1 AttRuntimeVisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)1 AttRuntimeVisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)1 ConcreteMethod (com.android.dx.cf.code.ConcreteMethod)1 Method (com.android.dx.cf.iface.Method)1 MethodList (com.android.dx.cf.iface.MethodList)1 DalvCode (com.android.dx.dex.code.DalvCode)1 EncodedMethod (com.android.dx.dex.file.EncodedMethod)1