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

The following examples show how to use com.android.dx.rop.annotation.Annotations#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: AnnotationParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: AnnotationParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 3
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 4
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 5
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
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 7
Source File: AnnotationParser.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: AttributeTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the {@code InnerClasses} attribute out of a given
 * {@link AttributeList}, if any, translating it to one or more of an
 * {@code InnerClass}, {@code EnclosingClass}, or
 * {@code MemberClasses} annotation.
 *
 * @param thisClass {@code non-null;} type representing the class being
 * processed
 * @param attribs {@code non-null;} the attributes list to search in
 * @param needEnclosingClass whether to include an
 * {@code EnclosingClass} annotation
 * @return {@code null-ok;} the converted list of annotations, if there
 * was an attribute to translate
 */
private static Annotations translateInnerClasses(CstType thisClass,
        AttributeList attribs, boolean needEnclosingClass) {
    AttInnerClasses innerClasses = (AttInnerClasses)
        attribs.findFirst(AttInnerClasses.ATTRIBUTE_NAME);

    if (innerClasses == null) {
        return null;
    }

    /*
     * Search the list for the element representing the current class
     * as well as for any named member classes.
     */

    InnerClassList list = innerClasses.getInnerClasses();
    int size = list.size();
    InnerClassList.Item foundThisClass = null;
    ArrayList<Type> membersList = new ArrayList<Type>();

    for (int i = 0; i < size; i++) {
        InnerClassList.Item item = list.get(i);
        CstType innerClass = item.getInnerClass();
        if (innerClass.equals(thisClass)) {
            foundThisClass = item;
        } else if (thisClass.equals(item.getOuterClass())) {
            membersList.add(innerClass.getClassType());
        }
    }

    int membersSize = membersList.size();

    if ((foundThisClass == null) && (membersSize == 0)) {
        return null;
    }

    Annotations result = new Annotations();

    if (foundThisClass != null) {
        result.add(AnnotationUtils.makeInnerClass(
                           foundThisClass.getInnerName(),
                           foundThisClass.getAccessFlags()));
        if (needEnclosingClass) {
            CstType outer = foundThisClass.getOuterClass();
            if (outer == null) {
                throw new Warning(
                        "Ignoring InnerClasses attribute for an " +
                        "anonymous inner class\n" +
                        "(" + thisClass.toHuman() +
                        ") that doesn't come with an\n" +
                        "associated EnclosingMethod attribute. " +
                        "This class was probably produced by a\n" +
                        "compiler that did not target the modern " +
                        ".class file format. The recommended\n" +
                        "solution is to recompile the class from " +
                        "source, using an up-to-date compiler\n" +
                        "and without specifying any \"-target\" type " +
                        "options. The consequence of ignoring\n" +
                        "this warning is that reflective operations " +
                        "on this class will incorrectly\n" +
                        "indicate that it is *not* an inner class.");
            }
            result.add(AnnotationUtils.makeEnclosingClass(
                               foundThisClass.getOuterClass()));
        }
    }

    if (membersSize != 0) {
        StdTypeList typeList = new StdTypeList(membersSize);
        for (int i = 0; i < membersSize; i++) {
            typeList.set(i, membersList.get(i));
        }
        typeList.setImmutable();
        result.add(AnnotationUtils.makeMemberClasses(typeList));
    }

    result.setImmutable();
    return result;
}
 
Example 9
Source File: AttributeTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the {@code InnerClasses} attribute out of a given
 * {@link AttributeList}, if any, translating it to one or more of an
 * {@code InnerClass}, {@code EnclosingClass}, or
 * {@code MemberClasses} annotation.
 *
 * @param thisClass {@code non-null;} type representing the class being
 * processed
 * @param attribs {@code non-null;} the attributes list to search in
 * @param needEnclosingClass whether to include an
 * {@code EnclosingClass} annotation
 * @return {@code null-ok;} the converted list of annotations, if there
 * was an attribute to translate
 */
