Search in sources :

Example 21 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

the class AttributeTranslator method getClassAnnotations.

/**
 * Gets the annotations out of a given class, similar to {@link
 * #getAnnotations}, also including annotations for translations
 * of class-level attributes {@code EnclosingMethod} and
 * {@code InnerClasses}, if present. Additionally, if the
 * class is an annotation class, then this also includes a
 * representation of all the {@code AnnotationDefault}
 * values.
 *
 * @param cf {@code non-null;} the class in question
 * @param args {@code non-null;} the high-level options
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getClassAnnotations(DirectClassFile cf, CfOptions args) {
    CstType thisClass = cf.getThisClass();
    AttributeList attribs = cf.getAttributes();
    Annotations result = getAnnotations(attribs);
    Annotation enclosingMethod = translateEnclosingMethod(attribs);
    try {
        Annotations innerClassAnnotations = translateInnerClasses(thisClass, attribs, enclosingMethod == null);
        if (innerClassAnnotations != null) {
            result = Annotations.combine(result, innerClassAnnotations);
        }
    } catch (Warning warn) {
        args.warn.println("warning: " + warn.getMessage());
    }
    if (enclosingMethod != null) {
        result = Annotations.combine(result, enclosingMethod);
    }
    if (AccessFlags.isAnnotation(cf.getAccessFlags())) {
        Annotation annotationDefault = translateAnnotationDefaults(cf);
        if (annotationDefault != null) {
            result = Annotations.combine(result, annotationDefault);
        }
    }
    return result;
}
Also used : Warning(com.android.dx.util.Warning) 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) AttributeList(com.android.dx.cf.iface.AttributeList) CstType(com.android.dx.rop.cst.CstType) Annotation(com.android.dx.rop.annotation.Annotation)

Example 22 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

the class CfTranslator method processFields.

/**
 * Processes the fields of the given class.
 *
 * @param cf {@code non-null;} class being translated
 * @param out {@code non-null;} output class
 * @param dexFile {@code non-null;} dex output
 */
private static void processFields(DirectClassFile cf, ClassDefItem out, DexFile dexFile) {
    CstType thisClass = cf.getThisClass();
    FieldList fields = cf.getFields();
    int sz = fields.size();
    for (int i = 0; i < sz; i++) {
        Field one = fields.get(i);
        try {
            CstFieldRef field = new CstFieldRef(thisClass, one.getNat());
            int accessFlags = one.getAccessFlags();
            if (AccessFlags.isStatic(accessFlags)) {
                TypedConstant constVal = one.getConstantValue();
                EncodedField fi = new EncodedField(field, accessFlags);
                if (constVal != null) {
                    constVal = coerceConstant(constVal, field.getType());
                }
                out.addStaticField(fi, constVal);
            } else {
                EncodedField fi = new EncodedField(field, accessFlags);
                out.addInstanceField(fi);
            }
            Annotations annotations = AttributeTranslator.getAnnotations(one.getAttributes());
            if (annotations.size() != 0) {
                out.addFieldAnnotations(field, annotations, dexFile);
            }
            dexFile.getFieldIds().intern(field);
        } catch (RuntimeException ex) {
            String msg = "...while processing " + one.getName().toHuman() + " " + one.getDescriptor().toHuman();
            throw ExceptionWithContext.withContext(ex, msg);
        }
    }
}
Also used : Field(com.android.dx.cf.iface.Field) EncodedField(com.android.dx.dex.file.EncodedField) Annotations(com.android.dx.rop.annotation.Annotations) TypedConstant(com.android.dx.rop.cst.TypedConstant) CstType(com.android.dx.rop.cst.CstType) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) EncodedField(com.android.dx.dex.file.EncodedField) CstString(com.android.dx.rop.cst.CstString) FieldList(com.android.dx.cf.iface.FieldList)

Example 23 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

the class CfTranslator method processMethods.

/**
 * Processes the methods of the given class.
 *
 * @param context {@code non-null;} the state global to this invocation.
 * @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, dexOptions);
                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 24 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

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 {@code non-null;} the state global to this invocation.
 * @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)

Aggregations

Annotations (com.android.dx.rop.annotation.Annotations)24 AttRuntimeInvisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)12 AttRuntimeInvisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)12 AttRuntimeVisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)12 AttRuntimeVisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)12 Annotation (com.android.dx.rop.annotation.Annotation)10 CstType (com.android.dx.rop.cst.CstType)10 CstString (com.android.dx.rop.cst.CstString)8 AnnotationsList (com.android.dx.rop.annotation.AnnotationsList)6 ClassDefItem (com.android.dx.dex.file.ClassDefItem)4 EncodedMethod (com.android.dx.dex.file.EncodedMethod)4 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)4 CstMethodRef (com.android.dx.rop.cst.CstMethodRef)4 TypedConstant (com.android.dx.rop.cst.TypedConstant)4 StdTypeList (com.android.dx.rop.type.StdTypeList)4 TypeList (com.android.dx.rop.type.TypeList)4 Warning (com.android.dx.util.Warning)4 AttInnerClasses (com.android.dx.cf.attrib.AttInnerClasses)2 InnerClassList (com.android.dx.cf.attrib.InnerClassList)2 ConcreteMethod (com.android.dx.cf.code.ConcreteMethod)2