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

The following examples show how to use com.android.dx.rop.annotation.Annotation. 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 6 votes vote down vote up
/**
 * Gets the annotations out of a given {@link AttributeList}. This
 * combines both visible and invisible annotations into a single
 * result set and also adds in a system annotation for the
 * {@code Signature} attribute if present.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getAnnotations(AttributeList attribs) {
    Annotations result = getAnnotations0(attribs);
    Annotation signature = getSignature(attribs);
    Annotation sourceDebugExtension = getSourceDebugExtension(attribs);

    if (signature != null) {
        result = Annotations.combine(result, signature);
    }

    if (sourceDebugExtension != null) {
        result = Annotations.combine(result, sourceDebugExtension);
    }

    return result;
}
 
Example #2
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

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

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #3
Source File: AnnotationLister.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Inspects a class annotation.
 *
 * @param cf {@code non-null;} class file
 * @param ann {@code non-null;} annotation
 */
private void visitClassAnnotation(DirectClassFile cf,
        BaseAnnotations ann) {

    if (!args.eTypes.contains(ElementType.TYPE)) {
        return;
    }

    for (Annotation anAnn : ann.getAnnotations().getAnnotations()) {
        String annClassName
                = anAnn.getType().getClassType().getClassName();
        if (args.aclass.equals(annClassName)) {
            printMatch(cf);
        }
    }
}
 
Example #4
Source File: AttributeTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the annotations out of a given {@link AttributeList}. This
 * combines both visible and invisible annotations into a single
 * result set and also adds in a system annotation for the
 * {@code Signature} attribute if present.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getAnnotations(AttributeList attribs) {
    Annotations result = getAnnotations0(attribs);
    Annotation signature = getSignature(attribs);
    Annotation sourceDebugExtension = getSourceDebugExtension(attribs);

    if (signature != null) {
        result = Annotations.combine(result, signature);
    }

    if (sourceDebugExtension != null) {
        result = Annotations.combine(result, sourceDebugExtension);
    }

    return result;
}
 
Example #5
Source File: AnnotationItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param annotation {@code non-null;} annotation to represent
 * @param dexFile {@code non-null;} dex output
 */
public AnnotationItem(Annotation annotation, DexFile dexFile) {
    /*
     * The write size isn't known up-front because (the variable-lengthed)
     * leb128 type is used to represent some things.
     */
    super(ALIGNMENT, -1);

    if (annotation == null) {
        throw new NullPointerException("annotation == null");
    }

    this.annotation = annotation;
    this.type = null;
    this.encodedForm = null;
    addContents(dexFile);
}
 
Example #6
Source File: AnnotationItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param annotation {@code non-null;} annotation to represent
 * @param dexFile {@code non-null;} dex output
 */
public AnnotationItem(Annotation annotation, DexFile dexFile) {
    /*
     * The write size isn't known up-front because (the variable-lengthed)
     * leb128 type is used to represent some things.
     */
    super(ALIGNMENT, -1);

    if (annotation == null) {
        throw new NullPointerException("annotation == null");
    }

    this.annotation = annotation;
    this.type = null;
    this.encodedForm = null;
    addContents(dexFile);
}
 
Example #7
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the annotations out of a given {@link AttributeList}. This
 * combines both visible and invisible annotations into a single
 * result set and also adds in a system annotation for the
 * {@code Signature} attribute if present.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getAnnotations(AttributeList attribs) {
    Annotations result = getAnnotations0(attribs);
    Annotation signature = getSignature(attribs);
    Annotation sourceDebugExtension = getSourceDebugExtension(attribs);

    if (signature != null) {
        result = Annotations.combine(result, signature);
    }

    if (sourceDebugExtension != null) {
        result = Annotations.combine(result, sourceDebugExtension);
    }

    return result;
}
 
Example #8
Source File: AttributeTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

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

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #9
Source File: AnnotationLister.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Inspects a class annotation.
 *
 * @param cf {@code non-null;} class file
 * @param ann {@code non-null;} annotation
 */
private void visitClassAnnotation(DirectClassFile cf,
        BaseAnnotations ann) {

    if (!args.eTypes.contains(ElementType.TYPE)) {
        return;
    }

    for (Annotation anAnn : ann.getAnnotations().getAnnotations()) {
        String annClassName
                = anAnn.getType().getClassType().getClassName();
        if (args.aclass.equals(annClassName)) {
            printMatch(cf);
        }
    }
}
 
Example #10
Source File: AnnotationLister.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Inspects a class annotation.
 *
 * @param cf {@code non-null;} class file
 * @param ann {@code non-null;} annotation
 */