private static Annotations translateInnerClasses(CstType thisClass,
        AttributeList attribs, boolean needEnclosingClass) {
    AttInnerClasses innerClasses = (AttInnerClasses)
        attribs.findFirst(AttInnerClasses.ATTRIBUTE_NAME);

    if (innerClasses == null) {
        return null;
    }

    /*
     * Search the list for the element representing the current class
     * as well as for any named member classes.
     */

    InnerClassList list = innerClasses.getInnerClasses();
    int size = list.size();
    InnerClassList.Item foundThisClass = null;
    ArrayList<Type> membersList = new ArrayList<Type>();

    for (int i = 0; i < size; i++) {
        InnerClassList.Item item = list.get(i);
        CstType innerClass = item.getInnerClass();
        if (innerClass.equals(thisClass)) {
            foundThisClass = item;
        } else if (thisClass.equals(item.getOuterClass())) {
            membersList.add(innerClass.getClassType());
        }
    }

    int membersSize = membersList.size();

    if ((foundThisClass == null) && (membersSize == 0)) {
        return null;
    }

    Annotations result = new Annotations();

    if (foundThisClass != null) {
        result.add(AnnotationUtils.makeInnerClass(
                           foundThisClass.getInnerName(),
                           foundThisClass.getAccessFlags()));
        if (needEnclosingClass) {
            CstType outer = foundThisClass.getOuterClass();
            if (outer == null) {
                throw new Warning(
                        "Ignoring InnerClasses attribute for an " +
                        "anonymous inner class\n" +
                        "(" + thisClass.toHuman() +
                        ") that doesn't come with an\n" +
                        "associated EnclosingMethod attribute. " +
                        "This class was probably produced by a\n" +
                        "compiler that did not target the modern " +
                        ".class file format. The recommended\n" +
                        "solution is to recompile the class from " +
                        "source, using an up-to-date compiler\n" +
                        "and without specifying any \"-target\" type " +
                        "options. The consequence of ignoring\n" +
                        "this warning is that reflective operations " +
                        "on this class will incorrectly\n" +
                        "indicate that it is *not* an inner class.");
            }
            result.add(AnnotationUtils.makeEnclosingClass(
                               foundThisClass.getOuterClass()));
        }
    }

    if (membersSize != 0) {
        StdTypeList typeList = new StdTypeList(membersSize);
        for (int i = 0; i < membersSize; i++) {
            typeList.set(i, membersList.get(i));
        }
        typeList.setImmutable();
        result.add(AnnotationUtils.makeMemberClasses(typeList));
    }

    result.setImmutable();
    return result;
}
 
Example 10
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());
}
 
Example 11
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the {@code InnerClasses} attribute out of a given
 * {@link AttributeList}, if any, translating it to one or more of an
 * {@code InnerClass}, {@code EnclosingClass}, or
 * {@code MemberClasses} annotation.
 *
 * @param thisClass {@code non-null;} type representing the class being
 * processed
 * @param attribs {@code non-null;} the attributes list to search in
 * @param needEnclosingClass whether to include an
 * {@code EnclosingClass} annotation
 * @return {@code null-ok;} the converted list of annotations, if there
 * was an attribute to translate
 */
private static Annotations translateInnerClasses(CstType thisClass,
        AttributeList attribs, boolean needEnclosingClass) {
    AttInnerClasses innerClasses = (AttInnerClasses)
        attribs.findFirst(AttInnerClasses.ATTRIBUTE_NAME);

    if (innerClasses == null) {
        return null;
    }

    /*
     * Search the list for the element representing the current class
     * as well as for any named member classes.
     */

    InnerClassList list = innerClasses.getInnerClasses();
    int size = list.size();
    InnerClassList.Item foundThisClass = null;
    ArrayList<Type> membersList = new ArrayList<Type>();

    for (int i = 0; i < size; i++) {
        InnerClassList.Item item = list.get(i);
        CstType innerClass = item.getInnerClass();
        if (innerClass.equals(thisClass)) {
            foundThisClass = item;
        } else if (thisClass.equals(item.getOuterClass())) {
            membersList.add(innerClass.getClassType());
        }
    }

    int membersSize = membersList.size();

    if ((foundThisClass == null) && (membersSize == 0)) {
        return null;
    }

    Annotations result = new Annotations();

    if (foundThisClass != null) {
        result.add(AnnotationUtils.makeInnerClass(
                           foundThisClass.getInnerName(),
                           foundThisClass.getAccessFlags()));
        if (needEnclosingClass) {
            CstType outer = foundThisClass.getOuterClass();
            if (outer == null) {
                throw new Warning(
                        "Ignoring InnerClasses attribute for an " +
                        "anonymous inner class\n" +
                        "(" + thisClass.toHuman() +
                        ") that doesn't come with an\n" +
                        "associated EnclosingMethod attribute. " +
                        "This class was probably produced by a\n" +
                        "compiler that did not target the modern " +
                        ".class file format. The recommended\n" +
                        "solution is to recompile the class from " +
                        "source, using an up-to-date compiler\n" +
                        "and without specifying any \"-target\" type " +
                        "options. The consequence of ignoring\n" +
                        "this warning is that reflective operations " +
                        "on this class will incorrectly\n" +
                        "indicate that it is *not* an inner class.");
            }
            result.add(AnnotationUtils.makeEnclosingClass(
                               foundThisClass.getOuterClass()));
        }
    }

    if (membersSize != 0) {
        StdTypeList typeList = new StdTypeList(membersSize);
        for (int i = 0; i < membersSize; i++) {
            typeList.set(i, membersList.get(i));
        }
        typeList.setImmutable();
        result.add(AnnotationUtils.makeMemberClasses(typeList));
    }

    result.setImmutable();
    return result;
}
 
