Java Code Examples for com.android.dx.rop.annotation.Annotation#add()

The following examples show how to use com.android.dx.rop.annotation.Annotation#add() . 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: 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 2
Source File: AnnotationParser.java    From Box 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 3
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 4
Source File: AnnotationParser.java    From Box 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 5
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add this annotation to a method.
 *
 * @param dexMaker DexMaker instance.
 * @param method   Method to be added to.
 */
public static void addToMethod(DexMaker dexMaker, MethodId<?, ?> method,List<AnnotationId<?,?>> ids) {
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(method.declaringType).toClassDefItem();

    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    }
    CstMethodRef cstMethodRef = method.constant;
    if (cstMethodRef == null) {
        throw new NullPointerException("Method reference is NULL");
    }
    Annotations annotations = new Annotations();
    for (AnnotationId<?, ?> id :
            ids) {
        if (id.annotatedElement != ElementType.METHOD) {
            throw new IllegalStateException("This annotation is not for method");
        }

        if (method.declaringType != id.declaringType) {
            throw new IllegalArgumentException("Method" + method + "'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);
    }
    classDefItem.addMethodAnnotations(cstMethodRef, annotations, dexMaker.getDexFile());


}
 
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: 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 9
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 10
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 11
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 12
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void addToParameters(DexMaker dexMaker, MethodId<?, ?> methodId,
                                   List<List<AnnotationId<?,?>>> annotationIds) {
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(methodId.declaringType).toClassDefItem();

    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    }
    CstMethodRef cstMethodRef = methodId.constant;
    if (cstMethodRef == null) {
        throw new NullPointerException("Method reference is NULL");
    }
    AnnotationsList list=new AnnotationsList(annotationIds.size());
    for (int i=list.size()-1;i>=0;--i) {
        List<AnnotationId<?, ?>> ids =
        annotationIds.get(i);
        if(ids==null) continue;
        Annotations annotations = new Annotations();
        for (AnnotationId<?,?> id:ids) {

            if (id.annotatedElement != ElementType.PARAMETER) {
                throw new IllegalStateException("This annotation is not for method");
            }

            if (id.declaringType != methodId.declaringType) {
                throw new IllegalArgumentException("Method" + methodId + "'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);
        }
        list.set(i,annotations);
    }


    classDefItem.addParameterAnnotations(cstMethodRef,list , dexMaker.getDexFile());
}