Search in sources :

Example 1 with AnnotationInfo

use of fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo in project Payara by payara.

the class ModelUtils method getSchemaName.

@SuppressWarnings("unchecked")
public static String getSchemaName(ApiContext context, AnnotatedElement type) {
    assert type != null;
    // context and annotation can be null
    final Class<? extends Annotation>[] ANNOTATION_TYPES = new Class[] { org.eclipse.microprofile.openapi.annotations.media.Schema.class, javax.xml.bind.annotation.XmlRootElement.class, javax.xml.bind.annotation.XmlElement.class };
    for (Class<? extends Annotation> annotationType : ANNOTATION_TYPES) {
        AnnotationModel annotationModel;
        // Fetch the element annotations
        if (context != null && type instanceof ExtensibleType) {
            // Fetch the annotation from the cache
            ExtensibleType<?> implementationType = (ExtensibleType<?>) type;
            AnnotationInfo annotationInfo = context.getAnnotationInfo(implementationType);
            annotationModel = annotationInfo.getAnnotation(annotationType);
        } else {
            // Fetch the annotation manually
            annotationModel = type.getAnnotation(annotationType.getName());
        }
        // Fields can be named by their accessors
        if (annotationModel == null) {
            if (type instanceof FieldModel) {
                final FieldModel field = (FieldModel) type;
                final String accessorName = getAccessorName(field.getName());
                for (MethodModel method : field.getDeclaringType().getMethods()) {
                    // Check if it's the accessor
                    if (accessorName.equals(method.getName())) {
                        annotationModel = type.getAnnotation(annotationType.getName());
                        break;
                    }
                }
            }
        }
        // Get the schema name if the annotation exists
        if (annotationModel != null) {
            final String name = annotationModel.getValue("name", String.class);
            if (name != null && !name.isEmpty() && !name.equals("##default")) {
                return name;
            }
        }
    }
    return getSimpleName(type.getName());
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Annotation(java.lang.annotation.Annotation) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) AnnotationInfo(fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo)

Aggregations

AnnotationInfo (fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo)1 Annotation (java.lang.annotation.Annotation)1 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)1 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)1 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)1 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)1