Search in sources :

Example 1 with TypeAnnotation

use of sun.reflect.annotation.TypeAnnotation in project jdk8u_jdk by JetBrains.

the class TypeAnnotationParser method parseTypeAnnotations.

/* Parse type annotations encoded as an array of bytes */
private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations, ConstantPool cp, AnnotatedElement baseDecl, Class<?> container) {
    if (rawAnnotations == null)
        return EMPTY_TYPE_ANNOTATION_ARRAY;
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int annotationCount = buf.getShort() & 0xFFFF;
    List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);
    // Parse each TypeAnnotation
    for (int i = 0; i < annotationCount; i++) {
        TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
        if (ta != null)
            typeAnnotations.add(ta);
    }
    return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
}
Also used : TypeAnnotation(sun.reflect.annotation.TypeAnnotation) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 2 with TypeAnnotation

use of sun.reflect.annotation.TypeAnnotation in project jdk8u_jdk by JetBrains.

the class TypeAnnotationParser method parseTypeAnnotation.

private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf, ConstantPool cp, AnnotatedElement baseDecl, Class<?> container) {
    try {
        TypeAnnotationTargetInfo ti = parseTargetInfo(buf);
        LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf);
        Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false);
        if (// Inside a method for example
        ti == null)
            return null;
        return new TypeAnnotation(ti, locationInfo, a, baseDecl);
    } catch (// Bad type in const pool at specified index
    IllegalArgumentException | BufferUnderflowException e) {
        throw new AnnotationFormatError(e);
    }
}
Also used : TypeAnnotation(sun.reflect.annotation.TypeAnnotation) TypeAnnotation(sun.reflect.annotation.TypeAnnotation) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 3 with TypeAnnotation

use of sun.reflect.annotation.TypeAnnotation in project jdk8u_jdk by JetBrains.

the class TypeAnnotationParser method parseTypeVariableAnnotations.

// TypeVariable helpers
/**
     * Parse regular annotations on a TypeVariable declared on genericDecl.
     *
     * Regular Annotations on TypeVariables are stored in the type
     * annotation byte[] in the class file.
     *
     * @param genericsDecl the declaration declaring the type variable
     * @param typeVarIndex the 0-based index of this type variable in the declaration
     */
public static <D extends GenericDeclaration> Annotation[] parseTypeVariableAnnotations(D genericDecl, int typeVarIndex) {
    AnnotatedElement decl;
    TypeAnnotationTarget predicate;
    if (genericDecl instanceof Class) {
        decl = (Class<?>) genericDecl;
        predicate = TypeAnnotationTarget.CLASS_TYPE_PARAMETER;
    } else if (genericDecl instanceof Executable) {
        decl = (Executable) genericDecl;
        predicate = TypeAnnotationTarget.METHOD_TYPE_PARAMETER;
    } else {
        throw new AssertionError("Unknown GenericDeclaration " + genericDecl + "\nthis should not happen.");
    }
    List<TypeAnnotation> typeVarAnnos = TypeAnnotation.filter(parseAllTypeAnnotations(decl), predicate);
    List<Annotation> res = new ArrayList<>(typeVarAnnos.size());
    for (TypeAnnotation t : typeVarAnnos) if (t.getTargetInfo().getCount() == typeVarIndex)
        res.add(t.getAnnotation());
    return res.toArray(new Annotation[0]);
}
Also used : TypeAnnotation(sun.reflect.annotation.TypeAnnotation) ArrayList(java.util.ArrayList) TypeAnnotation(sun.reflect.annotation.TypeAnnotation)

Example 4 with TypeAnnotation

use of sun.reflect.annotation.TypeAnnotation in project jdk8u_jdk by JetBrains.

the class TypeAnnotationParser method mapTypeAnnotations.

// Helper
static Map<Class<? extends Annotation>, Annotation> mapTypeAnnotations(TypeAnnotation[] typeAnnos) {
    Map<Class<? extends Annotation>, Annotation> result = new LinkedHashMap<>();
    for (TypeAnnotation t : typeAnnos) {
        Annotation a = t.getAnnotation();
        Class<? extends Annotation> klass = a.annotationType();
        AnnotationType type = AnnotationType.getInstance(klass);
        if (type.retention() == RetentionPolicy.RUNTIME)
            if (result.put(klass, a) != null)
                throw new AnnotationFormatError("Duplicate annotation for class: " + klass + ": " + a);
    }
    return result;
}
Also used : TypeAnnotation(sun.reflect.annotation.TypeAnnotation) TypeAnnotation(sun.reflect.annotation.TypeAnnotation) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with TypeAnnotation

use of sun.reflect.annotation.TypeAnnotation in project jdk8u_jdk by JetBrains.

the class TypeAnnotationParser method buildAnnotatedType.

/**
     * Build an AnnotatedType from the parameters supplied.
     *
     * This method and {@code buildAnnotatedTypes} are probably
     * the entry points you are looking for.
     *
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
     * @param cp the ConstantPool needed to parse the embedded Annotation
     * @param decl the declaration this type annotation is on
     * @param container the Class this type annotation is on (may be the same as decl)
     * @param type the type the AnnotatedType corresponds to
     * @param filter the type annotation targets included in this AnnotatedType
     */
public static AnnotatedType buildAnnotatedType(byte[] rawAnnotations, ConstantPool cp, AnnotatedElement decl, Class<?> container, Type type, TypeAnnotationTarget filter) {
    TypeAnnotation[] tas = parseTypeAnnotations(rawAnnotations, cp, decl, container);
    List<TypeAnnotation> l = new ArrayList<>(tas.length);
    for (TypeAnnotation t : tas) {
        TypeAnnotationTargetInfo ti = t.getTargetInfo();
        if (ti.getTarget() == filter)
            l.add(t);
    }
    TypeAnnotation[] typeAnnotations = l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
    return AnnotatedTypeFactory.buildAnnotatedType(type, LocationInfo.BASE_LOCATION, typeAnnotations, typeAnnotations, decl);
}
Also used : TypeAnnotation(sun.reflect.annotation.TypeAnnotation) ArrayList(java.util.ArrayList)

Aggregations

TypeAnnotation (sun.reflect.annotation.TypeAnnotation)7 ArrayList (java.util.ArrayList)5 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedHashMap (java.util.LinkedHashMap)1