Search in sources :

Example 6 with NameValuePair

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

the class ValueEncoder method writeAnnotation.

/**
     * Writes out the encoded form of the given annotation, that is,
     * as an {@code encoded_annotation} and not including a
     * {@code value_type} prefix. If the output stream keeps
     * (debugging) annotations and {@code topLevel} is
     * {@code true}, then this method will write (debugging)
     * annotations.
     *
     * @param annotation {@code non-null;} annotation instance to write
     * @param topLevel {@code true} iff the given annotation is the
     * top-level annotation or {@code false} if it is a sub-annotation
     * of some other annotation
     */
public void writeAnnotation(Annotation annotation, boolean topLevel) {
    boolean annotates = topLevel && out.annotates();
    StringIdsSection stringIds = file.getStringIds();
    TypeIdsSection typeIds = file.getTypeIds();
    CstType type = annotation.getType();
    int typeIdx = typeIds.indexOf(type);
    if (annotates) {
        out.annotate("  type_idx: " + Hex.u4(typeIdx) + " // " + type.toHuman());
    }
    out.writeUleb128(typeIds.indexOf(annotation.getType()));
    Collection<NameValuePair> pairs = annotation.getNameValuePairs();
    int size = pairs.size();
    if (annotates) {
        out.annotate("  size: " + Hex.u4(size));
    }
    out.writeUleb128(size);
    int at = 0;
    for (NameValuePair pair : pairs) {
        CstString name = pair.getName();
        int nameIdx = stringIds.indexOf(name);
        Constant value = pair.getValue();
        if (annotates) {
            out.annotate(0, "  elements[" + at + "]:");
            at++;
            out.annotate("    name_idx: " + Hex.u4(nameIdx) + " // " + name.toHuman());
        }
        out.writeUleb128(nameIdx);
        if (annotates) {
            out.annotate("    value: " + constantToHuman(value));
        }
        writeConstant(value);
    }
    if (annotates) {
        out.endAnnotation();
    }
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) Constant(com.taobao.android.dx.rop.cst.Constant) CstType(com.taobao.android.dx.rop.cst.CstType) CstString(com.taobao.android.dx.rop.cst.CstString)

Example 7 with NameValuePair

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

the class AnnotationParser method parseAnnotation.

/**
     * Parses a single annotation.
     *
     * @param visibility {@code non-null;} visibility of the parsed annotation
     * @return {@code non-null;} the parsed annotation
     */
private Annotation parseAnnotation(AnnotationVisibility visibility) throws IOException {
    requireLength(4);
    int typeIndex = input.readUnsignedShort();
    int numElements = input.readUnsignedShort();
    CstString typeString = (CstString) pool.get(typeIndex);
    CstType type = new CstType(Type.intern(typeString.getString()));
    if (observer != null) {
        parsed(2, "type: " + type.toHuman());
        parsed(2, "num_elements: " + numElements);
    }
    Annotation annotation = new Annotation(type, visibility);
    for (int i = 0; i < numElements; i++) {
        if (observer != null) {
            parsed(0, "elements[" + i + "]:");
            changeIndent(1);
        }
        NameValuePair element = parseElement();
        annotation.add(element);
        if (observer != null) {
            changeIndent(-1);
        }
    }
    annotation.setImmutable();
    return annotation;
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) CstType(com.taobao.android.dx.rop.cst.CstType) CstString(com.taobao.android.dx.rop.cst.CstString) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation)

Example 8 with NameValuePair

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

the class AttributeTranslator method translateAnnotationDefaults.

/**
     * Gets the {@code AnnotationDefault} attributes out of a
     * given class, if any, reforming them as an
     * {@code AnnotationDefault} annotation.
     *
     * @param cf {@code non-null;} the class in question
     * @return {@code null-ok;} an appropriately-constructed
     * {@code AnnotationDefault} annotation, if there were any
     * annotation defaults in the class, or {@code null} if not
     */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result = new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;
    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault) attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);
        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(one.getNat().getName(), oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }
    if (!any) {
        return null;
    }
    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) CstType(com.taobao.android.dx.rop.cst.CstType) MethodList(com.taobao.android.dx.cf.iface.MethodList) Method(com.taobao.android.dx.cf.iface.Method) AttEnclosingMethod(com.taobao.android.dx.cf.attrib.AttEnclosingMethod) Annotation(com.taobao.android.dx.rop.annotation.Annotation) AttAnnotationDefault(com.taobao.android.dx.cf.attrib.AttAnnotationDefault)

Example 9 with NameValuePair

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

the class ValueEncoder method addContents.

/**
     * Helper for {@code addContents()} methods, which adds
     * contents for a particular {@link Annotation}, calling itself
     * recursively should it encounter a nested annotation.
     *
     * @param file {@code non-null;} the file to add to
     * @param annotation {@code non-null;} the annotation to add contents for
     */
public static void addContents(DexFile file, Annotation annotation) {
    TypeIdsSection typeIds = file.getTypeIds();
    StringIdsSection stringIds = file.getStringIds();
    typeIds.intern(annotation.getType());
    for (NameValuePair pair : annotation.getNameValuePairs()) {
        stringIds.intern(pair.getName());
        addContents(file, pair.getValue());
    }
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair)

Example 10 with NameValuePair

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

the class AnnotationItem method annotateTo.

/**
     * Write a (listing file) annotation for this instance to the given
     * output, that consumes no bytes of output. This is for annotating
     * a reference to this instance at the point of the reference.
     *
     * @param out {@code non-null;} where to output to
     * @param prefix {@code non-null;} prefix for each line of output
     */
public void annotateTo(AnnotatedOutput out, String prefix) {
    out.annotate(0, prefix + "visibility: " + annotation.getVisibility().toHuman());
    out.annotate(0, prefix + "type: " + annotation.getType().toHuman());
    for (NameValuePair pair : annotation.getNameValuePairs()) {
        CstString name = pair.getName();
        Constant value = pair.getValue();
        out.annotate(0, prefix + name.toHuman() + ": " + ValueEncoder.constantToHuman(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

NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)13 Annotation (com.taobao.android.dx.rop.annotation.Annotation)9 CstAnnotation (com.taobao.android.dx.rop.cst.CstAnnotation)8 CstString (com.taobao.android.dx.rop.cst.CstString)5 Constant (com.taobao.android.dx.rop.cst.Constant)4 CstArray (com.taobao.android.dx.rop.cst.CstArray)3 CstType (com.taobao.android.dx.rop.cst.CstType)3 AttAnnotationDefault (com.taobao.android.dx.cf.attrib.AttAnnotationDefault)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 AttributeList (com.taobao.android.dx.cf.iface.AttributeList)1 Method (com.taobao.android.dx.cf.iface.Method)1 MethodList (com.taobao.android.dx.cf.iface.MethodList)1 TypeList (com.taobao.android.dx.rop.type.TypeList)1 ArrayList (java.util.ArrayList)1