Search in sources :

Example 1 with AnnotatedElement

use of org.glassfish.hk2.classmodel.reflect.AnnotatedElement in project Payara by payara.

the class HK2IntegrationUtilities method convertInjectionPointToInjectee.

public static Injectee convertInjectionPointToInjectee(InjectionPoint injectionPoint) {
    InjecteeImpl retVal = new InjecteeImpl(injectionPoint.getType());
    retVal.setRequiredQualifiers(getHK2Qualifiers(injectionPoint));
    // Also sets InjecteeClass
    retVal.setParent((AnnotatedElement) injectionPoint.getMember());
    Annotated annotated = injectionPoint.getAnnotated();
    if (annotated instanceof AnnotatedField) {
        retVal.setPosition(-1);
    } else {
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) annotated;
        retVal.setPosition(annotatedParameter.getPosition());
    }
    return retVal;
}
Also used : Annotated(javax.enterprise.inject.spi.Annotated) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 2 with AnnotatedElement

use of org.glassfish.hk2.classmodel.reflect.AnnotatedElement in project Payara by payara.

the class ApplicationProcessor method getArraySchema.

private static SchemaImpl getArraySchema(AnnotatedElement element, ApiContext context) {
    SchemaImpl arraySchema = new SchemaImpl();
    List<ParameterizedType> parameterizedType;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        parameterizedType = parameter.getParameterizedTypes();
    } else {
        FieldModel field = (FieldModel) element;
        parameterizedType = field.getParameterizedTypes();
    }
    arraySchema.setType(ModelUtils.getSchemaType(parameterizedType.get(0).getTypeName(), context));
    return arraySchema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 3 with AnnotatedElement

use of org.glassfish.hk2.classmodel.reflect.AnnotatedElement in project Payara by payara.

the class ApplicationProcessor method insertObjectReference.

/**
 * Replace the object in the referee with a reference, and create the
 * reference in the API.
 *
 * @param context the API context.
 * @param referee the object containing the reference.
 * @param referenceClass the class of the object being referenced.
 * @return if the reference has been created.
 */
private boolean insertObjectReference(ApiContext context, Reference<?> referee, AnnotatedElement referenceClass, String referenceClassName) {
    // Firstly check if it's been already defined (i.e. config property definition)
    for (Entry<String, Schema> schemaEntry : context.getApi().getComponents().getSchemas().entrySet()) {
        final Schema entryValue = schemaEntry.getValue();
        if (entryValue instanceof SchemaImpl) {
            final SchemaImpl entryValueImpl = (SchemaImpl) entryValue;
            final String implementationClass = entryValueImpl.getImplementation();
            if (implementationClass != null && implementationClass.equals(referenceClassName)) {
                referee.setRef(schemaEntry.getKey());
                return true;
            }
        }
    }
    // If the object is a java core class
    if (referenceClassName == null || referenceClassName.startsWith("java.")) {
        return false;
    }
    // If the object is a Java EE object type
    if (referenceClassName.startsWith("javax.")) {
        return false;
    }
    // Check the class exists in the application
    if (!context.isApplicationType(referenceClassName)) {
        return false;
    }
    if (referenceClass != null && referenceClass instanceof ExtensibleType) {
        ExtensibleType referenceClassType = (ExtensibleType) referenceClass;
        final AnnotationModel schemaAnnotation = context.getAnnotationInfo(referenceClassType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        String schemaName = ModelUtils.getSchemaName(context, referenceClass);
        // Set the reference name
        referee.setRef(schemaName);
        Schema schema = context.getApi().getComponents().getSchemas().get(schemaName);
        if (schema == null) {
            // Create the schema
            if (context.isAllowedType(referenceClassType)) {
                visitSchema(schemaAnnotation, referenceClassType, context);
            } else if (referenceClassType instanceof ClassModel) {
                apiWalker.processAnnotation((ClassModel) referenceClassType, this);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { referenceClassName });
            }
        }
        return true;
    }
    return false;
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType)

Example 4 with AnnotatedElement

use of org.glassfish.hk2.classmodel.reflect.AnnotatedElement in project Payara by payara.

the class ApplicationProcessor method visitAPIResponseSchema.

