Search in sources :

Example 6 with Annotations

use of com.taobao.android.dx.rop.annotation.Annotations 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 7 with Annotations

use of com.taobao.android.dx.rop.annotation.Annotations in project atlas by alibaba.

the class StdAttributeFactory method runtimeVisibleAnnotations.

/**
     * Parses a {@code RuntimeVisibleAnnotations} attribute.
     */
private Attribute runtimeVisibleAnnotations(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        throwSeverelyTruncated();
    }
    AnnotationParser ap = new AnnotationParser(cf, offset, length, observer);
    Annotations annotations = ap.parseAnnotationAttribute(AnnotationVisibility.RUNTIME);
    return new AttRuntimeVisibleAnnotations(annotations, length);
}
Also used : AttRuntimeVisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttRuntimeVisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) AttRuntimeInvisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) AttRuntimeInvisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) Annotations(com.taobao.android.dx.rop.annotation.Annotations) AttRuntimeVisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)

Example 8 with Annotations

use of com.taobao.android.dx.rop.annotation.Annotations in project atlas by alibaba.

the class StdAttributeFactory method runtimeInvisibleAnnotations.

/**
     * Parses a {@code RuntimeInvisibleAnnotations} attribute.
     */
private Attribute runtimeInvisibleAnnotations(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        throwSeverelyTruncated();
    }
    AnnotationParser ap = new AnnotationParser(cf, offset, length, observer);
    Annotations annotations = ap.parseAnnotationAttribute(AnnotationVisibility.BUILD);
    return new AttRuntimeInvisibleAnnotations(annotations, length);
}
Also used : AttRuntimeVisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) AttRuntimeInvisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) AttRuntimeInvisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) Annotations(com.taobao.android.dx.rop.annotation.Annotations) AttRuntimeVisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttRuntimeInvisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)

Example 9 with Annotations

use of com.taobao.android.dx.rop.annotation.Annotations in project atlas by alibaba.

the class AnnotationParser method parseAnnotations.

/**
     * Parses an annotation list.
     *
     * @param visibility {@code non-null;} visibility of the parsed annotations
     * @return {@code non-null;} the list of annotations read from the attribute
     * data
     */
private Annotations parseAnnotations(AnnotationVisibility visibility) throws IOException {
    int count = input.readUnsignedShort();
    if (observer != null) {
        parsed(2, "num_annotations: " + Hex.u2(count));
    }
    Annotations annotations = new Annotations();
    for (int i = 0; i < count; i++) {
        if (observer != null) {
            parsed(0, "annotations[" + i + "]:");
            changeIndent(1);
        }
        Annotation annotation = parseAnnotation(visibility);
        annotations.add(annotation);
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    annotations.setImmutable();
    return annotations;
}
Also used : Annotations(com.taobao.android.dx.rop.annotation.Annotations) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation)

Example 10 with Annotations

use of com.taobao.android.dx.rop.annotation.Annotations in project atlas by alibaba.

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.taobao.android.dx.util.Warning) AttRuntimeVisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) AttRuntimeInvisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) AttRuntimeInvisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) Annotations(com.taobao.android.dx.rop.annotation.Annotations) AttRuntimeVisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) CstType(com.taobao.android.dx.rop.cst.CstType) Annotation(com.taobao.android.dx.rop.annotation.Annotation)

Aggregations

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