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;
}
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;
}
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;
}
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);
}
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() });
}
}
Aggregations