@Override
public void visitAPIResponseSchema(AnnotationModel apiResponseSchema, AnnotatedElement element, ApiContext context) {
    final APIResponseImpl response = APIResponseImpl.createInstance(apiResponseSchema, context);
    final OperationImpl operation = (OperationImpl) context.getWorkingOperation();
    // Handle exception mappers
    if (operation == null) {
        if (element instanceof MethodModel && "toResponse".equals(element.getName())) {
            final MethodModel methodModel = (MethodModel) element;
            final String exceptionType = methodModel.getParameter(0).getTypeName();
            mapException(context, exceptionType, response);
        } else {
            LOGGER.warning("Unrecognised annotation position at: " + element.shortDesc());
        }
        return;
    }
    // If response code hasn't been specified
    String responseCode = response.getResponseCode();
    if (responseCode == null || responseCode.isEmpty()) {
        assert element instanceof MethodModel;
        final MethodModel method = (MethodModel) element;
        if (isVoid(method.getReturnType())) {
            if (HttpMethod.POST.equals(operation.getMethod())) {
                responseCode = "201";
            } else if (Arrays.asList(method.getArgumentTypes()).contains("javax.ws.rs.container.AsyncResponse")) {
                responseCode = "200";
            } else {
                responseCode = "204";
            }
        } else {
            responseCode = "200";
        }
    }
    response.setResponseCode(responseCode);
    // If the response description hasn't been specified
    final String responseDescription = response.getDescription();
    if (responseDescription == null || responseDescription.isEmpty()) {
        try {
            final int statusInt = Integer.parseInt(responseCode);
            final Status status = Status.fromStatusCode(statusInt);
            if (status != null) {
                response.setDescription(status.getReasonPhrase());
            }
        } catch (NumberFormatException ex) {
            LOGGER.log(Level.FINE, "Unrecognised status code, description will be empty", ex);
        }
    }
    final APIResponses responses = operation.getResponses();
    // Remove the default response
    final APIResponse defaultResponse = responses.getAPIResponse(APIResponses.DEFAULT);
    if (defaultResponse != null) {
        responses.removeAPIResponse(APIResponses.DEFAULT);
        responses.addAPIResponse(responseCode, defaultResponse);
    }
    // Add the generated response
    APIResponsesImpl.merge(response, responses, true, context);
}
Also used : Status(javax.ws.rs.core.Response.Status) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) OperationImpl(fish.payara.microprofile.openapi.impl.model.OperationImpl) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)

Example 5 with AnnotatedElement

use of org.glassfish.hk2.classmodel.reflect.AnnotatedElement in project Payara by payara.

the class ApplicationProcessor method addParameter.

private static void addParameter(AnnotatedElement element, ApiContext context, String name, In in, Boolean required) {
    Boolean hidden = false;
    AnnotationModel paramAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.parameters.Parameter.class.getName());
    if (paramAnnotation != null) {
        hidden = paramAnnotation.getValue("hidden", Boolean.class);
    }
    if (hidden != null && hidden) {
        return;
    }
    Parameter newParameter = new ParameterImpl();
    newParameter.setName(name);
    newParameter.setIn(in);
    newParameter.setRequired(required);
    SchemaImpl schema = new SchemaImpl();
    String defaultValue = getDefaultValueIfPresent(element);
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        schema.setType(ModelUtils.getSchemaType(parameter.getTypeName(), context));
    } else {
        FieldModel field = (FieldModel) element;
        schema.setType(ModelUtils.getSchemaType(field.getTypeName(), context));
    }
    if (schema.getType() == SchemaType.ARRAY) {
        schema.setItems(getArraySchema(element, context));
        if (defaultValue != null) {
            schema.getItems().setDefaultValue(defaultValue);
        }
    } else if (defaultValue != null) {
        schema.setDefaultValue(defaultValue);
    }
    newParameter.setSchema(schema);
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        for (Parameter parameter : workingOperation.getParameters()) {
            final String parameterName = parameter.getName();
            if (parameterName != null && parameterName.equals(newParameter.getName())) {
                ParameterImpl.merge(newParameter, parameter, false, context);
                return;
            }
        }
        workingOperation.addParameter(newParameter);
    } else {
        LOGGER.log(SEVERE, "Couldn''t add {0} parameter, \"{1}\" to the OpenAPI Document. This is usually caused by declaring parameter under a method with an unsupported annotation.", new Object[] { newParameter.getIn(), newParameter.getName() });
    }
}
Also used : Operation(org.eclipse.microprofile.openapi.models.Operation) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)14 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)8 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)8 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)5 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)4 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)4 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)3 ExternalDocumentationImpl (fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl)2 OperationImpl (fish.payara.microprofile.openapi.impl.model.OperationImpl)2 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)2 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)2 SecurityRequirementImpl (fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl)2 ServerImpl (fish.payara.microprofile.openapi.impl.model.servers.ServerImpl)2 TagImpl (fish.payara.microprofile.openapi.impl.model.tags.TagImpl)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2