private void visitClassAnnotation(DirectClassFile cf,
        BaseAnnotations ann) {

    if (!args.eTypes.contains(ElementType.TYPE)) {
        return;
    }

    for (Annotation anAnn : ann.getAnnotations().getAnnotations()) {
        String annClassName
                = anAnn.getType().getClassType().getClassName();
        if (args.aclass.equals(annClassName)) {
            printMatch(cf);
        }
    }
}
 
Example #11
Source File: AttributeTranslator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

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

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #12
Source File: AttributeTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

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

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #13
Source File: AnnotationItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param annotation {@code non-null;} annotation to represent
 * @param dexFile {@code non-null;} dex output
 */
public AnnotationItem(Annotation annotation, DexFile dexFile) {
    /*
     * The write size isn't known up-front because (the variable-lengthed)
     * leb128 type is used to represent some things.
     */
    super(ALIGNMENT, -1);

    if (annotation == null) {
        throw new NullPointerException("annotation == null");
    }

    this.annotation = annotation;
    this.type = null;
    this.encodedForm = null;
    addContents(dexFile);
}
 
Example #14
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 #15
Source File: AnnotationLister.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Inspects a package annotation
 *
 * @param cf {@code non-null;} class file of "package-info" pseudo-class
 * @param ann {@code non-null;} annotation
 */
private void visitPackageAnnotation(
        DirectClassFile cf, BaseAnnotations ann) {

    if (!args.eTypes.contains(ElementType.PACKAGE)) {
        return;
    }

    String packageName = cf.getThisClass().getClassType().getClassName();

    int slashIndex = packageName.lastIndexOf('/');

    if (slashIndex == -1) {
        packageName = "";
    } else {
        packageName
                = packageName.substring(0, slashIndex);
    }


    for (Annotation anAnn : ann.getAnnotations().getAnnotations()) {
        String annClassName
                = anAnn.getType().getClassType().getClassName();
        if (args.aclass.equals(annClassName)) {
            printMatchPackage(packageName);
        }
    }
}
 
Example #16
Source File: CstAnnotation.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param annotation {@code non-null;} the annotation to hold
 */
public CstAnnotation(Annotation annotation) {
    if (annotation == null) {
        throw new NullPointerException("annotation == null");
    }

    annotation.throwIfMutable();

    this.annotation = annotation;
}
 
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: 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 #19
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 #20
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 #21
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 #22
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 #23
Source File: ValueEncoder.java    From Box 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 #24
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code Signature} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code Signature} annotation,
 * if there was an attribute to translate
 */
private static Annotation getSignature(AttributeList attribs) {
    AttSignature signature = (AttSignature)
        attribs.findFirst(AttSignature.ATTRIBUTE_NAME);

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

    return AnnotationUtils.makeSignature(signature.getSignature());
}
 
Example #25
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 #26
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given class, similar to {@link
 * #getAnnotations}, also including annotations for translations
 * of class-level attributes {@code EnclosingMethod} and
 * {@code InnerClasses}, if present. Additionally, if the
 * class is an annotation class, then this also includes a
 * representation of all the {@code AnnotationDefault}
 * values.
 *
 * @param cf {@code non-null;} the class in question
 * @param args {@code non-null;} the high-level options
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getClassAnnotations(DirectClassFile cf,
        CfOptions args) {
    CstType thisClass = cf.getThisClass();
    AttributeList attribs = cf.getAttributes();
    Annotations result = getAnnotations(attribs);
    Annotation enclosingMethod = translateEnclosingMethod(attribs);

    try {
        Annotations innerClassAnnotations =
            translateInnerClasses(thisClass, attribs,
                    enclosingMethod == null);
        if (innerClassAnnotations != null) {
            result = Annotations.combine(result, innerClassAnnotations);
        }
    } catch (Warning warn) {
        args.warn.println("warning: " + warn.getMessage());
    }

    if (enclosingMethod != null) {
        result = Annotations.combine(result, enclosingMethod);
    }

    if (AccessFlags.isAnnotation(cf.getAccessFlags())) {
        Annotation annotationDefault =
            translateAnnotationDefaults(cf);
        if (annotationDefault != null) {
            result = Annotations.combine(result, annotationDefault);
        }
    }

    return result;
}
 
Example #27
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 #28
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 #29
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 #30
Source File: CstAnnotation.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param annotation {@code non-null;} the annotation to hold
 */
public CstAnnotation(Annotation annotation) {
    if (annotation == null) {
        throw new NullPointerException("annotation == null");
    }

    annotation.throwIfMutable();

    this.annotation = annotation;
}