Search in sources :

Example 6 with Annotation

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

the class AttributeTranslator method translateAnnotationDefaults.

/**
     * 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);
}
Also used : NameValuePair(com.android.dx.rop.annotation.NameValuePair) AttributeList(com.android.dx.cf.iface.AttributeList) CstType(com.android.dx.rop.cst.CstType) MethodList(com.android.dx.cf.iface.MethodList) AttEnclosingMethod(com.android.dx.cf.attrib.AttEnclosingMethod) Method(com.android.dx.cf.iface.Method) Annotation(com.android.dx.rop.annotation.Annotation) AttAnnotationDefault(com.android.dx.cf.attrib.AttAnnotationDefault)

Example 7 with Annotation

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

the class AttributeTranslator method getMethodAnnotations.

/**
     * Gets the annotations out of a given method, similar to {@link
     * #getAnnotations}, also including an annotation for the translation
     * of the method-specific attribute {@code Exceptions}.
     *
     * @param method {@code non-null;} the method in question
     * @return {@code non-null;} the set of annotations, which may be empty
     */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);
    if (exceptions.size() != 0) {
        Annotation throwsAnnotation = AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }
    return result;
}
Also used : AttRuntimeVisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttRuntimeVisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) Annotations(com.android.dx.rop.annotation.Annotations) AttRuntimeInvisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) AttRuntimeInvisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) TypeList(com.android.dx.rop.type.TypeList) StdTypeList(com.android.dx.rop.type.StdTypeList) Annotation(com.android.dx.rop.annotation.Annotation)

Example 8 with Annotation

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

the class AnnotationParser method parseAnnotation.

/**
     * 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;
}
Also used : NameValuePair(com.android.dx.rop.annotation.NameValuePair) CstType(com.android.dx.rop.cst.CstType) CstString(com.android.dx.rop.cst.CstString) Annotation(com.android.dx.rop.annotation.Annotation) CstAnnotation(com.android.dx.rop.cst.CstAnnotation)

Example 9 with Annotation

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

the class AnnotationParser method parseValue.

/**
     * Parses an annotation value.
     *
     * @return {@code non-null;} the parsed value
     */
private Constant parseValue() throws IOException {
    int tag = input.readUnsignedByte();
    if (observer != null) {
        CstString humanTag = new CstString(Character.toString((char) tag));
        parsed(1, "tag: " + humanTag.toQuoted());
    }
    switch(tag) {
        case 'B':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstByte.make(value.getValue());
            }
        case 'C':
            {
                CstInteger value = (CstInteger) parseConstant();
                int intValue = value.getValue();
                return CstChar.make(value.getValue());
            }
        case 'D':
            {
                CstDouble value = (CstDouble) parseConstant();
                return value;
            }
        case 'F':
            {
                CstFloat value = (CstFloat) parseConstant();
                return value;
            }
        case 'I':
            {
                CstInteger value = (CstInteger) parseConstant();
                return value;
            }
        case 'J':
            {
                CstLong value = (CstLong) parseConstant();
                return value;
            }
        case 'S':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstShort.make(value.getValue());
            }
        case 'Z':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstBoolean.make(value.getValue());
            }
        case 'c':
            {
                int classInfoIndex = input.readUnsignedShort();
                CstString value = (CstString) pool.get(classInfoIndex);
                Type type = Type.internReturnType(value.getString());
                if (observer != null) {
                    parsed(2, "class_info: " + type.toHuman());
                }
                return new CstType(type);
            }
        case 's':
            {
                return parseConstant();
            }
        case 'e':
            {
                requireLength(4);
                int typeNameIndex = input.readUnsignedShort();
                int constNameIndex = input.readUnsignedShort();
                CstString typeName = (CstString) pool.get(typeNameIndex);
                CstString constName = (CstString) pool.get(constNameIndex);
                if (observer != null) {
                    parsed(2, "type_name: " + typeName.toHuman());
                    parsed(2, "const_name: " + constName.toHuman());
                }
                return new CstEnumRef(new CstNat(constName, typeName));
            }
        case '@':
            {
                Annotation annotation = parseAnnotation(AnnotationVisibility.EMBEDDED);
                return new CstAnnotation(annotation);
            }
        case '[':
            {
                requireLength(2);
                int numValues = input.readUnsignedShort();
                CstArray.List list = new CstArray.List(numValues);
                if (observer != null) {
                    parsed(2, "num_values: " + numValues);
                    changeIndent(1);
                }
                for (int i = 0; i < numValues; i++) {
                    if (observer != null) {
                        changeIndent(-1);
                        parsed(0, "element_value[" + i + "]:");
                        changeIndent(1);
                    }
                    list.set(i, parseValue());
                }
                if (observer != null) {
                    changeIndent(-1);
                }
                list.setImmutable();
                return new CstArray(list);
            }
        default:
            {
                throw new ParseException("unknown annotation tag: " + Hex.u1(tag));
            }
    }
}
Also used : CstFloat(com.android.dx.rop.cst.CstFloat) CstNat(com.android.dx.rop.cst.CstNat) CstArray(com.android.dx.rop.cst.CstArray) CstLong(com.android.dx.rop.cst.CstLong) CstString(com.android.dx.rop.cst.CstString) CstAnnotation(com.android.dx.rop.cst.CstAnnotation) CstDouble(com.android.dx.rop.cst.CstDouble) CstEnumRef(com.android.dx.rop.cst.CstEnumRef) Annotation(com.android.dx.rop.annotation.Annotation) CstAnnotation(com.android.dx.rop.cst.CstAnnotation) Type(com.android.dx.rop.type.Type) CstType(com.android.dx.rop.cst.CstType) CstInteger(com.android.dx.rop.cst.CstInteger) CstType(com.android.dx.rop.cst.CstType) AnnotationsList(com.android.dx.rop.annotation.AnnotationsList) ParseException(com.android.dx.cf.iface.ParseException)

Example 10 with Annotation

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

the class AnnotationParser method parseAnnotations.

/**
 * 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;
}
Also used : Annotations(com.android.dx.rop.annotation.Annotations) Annotation(com.android.dx.rop.annotation.Annotation) CstAnnotation(com.android.dx.rop.cst.CstAnnotation)

Aggregations

Annotation (com.android.dx.rop.annotation.Annotation)32 CstAnnotation (com.android.dx.rop.cst.CstAnnotation)21 NameValuePair (com.android.dx.rop.annotation.NameValuePair)19 Annotations (com.android.dx.rop.annotation.Annotations)10 CstArray (com.android.dx.rop.cst.CstArray)8 CstString (com.android.dx.rop.cst.CstString)8 CstType (com.android.dx.rop.cst.CstType)8 AttRuntimeInvisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)6 AttRuntimeInvisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)6 AttRuntimeVisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)6 AttRuntimeVisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)6 AttributeList (com.android.dx.cf.iface.AttributeList)4 AnnotationsList (com.android.dx.rop.annotation.AnnotationsList)4 CstNat (com.android.dx.rop.cst.CstNat)4 TypeList (com.android.dx.rop.type.TypeList)4 AttAnnotationDefault (com.android.dx.cf.attrib.AttAnnotationDefault)2 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)2 Method (com.android.dx.cf.iface.Method)2 MethodList (com.android.dx.cf.iface.MethodList)2 ParseException (com.android.dx.cf.iface.ParseException)2