use of org.glassfish.hk2.classmodel.reflect.MethodModel 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);
}
use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.
the class ApplicationProcessor method visitExternalDocumentation.
@Override
public void visitExternalDocumentation(AnnotationModel externalDocs, AnnotatedElement element, ApiContext context) {
if (element instanceof MethodModel) {
ExternalDocumentation newExternalDocs = new ExternalDocumentationImpl();
ExternalDocumentationImpl.merge(ExternalDocumentationImpl.createInstance(externalDocs), newExternalDocs, true);
if (newExternalDocs.getUrl() != null && !newExternalDocs.getUrl().isEmpty()) {
context.getWorkingOperation().setExternalDocs(newExternalDocs);
}
}
}
use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.
the class ApplicationProcessor method visitTag.
@Override
public void visitTag(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
Tag from = TagImpl.createInstance(annotation, context);
if (element instanceof MethodModel) {
final List<Tag> tags = new ArrayList<>();
tags.addAll(context.getApi().getTags());
TagImpl.merge(from, context.getWorkingOperation(), true, tags);
context.getApi().setTags(tags);
} else {
Tag newTag = new TagImpl();
TagImpl.merge(from, newTag, true);
if (newTag.getName() != null && !newTag.getName().isEmpty()) {
context.getApi().addTag(newTag);
}
}
}
use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.
the class ApplicationProcessor method visitTags.
@Override
public void visitTags(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
if (element instanceof MethodModel) {
List<AnnotationModel> tags = annotation.getValue("value", List.class);
if (tags != null) {
for (AnnotationModel tag : tags) {
visitTag(tag, element, context);
}
}
List<String> refs = annotation.getValue("refs", List.class);
if (refs != null) {
for (String ref : refs) {
if (ref != null && !ref.isEmpty()) {
context.getWorkingOperation().addTag(ref);
}
}
}
}
}
use of org.glassfish.hk2.classmodel.reflect.MethodModel 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());
}
Aggregations