com.android.dx.rop.annotation.NameValuePair Java Examples

The following examples show how to use com.android.dx.rop.annotation.NameValuePair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotationParser.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void addToField(DexMaker dexMaker, FieldId<?, ?> field,List<AnnotationId<?,?>> ids) {
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(field.declaringType).toClassDefItem();
    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    }
    Annotations annotations = new Annotations();
    for (AnnotationId<?,?> id:ids){
        if (id.annotatedElement != ElementType.FIELD) {
            throw new IllegalStateException("This annotation is not for method");
        }

        if (field.declaringType != id.declaringType) {
            throw new IllegalArgumentException("Field" + field + "'s declaring type is inconsistent with" + id);
        }

        // Generate CstType
        CstType cstType = CstType.intern(id.type.ropType);

        // Generate Annotation
        Annotation annotation = new Annotation(cstType, AnnotationVisibility.RUNTIME);

        // Add generated annotation
        for (NameValuePair nvp : id.elements.values()) {
            annotation.add(nvp);
        }
        annotations.add(annotation);
    }

    CstFieldRef cstFieldRef = field.constant;
    classDefItem.addFieldAnnotations(cstFieldRef, annotations, dexMaker.getDexFile());
}
 
Example #7
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code AnnotationDefault} annotation.
 *
 * @param defaults {@code non-null;} the defaults, itself as an annotation
 * @return {@code non-null;} the constructed annotation
 */
public static Annotation makeAnnotationDefault(Annotation defaults) {
    Annotation result = new Annotation(ANNOTATION_DEFAULT_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, new CstAnnotation(defaults)));
    result.setImmutable();
    return result;
}
 
Example #9
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingClass} annotation.
 *
 * @param clazz {@code non-null;} the enclosing class
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingClass(CstType clazz) {
    Annotation result = new Annotation(ENCLOSING_CLASS_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, clazz));
    result.setImmutable();
    return result;
}
 
Example #10
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingMethod} annotation.
 *
 * @param method {@code non-null;} the enclosing method
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingMethod(CstMethodRef method) {
    Annotation result = new Annotation(ENCLOSING_METHOD_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, method));
    result.setImmutable();
    return result;
}
 
Example #11
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #12
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code MemberClasses} annotation.
 *
 * @param types {@code non-null;} the list of (the types of) the member classes
 * @return {@code non-null;} the annotation
 */
public static Annotation makeMemberClasses(TypeList types) {
    CstArray array = makeCstArray(types);
    Annotation result = new Annotation(MEMBER_CLASSES_TYPE, SYSTEM);
    result.put(new NameValuePair(VALUE_STRING, array));
    result.setImmutable();
    return result;
}
 
Example #13
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code SourceDebugExtension} annotation.
 *
 * @param smapString {@code non-null;} the SMAP string associated with
 * @return {@code non-null;} the annotation
 */
public static Annotation makeSourceDebugExtension(CstString smapString) {
    Annotation result = new Annotation(SOURCE_DEBUG_EXTENSION_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, smapString));
    result.setImmutable();
    return result;
}
 
Example #14
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code Throws} annotation.
 *
 * @param types {@code non-null;} the list of thrown types
 * @return {@code non-null;} the annotation
 */
public static Annotation makeThrows(TypeList types) {
    CstArray array = makeCstArray(types);
    Annotation result = new Annotation(THROWS_TYPE, SYSTEM);
    result.put(new NameValuePair(VALUE_STRING, array));
    result.setImmutable();
    return result;
}
 
Example #15
Source File: ValueEncoder.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example #16
Source File: AnnotationItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
    }
}
 
Example #17
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: AnnotationId.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #19
Source File: AnnotationId.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
        }
    }
}
 
Example #20
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #21
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code AnnotationDefault} annotation.
 *
 * @param defaults {@code non-null;} the defaults, itself as an annotation
 * @return {@code non-null;} the constructed annotation
 */
public static Annotation makeAnnotationDefault(Annotation defaults) {
    Annotation result = new Annotation(ANNOTATION_DEFAULT_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, new CstAnnotation(defaults)));
    result.setImmutable();
    return result;
}
 
Example #22
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingClass} annotation.
 *
 * @param clazz {@code non-null;} the enclosing class
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingClass(CstType clazz) {
    Annotation result = new Annotation(ENCLOSING_CLASS_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, clazz));
    result.setImmutable();
    return result;
}
 
Example #23
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingMethod} annotation.
 *
 * @param method {@code non-null;} the enclosing method
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingMethod(CstMethodRef method) {
    Annotation result = new Annotation(ENCLOSING_METHOD_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, method));
    result.setImmutable();
    return result;
}
 
Example #24
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #25
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code MemberClasses} annotation.
 *
 * @param types {@code non-null;} the list of (the types of) the member classes
 * @return {@code non-null;} the annotation
 */
public static Annotation makeMemberClasses(TypeList types) {
    CstArray array = makeCstArray(types);
    Annotation result = new Annotation(MEMBER_CLASSES_TYPE, SYSTEM);
    result.put(new NameValuePair(VALUE_STRING, array));
    result.setImmutable();
    return result;
}
 
Example #26
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code Throws} annotation.
 *
 * @param types {@code non-null;} the list of thrown types
 * @return {@code non-null;} the annotation
 */
public static Annotation makeThrows(TypeList types) {
    CstArray array = makeCstArray(types);
    Annotation result = new Annotation(THROWS_TYPE, SYSTEM);
    result.put(new NameValuePair(VALUE_STRING, array));
    result.setImmutable();
    return result;
}
 
Example #27
Source File: ValueEncoder.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example #28
Source File: AnnotationItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
    }
}
 
Example #29
Source File: AnnotationParser.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #30
Source File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code AnnotationDefault} annotation.
 *
 * @param defaults {@code non-null;} the defaults, itself as an annotation
 * @return {@code non-null;} the constructed annotation
 */
public static Annotation makeAnnotationDefault(Annotation defaults) {
    Annotation result = new Annotation(ANNOTATION_DEFAULT_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, new CstAnnotation(defaults)));
    result.setImmutable();
    return result;
}