Search in sources :

Example 11 with NameValuePair

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

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.android.dx.rop.annotation.NameValuePair) Constant(com.android.dx.rop.cst.Constant) CstType(com.android.dx.rop.cst.CstType) CstString(com.android.dx.rop.cst.CstString)

Example 12 with NameValuePair

use of com.android.dx.rop.annotation.NameValuePair in project dexmaker by linkedin.

the class AnnotationId method set.

/**
 * Set an annotation element of this instance.
 * If there is a preexisting element with the same name, it will be
 * replaced by this method.
 *
 * @param element {@code non-null;} the annotation element to be set.
 */
public void set(Element element) {
    if (element == null) {
        throw new NullPointerException("element == null");
    }
    CstString pairName = new CstString(element.getName());
    Constant pairValue = Element.toConstant(element.getValue());
    NameValuePair nameValuePair = new NameValuePair(pairName, pairValue);
    elements.put(element.getName(), nameValuePair);
}
Also used : NameValuePair(com.android.dx.rop.annotation.NameValuePair)

Example 13 with NameValuePair

use of com.android.dx.rop.annotation.NameValuePair in project dexmaker by linkedin.

the class AnnotationId method addToMethod.

/**
 * Add this annotation to a method.
 *
 * @param dexMaker DexMaker instance.
 * @param method Method to be added to.
 */
public void addToMethod(DexMaker dexMaker, MethodId<?, ?> method) {
    if (annotatedElement != ElementType.METHOD) {
        throw new IllegalStateException("This annotation is not for method");
    }
    if (!method.declaringType.equals(declaringType)) {
        throw new IllegalArgumentException("Method" + method + "'s declaring type is inconsistent with" + this);
    }
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(declaringType).toClassDefItem();
    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    } else {
        CstMethodRef cstMethodRef = method.constant;
        if (cstMethodRef == null) {
            throw new NullPointerException("Method reference is NULL");
        } else {
            // Generate CstType
            CstType cstType = CstType.intern(type.ropType);
            // Generate Annotation
            Annotation annotation = new Annotation(cstType, AnnotationVisibility.RUNTIME);
            // Add generated annotation
            Annotations annotations = new Annotations();
            for (NameValuePair nvp : elements.values()) {
                annotation.add(nvp);
            }
            annotations.add(annotation);
            classDefItem.addMethodAnnotations(cstMethodRef, annotations, dexMaker.getDexFile());
        }
    }
}
Also used : NameValuePair(com.android.dx.rop.annotation.NameValuePair) Annotations(com.android.dx.rop.annotation.Annotations) ClassDefItem(com.android.dx.dex.file.ClassDefItem) Annotation(com.android.dx.rop.annotation.Annotation)

Example 14 with NameValuePair

use of com.android.dx.rop.annotation.NameValuePair in project buck by facebook.

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.android.dx.rop.annotation.NameValuePair) Constant(com.android.dx.rop.cst.Constant) CstString(com.android.dx.rop.cst.CstString)

Example 15 with NameValuePair

use of com.android.dx.rop.annotation.NameValuePair in project buck by facebook.

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.android.dx.rop.annotation.NameValuePair)

Aggregations

NameValuePair (com.android.dx.rop.annotation.NameValuePair)29 Annotation (com.android.dx.rop.annotation.Annotation)20 CstAnnotation (com.android.dx.rop.cst.CstAnnotation)17 CstString (com.android.dx.rop.cst.CstString)10 Constant (com.android.dx.rop.cst.Constant)8 CstArray (com.android.dx.rop.cst.CstArray)6 CstType (com.android.dx.rop.cst.CstType)6 AttAnnotationDefault (com.android.dx.cf.attrib.AttAnnotationDefault)2 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)2 AttributeList (com.android.dx.cf.iface.AttributeList)2 Method (com.android.dx.cf.iface.Method)2 MethodList (com.android.dx.cf.iface.MethodList)2 TypeList (com.android.dx.rop.type.TypeList)2 ArrayList (java.util.ArrayList)2 ClassDefItem (com.android.dx.dex.file.ClassDefItem)1 Annotations (com.android.dx.rop.annotation.Annotations)1