Example 12
Source File: AttributeTranslator.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the {@code InnerClasses} attribute out of a given
 * {@link AttributeList}, if any, translating it to one or more of an
 * {@code InnerClass}, {@code EnclosingClass}, or
 * {@code MemberClasses} annotation.
 *
 * @param thisClass {@code non-null;} type representing the class being
 * processed
 * @param attribs {@code non-null;} the attributes list to search in
 * @param needEnclosingClass whether to include an
 * {@code EnclosingClass} annotation
 * @return {@code null-ok;} the converted list of annotations, if there
 * was an attribute to translate
 */
private static Annotations translateInnerClasses(CstType thisClass,
        AttributeList attribs, boolean needEnclosingClass) {
    AttInnerClasses innerClasses = (AttInnerClasses)
        attribs.findFirst(AttInnerClasses.ATTRIBUTE_NAME);

    if (innerClasses == null) {
        return null;
    }

    /*
     * Search the list for the element representing the current class
     * as well as for any named member classes.
     */

    InnerClassList list = innerClasses.getInnerClasses();
    int size = list.size();
    InnerClassList.Item foundThisClass = null;
    ArrayList<Type> membersList = new ArrayList<Type>();

    for (int i = 0; i < size; i++) {
        InnerClassList.Item item = list.get(i);
        CstType innerClass = item.getInnerClass();
        if (innerClass.equals(thisClass)) {
            foundThisClass = item;
        } else if (thisClass.equals(item.getOuterClass())) {
            membersList.add(innerClass.getClassType());
        }
    }

    int membersSize = membersList.size();

    if ((foundThisClass == null) && (membersSize == 0)) {
        return null;
    }

    Annotations result = new Annotations();

    if (foundThisClass != null) {
        result.add(AnnotationUtils.makeInnerClass(
                           foundThisClass.getInnerName(),
                           foundThisClass.getAccessFlags()));
        if (needEnclosingClass) {
            CstType outer = foundThisClass.getOuterClass();
            if (outer == null) {
                throw new Warning(
                        "Ignoring InnerClasses attribute for an " +
                        "anonymous inner class\n" +
                        "(" + thisClass.toHuman() +
                        ") that doesn't come with an\n" +
                        "associated EnclosingMethod attribute. " +
                        "This class was probably produced by a\n" +
                        "compiler that did not target the modern " +
                        ".class file format. The recommended\n" +
                        "solution is to recompile the class from " +
                        "source, using an up-to-date compiler\n" +
                        "and without specifying any \"-target\" type " +
                        "options. The consequence of ignoring\n" +
                        "this warning is that reflective operations " +
                        "on this class will incorrectly\n" +
                        "indicate that it is *not* an inner class.");
            }
            result.add(AnnotationUtils.makeEnclosingClass(
                               foundThisClass.getOuterClass()));
        }
    }

    if (membersSize != 0) {
        StdTypeList typeList = new StdTypeList(membersSize);
        for (int i = 0; i < membersSize; i++) {
            typeList.set(i, membersList.get(i));
        }
        typeList.setImmutable();
        result.add(AnnotationUtils.makeMemberClasses(typeList));
    }

    result.setImmutable();
    return result;
}